gpt4 book ai didi

java - JOptionPane.showInputDialog 中的多个输入

转载 作者:IT老高 更新时间:2023-10-28 20:21:43 24 4
gpt4 key购买 nike

有没有办法在 JOptionPane.showInputDialog 中创建多个输入而不是一个输入?

最佳答案

是的。您知道您可以将任何 Object 放入大多数 JOptionPane.showXXX 方法Object 参数中,通常是 Object 恰好是一个 JPanel

在您的情况下,也许您可​​以使用其中包含多个 JTextFieldsJPanel:

import javax.swing.*;

public class JOptionPaneMultiInput {
public static void main(String[] args) {
JTextField xField = new JTextField(5);
JTextField yField = new JTextField(5);

JPanel myPanel = new JPanel();
myPanel.add(new JLabel("x:"));
myPanel.add(xField);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("y:"));
myPanel.add(yField);

int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
System.out.println("x value: " + xField.getText());
System.out.println("y value: " + yField.getText());
}
}
}

关于java - JOptionPane.showInputDialog 中的多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6555040/

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