日期:2014-05-20  浏览次数:20837 次

新手编了这一段小代码,但是为什么在输入卡号界面按了返回之后在欢迎界面按继续,就会输出两次cont也就是触发了两次cont事件呢??求解决方法·····
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

------解决方案--------------------
楼主如果学会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);这样的就可以了。。。