gpt4 book ai didi

java - 具有排列的多个输入的 JOptionPane

转载 作者:行者123 更新时间:2023-12-01 18:36:51 26 4
gpt4 key购买 nike

我目前正在开发 GUI,我想使用 JOptionPane 显示一个与下面所示的窗口相同的弹出窗口。我目前可以显示 JTextField 和 JLabel,但不能显示在与图片所示相同的特定位置。此外,我无法将用户输入存储到变量中。有人可以向我提供提示或一些代码示例,以便我可以继续走正确的道路吗?这就是我正在做的事情:

GridBagConstraintslayoutConst = null;//GUI组件布局 JPanel myPanel = new JPanel();

    JLabel sNameLabel = null;               // Label for hourly salary
JLabel sDepLabel = null; // Label for yearly salary

JTextField sNameField = null; // Displays hourly salary
JTextField sDepField = null; // Displays yearly salary

sNameLabel = new JLabel("Student Name:");
sDepLabel = new JLabel("Student Department:");

sNameField = new JTextField(15);
sNameField.setEditable(true);
sDepField = new JTextField(15);
sDepField.setEditable(true);


layoutConst = new GridBagConstraints();
layoutConst.gridx = 0;
layoutConst.gridy = 0;
layoutConst.insets = new Insets(10, 10, 10, 10);
myPanel.add(sNameLabel, layoutConst);

layoutConst = new GridBagConstraints();
layoutConst.gridx = 0;
layoutConst.gridy = 1;
layoutConst.insets = new Insets(10, 10, 10, 10);
myPanel.add(sNameField, layoutConst);

layoutConst = new GridBagConstraints();
layoutConst.gridx = 0;
layoutConst.gridy = 0;
layoutConst.insets = new Insets(10, 10, 10, 10);
myPanel.add(sDepLabel, layoutConst);

layoutConst = new GridBagConstraints();
layoutConst.gridx = 1;
layoutConst.gridy = 1;
layoutConst.insets = new Insets(10, 10, 10, 10);
myPanel.add(sDepField, layoutConst);

JOptionPane.showInputDialog(null, myPanel, "Add Course", JOptionPane.OK_CANCEL_OPTION);

What I Want pic is in here

最佳答案

您可以使用JOptionPane.showOptionDialog方法。

@param message the Object to display

message 参数可以是简单字符串,也可以是 JPanel 等复杂对象。

对于面板的布局,我们可以使用名为 GridBagLayout 的布局管理器。

有关更多信息,请查看以下指南:How To Use GridBagLayout

这是所有内容的一个简单示例:

public static void main(String[] args) {

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

GridBagConstraints constraints = new GridBagConstraints();

constraints.insets = new Insets(8, 8, 8, 8);

JLabel label;

label = new JLabel("Student Name");
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(label, constraints);

JTextField studentNameField = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 0;
panel.add(studentNameField, constraints);

label = new JLabel("Departament");
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(label, constraints);

JTextField departamentField = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(departamentField, constraints);

label = new JLabel("Course");
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(label, constraints);

JTextField courseField = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(courseField, constraints);

Object[] options = {"OK", "CANCEL"};

int result = JOptionPane.showOptionDialog(null, panel, null, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

if (result == 0) {

String studentName = studentNameField.getText();

String departament = departamentField.getText();

String course = courseField.getText();

System.out.println(studentName);

System.out.println(departament);

System.out.println(course);

}

}

关于java - 具有排列的多个输入的 JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60017856/

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