gpt4 book ai didi

Java:JButton 打开另一个我可以输入的 JFrame

转载 作者:行者123 更新时间:2023-11-30 08:59:30 28 4
gpt4 key购买 nike

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

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

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

最佳答案

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,可以包含一个 JPanel,它具有您能想象到的最复杂的 GUI,包括嵌套使用任何必要布局并包含多个组件的其他 JPanel。例如,以下代码创建 JOptionPane,它显示一个包含多个 JTextField 的 JPanel,所有 JOptionPane 返回后均可提取:

enter image description here

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207887/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com