JFrame 中的多个 PNG
Multiple PNG's in JFrame
我试图在 JFrame
中显示多个具有透明背景的 png,但它们始终具有白色背景。
这是我的代码:
JFrame frame = this;
frame.setUndecorated(true);
back1 = new ImageIcon("res/launcher/back_1.png");
JLabel label = new JLabel(back1);
label.setOpaque(false);
label.repaint();
frame.add(label);
play_btn = new ImageIcon("res/launcher/play_btn.png");
label = new JLabel(play_btn);
label.setOpaque(false);
label.repaint();
frame.add(label);
frame.setTitle("Schach");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2); // JFrame in middle of screen
frame.setVisible(true);
frame.repaint();
修复如下:
JLabel back1 = new JLabel(new ImageIcon("res/launcher/back_1.png"));
back1.setLayout( new BorderLayout() );
JLabel knight = new JLabel(new ImageIcon("res/launcher/play_btn.png"));
back1.add(knight);
frame.add(back1);
我试图在 JFrame
中显示多个具有透明背景的 png,但它们始终具有白色背景。
这是我的代码:
JFrame frame = this;
frame.setUndecorated(true);
back1 = new ImageIcon("res/launcher/back_1.png");
JLabel label = new JLabel(back1);
label.setOpaque(false);
label.repaint();
frame.add(label);
play_btn = new ImageIcon("res/launcher/play_btn.png");
label = new JLabel(play_btn);
label.setOpaque(false);
label.repaint();
frame.add(label);
frame.setTitle("Schach");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2); // JFrame in middle of screen
frame.setVisible(true);
frame.repaint();
修复如下:
JLabel back1 = new JLabel(new ImageIcon("res/launcher/back_1.png"));
back1.setLayout( new BorderLayout() );
JLabel knight = new JLabel(new ImageIcon("res/launcher/play_btn.png"));
back1.add(knight);
frame.add(back1);