【问题标题】:How to make a modal JFrame or another JComponent?如何制作模态 JFrame 或另一个 JComponent?
【发布时间】:2012-03-25 22:37:15
【问题描述】:

我有一个主 JFrame 和一个额外的 JFrame 用于输入许多附加参数。所以,我需要我的第二个 jframe 将数据返回到主 jframe(使用 ActionListener 作为“保存”按钮),但我不知道我该怎么做。请告诉我,我希望你能帮助我。谢谢你。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    当你想要模态时,最好使用JDialog而不是JFrame。

    HERE is a great Example showing the Modality of the JDIalog.

    【讨论】:

      【解决方案2】:

      不要将第二个依赖窗口设为 JFrame。请改用模态 JDialog 或 JOptionPane。许多人没有意识到 JOptionPanes 可以简单地通过将保存 GUI 的 JPanel 放入 JOptionPane 中来保存复杂的 GUI。

      例如:

      import java.awt.*;
      import java.awt.event.*;
      import java.util.HashMap;
      import java.util.Map;
      
      import javax.swing.*;
      
      @SuppressWarnings("serial")
      public class ComplexOptionPane extends JPanel {
         private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();
         private JTextArea textArea = new JTextArea(12, 30);
      
         public ComplexOptionPane() {
            textArea.setEditable(false);
            textArea.setFocusable(false);
            textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
            JPanel bottomPanel = new JPanel();
            bottomPanel.add(new JButton(new AbstractAction("Get Player Information") {
      
               @Override
               public void actionPerformed(ActionEvent arg0) {
                  int result = JOptionPane.showConfirmDialog(null, playerEditorPanel,
                        "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.PLAIN_MESSAGE);
                  if (result == JOptionPane.OK_OPTION) {
                     for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle
                           .values()) {
                        textArea.append(String.format("%10s: %s%n",
                              fieldTitle.getTitle(),
                              playerEditorPanel.getFieldText(fieldTitle)));
                     }
                  }
               }
            }));
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            setLayout(new BorderLayout(5, 5));
            add(new JScrollPane(textArea), BorderLayout.CENTER);
            add(bottomPanel, BorderLayout.PAGE_END);
         }
      
         private static void createAndShowGui() {
            ComplexOptionPane mainPanel = new ComplexOptionPane();
      
            JFrame frame = new JFrame("ComplexOptionPane");
            frame.setDefaultCloseOperation(JFrame.EXIT_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();
               }
            });
         }
      }
      
      @SuppressWarnings("serial")
      class PlayerEditorPanel extends JPanel {
         enum FieldTitle {
            NAME("Name"), SPEED("Speed"), STRENGTH("Strength"), HEALTH("Health");
            private String title;
      
            private FieldTitle(String title) {
               this.title = title;
            }
      
            public String getTitle() {
               return title;
            }
         };
      
         private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
         private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
         private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
      
         public PlayerEditorPanel() {
            setLayout(new GridBagLayout());
            setBorder(BorderFactory.createCompoundBorder(
                  BorderFactory.createTitledBorder("Player Editor"),
                  BorderFactory.createEmptyBorder(5, 5, 5, 5)));
            GridBagConstraints gbc;
            for (int i = 0; i < FieldTitle.values().length; i++) {
               FieldTitle fieldTitle = FieldTitle.values()[i];
               gbc = createGbc(0, i);
               add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);
               gbc = createGbc(1, i);
               JTextField textField = new JTextField(10);
               add(textField, gbc);
      
               fieldMap.put(fieldTitle, textField);
            }
         }
      
         private GridBagConstraints createGbc(int x, int y) {
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = x;
            gbc.gridy = y;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
      
            gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
            gbc.fill = (x == 0) ? GridBagConstraints.BOTH
                  : GridBagConstraints.HORIZONTAL;
      
            gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
            gbc.weightx = (x == 0) ? 0.1 : 1.0;
            gbc.weighty = 1.0;
            return gbc;
         }
      
         public String getFieldText(FieldTitle fieldTitle) {
            return fieldMap.get(fieldTitle).getText();
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-01
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        相关资源
        最近更新 更多