简体   繁体   English

单击按钮时关闭JFrame窗格

[英]Close JFrame pane when button is clicked

I'm trying to use close the pane from an airplane seat system, so each passenger chooses only 1 seat. 我正在尝试使用飞机座位系统的关闭窗格,因此每个乘客只能选择1个座位。 I researched and know I need a line of code JFrame.dispose(); 我研究了一下,知道我需要一行代码JFrame.dispose();。 But I don't know where to put it and what else to put it. 但是我不知道该放在哪里,还有什么要放置。 Thoughts? 有什么想法吗? (Other than that I'm a complete noob XD) (除此之外,我是一个完整的菜鸟XD)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class APlaneJToggle {

private int rows = 15;
private int columns = 6;
private Icon res = (UIManager.getIcon("OptionPane.errorIcon"));

public APlaneJToggle() {
    JPanel panel = new JPanel(new GridLayout(rows, columns));
    for (int row = 0; row < rows; row++) {
        String []rowChar = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        for (int column = 1; column < columns+1; column++) {

            final JToggleButton button = new JToggleButton(rowChar[row] + column);
            button.addActionListener(new ActionListener(){

                    @Override
                    public void actionPerformed(ActionEvent actionEvent) {
                        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                        boolean selected = abstractButton.getModel().isSelected();
                        if (selected) {
                            button.setIcon(res);
                        } else {
                            button.setIcon(null);
                        }
                    }
                });
            panel.add(button);
        }
    }
    final JFrame frame = new JFrame("Flight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setLocation(300, 100);
    frame.resize(750,450);
    frame.isDefaultLookAndFeelDecorated();
    frame.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                APlaneJToggle aPlaneJToggle = new APlaneJToggle();
            }
        });
}
}

Suggestions: 意见建议:

  1. I'm guessing that you want to allow the user to only select one button. 我猜想您要允许用户仅选择一个按钮。 If so, then perhaps a better solution: use a ButtonGroup and add all the JToggleButtons to the single ButtonGroup. 如果是这样,那么也许是一个更好的解决方案:使用ButtonGroup并将所有JToggleButtons添加到单个ButtonGroup中。 This way the user still can select another seat, but the previous seat will become unselected. 这样,用户仍然可以选择另一个座位,但是前一个座位将变为未选择状态。
  2. Use an ItemListener not an ActionListener if you go this route. 如果使用此路由,请使用ItemListener而不是ActionListener。
  3. The calling code can easily get the selected button from the ButtonGroup object. 调用代码可以轻松地从ButtonGroup对象中获取选定的按钮。
  4. If this window will be displayed from another top level JFrame window, it is better off as a JDialog and not a JFrame. 如果将从另一个顶级JFrame窗口显示此窗口,则最好将其作为JDialog而不是 JFrame。

