如何从按钮关闭的Java摆动窗口返回值? [英] How do you return a value from a java swing window closes from a button?

查看:112
本文介绍了如何从按钮关闭的Java摆动窗口返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我在主代码中的某处有该行代码被称为

Basically, I have somewhere in my main code where this line of code is called

editwindow clubeditwindow = new editwindow(1,"Club Edit");

此行打开一个新的JFrame,它基本上可以编辑来自主类的大量信息.我有2个按钮,分别称为保存"和取消".单击保存"后,我想从文本字段中获取值,然后将其放入新对象中,然后将其返回给主类,然后关闭窗口.单击取消时,我希望它什么也不做,这很简单.

This line opens a new JFrame that can basically edit a bunch of information from the main class. I have 2 buttons called save and cancel. When save is clicked, I want to take the values from the textfields and then put it into a new object and return that to the main class, and close the window. When cancel is clicked, I want it to just not do anything, which is simple enough.

谢谢.

推荐答案

不将窗口显示为JFrame,而是将其显示为模式对话框.然后,在不再可见后,主GUI可以查询对象以获取其持有的任何信息.最简单的方法是使用JOptionPane -如果正确使用,它们就是聪明的小野兽.

Don't display the window as a JFrame but rather display it as a modal dialog. Then after it is no longer visible, the main GUI can query the object for any information that it holds. The simplest way to do this is to use a JOptionPane -- they are clever little beasts if used correctly.

这是我的意思的一个例子. JOptionPane包含使用GridBagLayout显示信息的JPane.

Here's an example of what I mean. The JOptionPane holds JPane that displays information using a GridBagLayout.

ComplexOptionPane.java:显示主JFrame.

ComplexOptionPane.java: displays the main 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,其中包含显示在JOptionPane中的JPanel

PlayerEditorPanel.java which holds the JPanel that is displayed in a 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();
   }

}

这篇关于如何从按钮关闭的Java摆动窗口返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