Java 每按一次按钮,帧的关闭和重新定位频率就会增加
我正在使用自制的工具栏浏览我的应用程序,该工具栏显示在所有页面上。每次显示新页面时,我都会使用以下代码关闭当前帧并打开新帧:Java 每按一次按钮,帧的关闭和重新定位频率就会增加,java,swing,user-interface,jframe,dispose,Java,Swing,User Interface,Jframe,Dispose,我正在使用自制的工具栏浏览我的应用程序,该工具栏显示在所有页面上。每次显示新页面时,我都会使用以下代码关闭当前帧并打开新帧: java.awt.Window win[] = java.awt.Window.getWindows(); for(int i=0;i<win.length;i++){ win[i].dispose(); } 主类 public static void main(String[] args) { Customers.CustomersGui();
java.awt.Window win[] = java.awt.Window.getWindows();
for(int i=0;i<win.length;i++){
win[i].dispose();
}
主类
public static void main(String[] args) {
Customers.CustomersGui();
}
随着代码的每次迭代,您将向buttonCancel添加一个新的ActionListener,这就是程序行为的原因
另外,根据我的评论,你说 每次显示新页面时,我都会关闭当前帧并打开新帧 更好的设计可能不是交换可能令人讨厌的窗口,而是使用CardLayout交换JPanel视图。请阅读
例如,将这行代码添加到程序中:
if (panelname.matches("Add Customers")) {
container21.setLayout(GLayout);
container21.add(buttonCancel, BorderLayout.PAGE_START);
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
java.awt.Window win[] = java.awt.Window.getWindows();
for (int i = 0; i < win.length; i++) {
win[i].dispose();
}
System.out.println("Coutner " + counter);
Customers.CustomersGui();
}
});
// ***** add this here **********
System.out.println("buttonCancel ActionListener count: "
+ buttonCancel.getListeners(ActionListener.class).length);
}
您必须在某处重新添加侦听器
“每次显示新页面时,我都会关闭当前帧并打开一个新帧。”
--更好的设计可能不是交换可能令人讨厌的窗口,而是使用CardLayout交换JPanel视图。请阅读。@Alex-你为什么要删除所有的问题???有什么问题吗?@shekharsuman是的,因为这个社区的人们的行为。坦率地说,这是欺凌,我不想看到它的任何部分。如果是这样的话,那么为什么只有一种情况下,当按钮和按钮发生相同的情况时,它才会发生,但它没有相同的问题?@jim:因为你为添加客户创建了一个新的JButton,而不是为取消创建了一个新的JButton,因此,同一个按钮会多次添加侦听器。
public static void main(String[] args) {
Customers.CustomersGui();
}
if (panelname.matches("Add Customers")) {
container21.setLayout(GLayout);
container21.add(buttonCancel, BorderLayout.PAGE_START);
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
java.awt.Window win[] = java.awt.Window.getWindows();
for (int i = 0; i < win.length; i++) {
win[i].dispose();
}
System.out.println("Coutner " + counter);
Customers.CustomersGui();
}
});
// ***** add this here **********
System.out.println("buttonCancel ActionListener count: "
+ buttonCancel.getListeners(ActionListener.class).length);
}
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class SwapPanels extends JPanel {
public static final String CUSTOMER = "customer";
public static final String ADD_CUSTOMER = "Add Customer";
protected static final int PREF_W = 800;
protected static final int PREF_H = 600;
public static final String CANCEL = "Cancel";
private CardLayout cardLayout = new CardLayout();
public SwapPanels() {
setLayout(cardLayout);
add(createCustomerPanel(CUSTOMER), CUSTOMER);
add(createAddCustomerPanel(ADD_CUSTOMER), ADD_CUSTOMER);
}
public void showCard(String key) {
cardLayout.show(this, key);
}
public JPanel createAddCustomerPanel(String name) {
JPanel addCustPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
};
addCustPanel.setName(name);
addCustPanel.setBorder(BorderFactory.createTitledBorder(name));
addCustPanel.add(new JButton(new AbstractAction(CANCEL) {
{
int mnemonic = (int)getValue(NAME).toString().charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
if (CANCEL.equals(e.getActionCommand())) {
SwapPanels.this.showCard(CUSTOMER);
}
}
}));
return addCustPanel;
}
private JPanel createCustomerPanel(String name) {
JPanel custPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
};
custPanel.setName(name);
custPanel.setBorder(BorderFactory.createTitledBorder(name));
custPanel.add(new JButton(new AbstractAction(ADD_CUSTOMER) {
{
int mnemonic = (int)getValue(NAME).toString().charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
if (ADD_CUSTOMER.equals(e.getActionCommand())) {
SwapPanels.this.showCard(ADD_CUSTOMER);
}
}
}));
return custPanel;
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SwapPanels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SwapPanels());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}