让按钮上图像的透明部分透明
Let the transparent part of the image on a button be transparent
我使用了 2 张图片,其中一张是屏幕的背景,另一张是按钮的背景。按钮的图像在某些部分是透明的。我想要它覆盖透明部分的背景图像
它应该看起来像 this, but it doesn't. It looks like this。
这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TryOut1 extends JFrame
{
public TryOut1()
{
screen();
buttonDoor();
setSize(1280,1024);
}
public void screen(){
setSize(1280,1024);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("Ziegel4.jpg")));
setLayout(new FlowLayout());
setSize(1280,1024);
}
public void buttonDoor(){
JButton b1 = new JButton(new ImageIcon("Tor2.png"));
b1.setEnabled(true);
b1.setVisible(true);
b1.setBackground(new Color( 0, 0, 0, 0) );
add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
}
public static void main(String args[])
{
new TryOut1();
}
}
我怎样才能使图像的透明部分真正透明
提前感谢您的帮助^^
与其创建 JLabel
,不如向其添加背景图像,然后将此标签添加到您的内容窗格并不是最佳方法。标签将被视为一个组件,布局管理器将不会正确定位所有其他组件。
您应该通过覆盖 paintComponent(Graphics g)
方法来绘制内容窗格的背景图像,如图所示 here.
然后更改 JButton
的适当属性并使其透明,如图所示 here.
所有这些都在 SSCCE:
public class TryOut1 extends JFrame {
public TryOut1() {
try {
screen();
} catch (IOException e) {
e.printStackTrace();
}
buttonDoor();
setSize(1280, 1024);
}
public void screen() throws IOException {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
File desktop = new File(System.getProperty("user.home"), "Desktop");
File backgroundImg = new File(desktop, "background.png");
Image img = ImageIO.read(backgroundImg);
JPanel contentPane = new JPanel(new FlowLayout()) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
};
setContentPane(contentPane);
}
public void buttonDoor() {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File doorFile = new File(desktop, "door.png");
JButton b1 = new JButton(new ImageIcon(doorFile.getAbsolutePath()));
b1.setOpaque(false);
b1.setContentAreaFilled(false);
b1.setBorder(BorderFactory.createEmptyBorder());
b1.setBorderPainted(false);
add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
public static void main(String args[]) {
// All swing apps must run in their own thread.
SwingUtilities.invokeLater(() -> new TryOut1().setVisible(true));
}
}
预览:(忽略右边的白色space,我的背景图比较小)
在您的按钮上尝试以下方法:
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setFocusPainted(false);
b1.setOpaque(false);
我使用了 2 张图片,其中一张是屏幕的背景,另一张是按钮的背景。按钮的图像在某些部分是透明的。我想要它覆盖透明部分的背景图像
它应该看起来像 this, but it doesn't. It looks like this。
这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TryOut1 extends JFrame
{
public TryOut1()
{
screen();
buttonDoor();
setSize(1280,1024);
}
public void screen(){
setSize(1280,1024);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("Ziegel4.jpg")));
setLayout(new FlowLayout());
setSize(1280,1024);
}
public void buttonDoor(){
JButton b1 = new JButton(new ImageIcon("Tor2.png"));
b1.setEnabled(true);
b1.setVisible(true);
b1.setBackground(new Color( 0, 0, 0, 0) );
add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
}
public static void main(String args[])
{
new TryOut1();
}
}
我怎样才能使图像的透明部分真正透明
提前感谢您的帮助^^
与其创建 JLabel
,不如向其添加背景图像,然后将此标签添加到您的内容窗格并不是最佳方法。标签将被视为一个组件,布局管理器将不会正确定位所有其他组件。
您应该通过覆盖 paintComponent(Graphics g)
方法来绘制内容窗格的背景图像,如图所示 here.
然后更改 JButton
的适当属性并使其透明,如图所示 here.
所有这些都在 SSCCE:
public class TryOut1 extends JFrame {
public TryOut1() {
try {
screen();
} catch (IOException e) {
e.printStackTrace();
}
buttonDoor();
setSize(1280, 1024);
}
public void screen() throws IOException {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
File desktop = new File(System.getProperty("user.home"), "Desktop");
File backgroundImg = new File(desktop, "background.png");
Image img = ImageIO.read(backgroundImg);
JPanel contentPane = new JPanel(new FlowLayout()) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
};
setContentPane(contentPane);
}
public void buttonDoor() {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File doorFile = new File(desktop, "door.png");
JButton b1 = new JButton(new ImageIcon(doorFile.getAbsolutePath()));
b1.setOpaque(false);
b1.setContentAreaFilled(false);
b1.setBorder(BorderFactory.createEmptyBorder());
b1.setBorderPainted(false);
add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
public static void main(String args[]) {
// All swing apps must run in their own thread.
SwingUtilities.invokeLater(() -> new TryOut1().setVisible(true));
}
}
预览:(忽略右边的白色space,我的背景图比较小)
在您的按钮上尝试以下方法:
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setFocusPainted(false);
b1.setOpaque(false);