public class LoginPanel extends JPanel { // 登陆界面
Image backGround1 = Toolkit.getDefaultToolkit().getImage("./src/1615465.jpg");
public LoginPanel() {
super(true);
this.setLayout(new BorderLayout());
.....................
.....................
.....................
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = this.getParent().getSize();
g.drawImage(backGround1, 0, 0, size.width, size.height, null);
}
}
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("登陆");
frame.setDefaultCloseOperation(3);
frame.setSize(640, 480);
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize(); // 获得屏幕尺寸
Dimension frameSize = frame.getSize(); // dialog尺寸
frame.setLocation((displaySize.width - frameSize.width) / 2,
(displaySize.height - frameSize.height) / 2); // 设置位置,屏幕中央
frame.add(new LoginPanel());
frame.setVisible(true);
}
}
------解决方案--------------------
把你那Override方法改一改
- Java code
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.paintComponent(g); Image img = Toolkit.getDefaultToolkit().getImage(backGround1); g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); }