问题
I am currently working on a GUI and I want to display a pop up window identical to the one shown below using 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. 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; // GUI component layout 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
回答1:
You can use the JOptionPane.showOptionDialog method.
@param message the
Object
to display
The message
parameter can be a simple string or a complex object like a JPanel.
For the layout of your panel, we can use a layout manager called GridBagLayout.
For more information, check the following guide: 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);
}
}
来源:https://stackoverflow.com/questions/33726345/joptionpane-with-arranged-multiple-inputs