[英]JOptionPane with arranged multiple inputs
I am currently working on a GUI and I want to display a pop up window identical to the one shown below using JOptionPane. 我当前正在使用GUI,并且我想使用JOptionPane显示一个与以下所示相同的弹出窗口。 I am currently able to display the JTextField and JLabel but not in the same specific location as the one shown in the picture.
目前,我可以显示JTextField和JLabel,但不能在图片中所显示的特定位置显示它。 In addition, I am not able to store the user input into variables.
此外,我无法将用户输入存储到变量中。 Can someone please provided me with hints or some examples of code so I can continue on the right path?
有人可以给我提示或一些代码示例,以便我继续正确的道路吗? This is what I am doing:
这就是我在做什么:
GridBagConstraints layoutConst = null; GridBagConstraints layoutConst = null; // GUI component layout JPanel myPanel = new JPanel();
// 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);
You can use the JOptionPane.showOptionDialog
method. 您可以使用
JOptionPane.showOptionDialog
方法。
@param message the
Object
to display@param 消息要显示的
Object
The message
parameter can be a simple string or a complex object like a JPanel. message
参数可以是简单的字符串,也可以是复杂的对象(例如,JPanel)。
For the layout of your panel, we can use a layout manager called GridBagLayout
. 对于面板的布局,我们可以使用名为
GridBagLayout
的布局管理器。
For more information, check the following guide: How To Use GridBagLayout
有关更多信息,请参见以下指南:
How To Use GridBagLayout
Here's a quick example of everything together: 这是所有内容的简单示例:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.