先给一段代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class login extends JFrame implements ActionListener {
private JTextField name=new JTextField();
private JPasswordField password=new JPasswordField();
private JButton beSure=new JButton();
private JButton quit=new JButton();
private JButton register=new JButton();
private String na;
private String pass;
private Image icon=new ImageIcon("Image/login.gif").getImage();
public login(){
//this.setIconImage(icon);
name.setBounds(560 ,87,121,34);//尺寸和位置都是固定好的,因为在图片上面PS了一段空白的地方
password.setBounds(560 ,136,121,34);/尺寸和位置都是固定好的,因为在图片上面PS了一段空白的地方
password.setEchoChar('*');
beSure.setBounds(488,200 ,75 ,40 );//确定按钮显示
beSure.setText( "确定");
beSure.setFont(new Font("华文行楷",Font.BOLD,18));
beSure.setContentAreaFilled(false);
beSure.setVisible(true);
quit.setBounds(595,200 ,75 ,40 );//取消按钮
quit.setText( "取消");
quit.setFont(new Font("华文行楷",Font.BOLD,18));
quit.setContentAreaFilled(false);
quit.setVisible(true);
register.setBounds(490,260,180,40);//新用户注册按钮
register.setText( "新用户注册");
register.setFont( new Font("华文行楷",Font.BOLD,25));
register.setContentAreaFilled(false);
add(name);
name.setHorizontalAlignment(JTextField.CENTER);
add(password);
password.setHorizontalAlignment(JTextField.CENTER);
add(beSure);
add(quit);
add(register);
this.setLayout(null);//login属性设置
setSize( 716, 371);//框架设置成图片的大小
setTitle( "test");
setLocationRelativeTo(null);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
beSure.addActionListener( this);
quit.addActionListener(this);
register.addActionListener(this);
}
public static void main(String []args){
login l=new login();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==beSure){
System.out.println(name.getText());
System.out.println(password.getText());
}
else if(e.getSource()==quit){
dispose();
}
else if(e.getSource()==register){
dispose();
}
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(icon, 0, 0, 716,371,this);
}
}
现在的问题是,代码中的三个JButton按钮只有在鼠标移动到上面的时候才会显示,可能的问题是paint方法在构造方法结束后调用,此时以图片为背景才会将JButton覆盖掉,请问怎样才能解决这个问题?
------解决方案--------------------------------------------------------
明白楼主问题所在了
楼主直接重写的是JFrame的paint()方法,
paint()方法是没有二次缓存机制的
画的时候会全部重绘
所以图片会把按钮盖住
应该用一个Panel作为其他组件的载体
重写paintComponent()方法(这个方法画的时候不会把子组件覆盖掉)
改了个程序给你
- Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class login extends JFrame implements ActionListener {private JTextField name=new JTextField();private JPasswordField password=new JPasswordField();private JButton beSure=new JButton();private JButton quit=new JButton();private JButton register=new JButton();private String na;private String pass;private MyPanel pnlMain;private Image icon=new ImageIcon("Image/login.gif").getImage();public login(){ pnlMain = new MyPanel(icon); pnlMain.setLayout(null); name.setBounds(560 ,87,121,34);//尺寸和位置都是固定好的,因为在图片上面PS了一段空白的地方password.setBounds(560 ,136,121,34);//尺寸和位置都是固定好的,因为在图片上面PS了一段空白的地方password.setEchoChar('*');beSure.setBounds(488,200 ,75 ,40 );//确定按钮显示beSure.setText( "确定");beSure.setFont(new Font("华文行楷",Font.BOLD,18));beSure.setContentAreaFilled(false);quit.setBounds(595,200 ,75 ,40 );//取消按钮quit.setText( "取消");quit.setFont(new Font("华文行楷",Font.BOLD,18));quit.setContentAreaFilled(false);register.setBounds(490,260,180,40);//新用户注册按钮register.setText( "新用户注册");register.setFont( new Font("华文行楷",Font.BOLD,25));register.setContentAreaFilled(false);pnlMain.add(name);name.setHorizontalAlignment(JTextField.CENTER);pnlMain.add(password);password.setHorizontalAlignment(JTextField.CENTER);pnlMain.add(beSure);pnlMain.add(quit);pnlMain.add(register);Container container = getContentPane();container.add(pnlMain);setSize( 716, 371);//框架设置成图片的大小setTitle( "test");setLocationRelativeTo(null);setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);beSure.addActionListener( this);quit.addActionListener(this);register.addActionListener(this);}public static void main(String []args){login l=new login();l.setVisible(true);}public void actionPerformed(ActionEvent e){if(e.getSource()==beSure){System.out.println(name.getText());System.out.println(password.getText());}else if(e.getSource()==quit){dispose();}else if(e.getSource()==register){dispose();}}class MyPanel extends JPanel { private Image back; public MyPanel(Image image) { super(); back = image; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(back, 0, 0, 716,371,this); }}}