每次调用actionPerformed()
时,都会根据调用次数执行。
问题:
对于每次调用,执行次数增加一次。
我没有使用任何类型的循环,并且关闭括号放置在正确的位置。
这是我的代码:
public class Opensbt extends SBT implements ActionListener {
static String selected = "";
int i = 0;
int j = 0;
private static final long serialVersionUID = 1581264288151208369L;
JButton Openbtn = new JButton("Open");
//@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Second");
AbstractBorder brdr = new TextBubbleBorder(Color.LIGHT_GRAY, 1, 8, 0);
fileopen = new JInternalFrame("Open", true, true, false, false);
fileopen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fileopen.setSize(200, 200);
fileopen.setBorder(brdr);
fileopen.setBounds(0, 2, 145, 30);
desktop.add(fileopen);
JPanel jp = new JPanel(new GridBagLayout());
jp.setPreferredSize(new Dimension(300, 200));
GridBagConstraints firstCol = new GridBagConstraints();
firstCol.anchor = GridBagConstraints.WEST;
GridBagConstraints lastCol = new GridBagConstraints();
lastCol.gridwidth = GridBagConstraints.REMAINDER;
lastCol.fill = GridBagConstraints.HORIZONTAL;
List lb = new List(2);
lb.setBackground(Color.white);
l_label = new JLabel("<html><font size=3 , Arial color=black><b>"
+ "Open file :</b></font></html>");
jp.add(l_label, firstCol);
jp.add(l_label, firstCol);
lastCol.gridwidth = GridBagConstraints.REMAINDER;
lastCol.fill = GridBagConstraints.VERTICAL;
jp.add(lb);
Openbtn.setMargin(new java.awt.Insets(1, 2, 1, 2));
Openbtn.setBorder(brdr);
controlPanel.add(Openbtn);
Openbtn.addActionListener(new ActionListener() {
System.out.println("Somthing");
}
我不明白为什么会这样。为什么actionPerformed()
会根据拨打的电话或当前的电话号码执行?
答案 0 :(得分:0)
我认为问题出在你的JButton Openbtn
上。
每次调用类的actionPerformed时,都会向同一个JButton添加另一个动作侦听器。这意味着你做的次数越多,“Somthing”写入输出的次数就越多。
您需要在JButton Openbtn = new JButton("Open");
方法的开头再次致电actionPerformed()
。