从Java Swing窗口关闭按钮返回值的方法是什么?

5
基本上,在我的主要代码中,有一行代码被调用。
editwindow clubeditwindow = new editwindow(1,"Club Edit");

这行代码会打开一个新的JFrame,可以基本上从主类中编辑大量信息。我有两个按钮,分别称为“保存”和“取消”。 当单击“保存”时,我想要从文本字段中获取值,然后将其放入一个新对象中并返回到主类,并关闭该窗口。当单击“取消”时,我希望它什么都不做,这很简单。

提前感谢。


请学习Java命名规范并遵循它们。 - kleopatra
1个回答

12
不要将窗口显示为JFrame,而是将其显示为模态对话框。在它不再可见后,主GUI可以查询该对象以获取任何信息。最简单的方法是使用JOptionPane——如果使用正确,它们是聪明的小家伙。
这里有一个我所说的例子。JOptionPane包含JPane,使用GridBagLayout显示信息。
ComplexOptionPane.java: 显示主JFrame。
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class ComplexOptionPane extends JPanel {
   private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();
   private JTextArea textArea = new JTextArea(20, 40);

   public ComplexOptionPane() {
      add(new JScrollPane(textArea));
      add(new JButton(new AbstractAction("Get Player Information") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            int result = JOptionPane.showConfirmDialog(null, playerEditorPanel,
                  "Edit Player", 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)));
               }
            }
         }
      }));
   }

   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();
         }
      });
   }
}

PlayerEditorPanel.java持有一个JPanel,在JOptionPane中显示。

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

@SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
   enum FieldTitle {
      NAME("Name"), SPEED("Speed"), STRENGTH("Strength");
      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();
   }

}

@gammaraptor:请参考上面的示例代码作为“例如”。 - Hovercraft Full Of Eels
@gammaraptor:你问如何向对话框添加多个文本字段。答案是:如果您可以向JPanel添加多个JTextFields,那么您可以在JOptionPane(如上所示)或JDialog中显示该JPanel。 - Hovercraft Full Of Eels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接