For example (better late than never) 例如(迟到总比不到好)

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class APlaneJToggleTest extends JPanel {
   private DefaultListModel<String> seatListModel = new DefaultListModel<>();
   private JList<String> selectedSeatList = new JList<>(seatListModel);
   private JButton getSeatSelectionBtn = new JButton(new GetSelectionAction("Get Selected Button"));
   private JDialog getSeatDialog;
   private APlaneJToggle aPlaneJToggle = new APlaneJToggle();

   public APlaneJToggleTest() {
      selectedSeatList.setVisibleRowCount(8);
      String prototype = String.format("%20s", " ");
      selectedSeatList.setPrototypeCellValue(prototype);

      add(getSeatSelectionBtn);
      add(new JScrollPane(selectedSeatList));
   }

   private class GetSelectionAction extends AbstractAction {
      public GetSelectionAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (getSeatDialog == null) {
            Window win = SwingUtilities.getWindowAncestor(APlaneJToggleTest.this);
            getSeatDialog = new JDialog(win, "Choose Seat", ModalityType.APPLICATION_MODAL);
            getSeatDialog.add(aPlaneJToggle.getMainPanel());
            getSeatDialog.setResizable(false);
            getSeatDialog.pack();
         }
         getSeatDialog.setVisible(true);
         ButtonModel model = aPlaneJToggle.getSelectedModel();
         if (model != null) {
            String actionCommand = model.getActionCommand();
            seatListModel.addElement(actionCommand);
            aPlaneJToggle.clear();
         }
      }
   }

   private static void createAndShowGui() {
      APlaneJToggleTest mainPanel = new APlaneJToggleTest();

      JFrame frame = new JFrame("APlaneJToggleTest");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class APlaneJToggle {
   private int rows = 15;
   private int columns = 6;
   private Icon res = (UIManager.getIcon("OptionPane.errorIcon"));
   private Icon blankIcon;
   private ButtonGroup buttonGroup = new ButtonGroup();
   private JPanel panel = new JPanel(new BorderLayout());

   public APlaneJToggle() {
      int bi_width = res.getIconWidth();
      int bi_height = res.getIconHeight();
      BufferedImage blankImg = new BufferedImage(bi_width, bi_height,
            BufferedImage.TYPE_INT_ARGB);
      blankIcon = new ImageIcon(blankImg); 
      JPanel buttonPanel = new JPanel(new GridLayout(rows, columns));

      for (int row = 0; row < rows; row++) {
         String[] rowChar = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
               "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
               "X", "Y", "Z" };
         for (int column = 1; column < columns + 1; column++) {

            String text = rowChar[row] + column;
            final JToggleButton button = new JToggleButton(text, blankIcon);
            button.setActionCommand(text);
            buttonGroup.add(button);
            button.addItemListener(new ItemListener() {

               @Override
               public void itemStateChanged(ItemEvent e) {
                  Icon icon = e.getStateChange() == ItemEvent.SELECTED ? res
                        : blankIcon;
                  ((AbstractButton) e.getSource()).setIcon(icon);
               }
            });
            buttonPanel.add(button);
         }
      }
      JButton selectionButton = new JButton(new DisposeAction("Make Selection"));
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(selectionButton);

      panel.add(buttonPanel, BorderLayout.CENTER);
      panel.add(bottomPanel, BorderLayout.PAGE_END);
   }

   public void clear() {
      ButtonModel model = buttonGroup.getSelection();
      if (model != null) {
         model.setEnabled(false);
      }
      buttonGroup.clearSelection();
   }

   public ButtonModel getSelectedModel() {
      return buttonGroup.getSelection();
   }

   public JPanel getMainPanel() {
      return panel;
   }

   private class DisposeAction extends AbstractAction {
      public DisposeAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         Component comp = (Component) e.getSource();
         Window win = SwingUtilities.getWindowAncestor(comp);
         win.dispose();
      }
   }

}

You could use SwingUtilities.getWindowAncestor to return the Window which a component resides in, this way you could dispose on the returned result. 您可以使用SwingUtilities.getWindowAncestor返回组件所在的Window ,这样就可以dispose返回的结果。

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int rows = 15;
        private int columns = 6;

        public TestPane() {
            setLayout(new GridLayout(rows, columns));
            for (int row = 0; row < rows; row++) {
                String[] rowChar = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
                for (int column = 1; column < columns + 1; column++) {

                    final JToggleButton button = new JToggleButton(rowChar[row] + column);
                    button.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                            boolean selected = abstractButton.getModel().isSelected();
                            if (selected) {
                                button.setText("X");
                            } else {
                                button.setText("");
                            }
                            SwingUtilities.getWindowAncestor(button).dispose();
                        }
                    });
                    add(button);
                }
            }
        }

    }

}

You might consider using a JDialog instead of a JFrame as it will allow you to control the program flow better. 您可能会考虑使用JDialog而不是JFrame因为它将使您更好地控制程序流程。 Have a look at How to Make Dialogs for more details 看看如何制作对话框了解更多详细信息

如果要关闭应用程序,则也可以使用System类。

System.exit(0);

In Netbeans :- Netbeans中

  1. Set the defaultCloseOperation to DISPOSE from the properties of the JFrame. 从JFrame的属性中将defaultCloseOperation设置为DISPOSE
  2. then type this.dispose() inside the ActionPerformed() of the jButton. 然后在jButtonActionPerformed()中键入this.dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM