Java GUI不能像我想要的那样......?

时间:2015-03-25 01:39:47

标签: java swing layout joptionpane

问题只是希望GUI看起来像这样:

这是我的代码

import javax.swing.JOptionPane;

class Customer
  {

    String n = JOptionPane.showInputDialog("Enter name");
    String id = JOptionPane.showInputDialog("Enter id");
    String sub = JOptionPane.showInputDialog("Enter Submit");

JOptionPane.showMessageDialog(null, null, null, JOptionPane.PLAIN_MESSAGE)

  }

}

我希望一切看起来都是水平的

Enter Name         [box to fill in name]

Enter id            [box to fill in id]

Submit              [box to enter submit]

1 个答案:

答案 0 :(得分:2)

您将共显示4个选项窗格。如果您想要一个选项窗格中的所有内容,这可能会有所帮助:

JPanel msgPanel = new JPanel();
msgPanel.setLayout(new GridLayout(3 , 2));

msgPanel.add(new JLabel("Enter name");

JTextField nameField = new JTextField();
msgPanel.add(nameField);

//create other fields and labels

JOptionPane.showMessageDialog(null , msgPanel , "" , JOptionPane.PLAIN_MESSAGE);

String name = nameField.getText();
//read other fields