import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class Test extends JFrame {
JPanel jp0 = new JPanel();
JPanel jp = new JPanel();
JPanel jpwel = new JPanel() {
public void paintComponent(Graphics g) {// 重写这个方法
super.paintComponent(g);// 继承超类绘制组件方法
ImageIcon Icon = new ImageIcon("d:\\用到的图片\\欢迎界面.jpg");
Graphics2D gg = (Graphics2D) g;
gg.drawImage(Icon.getImage(), 0, 0, getWidth(), getHeight(), null);// 绘制背景
}
};
JButton quit = new JButton("退卡");
JButton cont = new JButton("继续");
JButton returned = new JButton("返回");
JButton sure = new JButton("确认");
JLabel Cardnum = new JLabel("请输入你的19位卡号:");
JTextField card = new JTextField(); public Test() {
this.setSize(700, 450);
this.setLocation(400, 200);
setBackground("d:\\用到的图片\\背景.jpg");
this.setTitle("建行ATM自动柜员机");
//this.setResizable(false);
this.setVisible(true);
Welcome();
//关闭窗口
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public void Welcome() {// 欢迎界面
jpwel.setLayout(null);
jpwel.add(cont);
cont.setBounds(566, 360, 130, 45);
add(jpwel);
jpwel.setOpaque(false);
jpwel.setSize(this.getWidth(), this.getHeight());
revalidate();
cont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("cont");
remove(jpwel);
Inputcn();
}
});
} public void Inputcn() {// 输入卡号界面
jp.setLayout(null);
jp.setOpaque(false);
jp.add(Cardnum);
Cardnum.setBounds(330, 100, 200, 40);
jp.add(card);
card.setBounds(280, 135, 130, 30);
jp.add(returned);
returned.setBounds(0, 360, 130, 45);
jp.add(sure);
sure.setBounds(500, 300, 130, 45);
add(jp);
revalidate();
returned.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("returned");
remove(jp);
revalidate();
Welcome();
}
});
}

public void setBackground(String s) {
JLabel jpicture = new JLabel(new ImageIcon(s));// g:\\处理后的图片\\首页.jpg
this.getLayeredPane().add(jpicture, new Integer(Integer.MIN_VALUE));
jpicture.setBounds(0, 0, this.getWidth(), this.getHeight());
jp0 = (JPanel) this.getContentPane();
jp0.setOpaque(false);
} public static void main(String[] args) {
new Test();
}
}
界面swing

解决方案 »

  1.   

    楼主如果学会debug就知道问题出在哪了。。
    cont.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.out.println("cont");
    remove(jpwel);
    Inputcn();
    }
    这里你cont添加了一次监听,但是并没有销毁该监听,只是删除了该按钮的父控件而已,该对象引用还存在。
    然后下一次执行的时候,你又执行了一遍该代码,所以等于一个按钮你添加了2次监听,所以出现2次。。
    改成如下这样就可以了:public void Welcome() {// 欢迎界面
    JButton cont = new JButton("继续");//把cont在这里实例化。
    jpwel.setLayout(null);
    jpwel.add(cont);
    cont.setBounds(566, 360, 130, 45);
    add(jpwel);
    jpwel.setOpaque(false);
    jpwel.setSize(this.getWidth(), this.getHeight());
    revalidate();
    cont.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.out.println("cont");
    System.out.println(jpwel);
    remove(jpwel);
    Inputcn();
    revalidate();
    }
    });
    }
    当然,下面的revalidate其实也需要改一下的,要不也是会出问题的。
    其实我是不建议删除界面的,swing中有隐藏机制。
    加一句jp.setVisible(false);这样的就可以了
      

  2.   

    System.out.println(jpwel);这一句应该没用的吧?? 刚才试了下,按照你那样界面还是不能刷新 把revalidate()换成repaint()就可以了,revalidate()这个是只刷新组件不刷界面的吧?望赐教~~
      

  3.   

    你的问题是触发了两次cont事件 这个问题吧?
    我那个代码应该是解决了这个问题的吧?
    至于界面刷新那是另外一个问题了,
    我说过你只要把界面改为显示和不显示就可以了,没必要每次都重绘背景JPanel。。
    刷新的话是revalidate(),但是得注重时机刷新,
    repaint()是重绘,重写一遍。
    输出语句当然没用,一个测试而已。。