【问题标题】:JOptionPane with arranged multiple inputs具有排列的多个输入的 JOptionPane
【发布时间】:2015-11-15 23:24:58
【问题描述】:

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

GridBagConstraints layoutConst = 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

【问题讨论】:

  • 只需将它们放在使用 GridBagLayout 的 JPanel 中即可。
  • JOptionPane 的一个很棒的功能是,如果你通过message 参数传递一个Component,它会简单地显示该组件

标签: java user-interface joptionpane


【解决方案1】:

您可以使用JOptionPane.showOptionDialog 方法。

@param 消息要显示的Object

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);

    }

}

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多