Java:JButton 打开另一个我可以输入的 JFrame [英] Java: JButton opening another JFrame that I can input into

查看:20
本文介绍了Java:JButton 打开另一个我可以输入的 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从我的 JFrame 中单击一个 JButton,这会打开另一个窗口,在第二个窗口中,我希望能够在该窗口的文本字段中输入一些内容并从该文本字段中获取信息.

I want to be able to click a JButton from my JFrame which opens up another window and in this 2nd window, I want to be able to input something into a text field on that window and get the information from that text field.

JButton myButton = new JButton("Click Here!");
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == myButton) {
// open the new window }

我知道如何定义这个新 JFrame 的组件,但是由于不允许嵌套模块,我将如何为该新窗口编写 actionPerformed 事件?

I know how to define the components of this new JFrame, but how will I write an actionPerformed event for that new window since nesting modules isn't allowed?

推荐答案

有没有办法自定义模态对话框窗口的内容?例如,如果我想要弹出窗口中的 2 个文本字段(因此它可以接受 2 个输入)?如果我想将确定"/取消"按钮更改为具有不同的标签和/或行为不同怎么办?

Is there a way to customize the contents of the modal dialog window? For instance if I wanted 2 text fields in the pop up window (so it can take 2 inputs)? What if I wanted to change the "OK"/"Cancel" buttons to have different labels and/or behave differently?

模态对话框 (JDialog) 或 JOptionPane(如 JFrame)可以容纳具有您可以想象的最复杂 GUI 的 JPanel,包括嵌套其他 JPanel,这些 JPanel 使用任何必要的布局并包含多个组件.例如,以下代码创建了 JOptionPane,该 JOptionPane 显示一个 JPanel,其中包含许多 JTextField,在 JOptionPane 返回后所有这些都可以提取:

A modal dialog (JDialog) or a JOptionPane, like a JFrame, can hold a JPanel that has the most complex GUI that you can imagine including nesting other JPanels that use whatever layouts are necessary and that contain multiple components. For example, the following code creates JOptionPane that displays a JPanel that holds a number of JTextFields, all extractable after the JOptionPane returns:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

public class ComplexDialogPanel extends JPanel {
   public static final String[] LABEL_TEXTS = { "Last Name", "First Name",
         "Address", "City", "State", "Zip" };
   public static final int COLS = 8;
   private Map<String, JTextField> labelFieldMap = new HashMap<>();

   public ComplexDialogPanel() {
      setLayout(new GridBagLayout());
      for (int i = 0; i < LABEL_TEXTS.length; i++) {
         String labelTxt = LABEL_TEXTS[i];
         add(new JLabel(labelTxt), createGbc(0, i));

         JTextField textField = new JTextField(COLS);
         labelFieldMap.put(labelTxt, textField);
         add(textField, createGbc(1, i));
      }

      setBorder(BorderFactory.createTitledBorder("Enter User Information"));
   }

   public String getText(String labelText) {
      JTextField textField = labelFieldMap.get(labelText);
      if (textField != null) {
         return textField.getText();
      } else {
         throw new IllegalArgumentException(labelText);
      }
   }

   public static GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = gbc.weightx;
      if (x == 0) {
         gbc.anchor = GridBagConstraints.LINE_START;
         gbc.fill = GridBagConstraints.BOTH;
         gbc.insets = new Insets(3, 3, 3, 8);
      } else {
         gbc.anchor = GridBagConstraints.LINE_END;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.insets = new Insets(3, 3, 3, 3);
      }
      return gbc;
   }

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

      int optionType = JOptionPane.DEFAULT_OPTION;
      int messageType = JOptionPane.PLAIN_MESSAGE;
      Icon icon = null;
      String[] options = { "Submit", "Cancel" };
      Object initialValue = options[0];
      int reply = JOptionPane.showOptionDialog(null, mainPanel,
            "Get User Information", optionType, messageType, icon, options,
            initialValue);
      if (reply == 0) {
         System.out.println("Selections:");
         for (String labelText : LABEL_TEXTS) {
            System.out.printf("%12s: %s%n", labelText,
                  mainPanel.getText(labelText));
         }

      }
   }

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

这篇关于Java:JButton 打开另一个我可以输入的 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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