【发布时间】:2019-09-23 07:48:18
【问题描述】:
所以我有一个简单的 java GUI,它有两个可编辑的文本字段。
如果我输入第一个,它将弹出一个显示第一个字段的对话框。
##########################################
# Message x #
##########################################
# Your textfields #
# 1: this is an editable field #
# #
##########################################
如果我输入,它会弹出一个显示第二个字段的对话框。
##########################################
# Message x #
##########################################
# Your textfields #
# 2: this is an editable field 2 #
# #
##########################################
但是,是否可以弹出一个同时显示两个字段的对话框?
##########################################
# Message x #
##########################################
# Your textfields #
# 1: this is an editable field #
# 2: this is an editable field 2 #
##########################################
这是我到目前为止所做的:
我的文本框:
class EditTextFrame extends JFrame
{
private JTextField editableField1;
private JTextField editableField2;
public EditTextFrame()
{
setLayout(new FlowLayout());
editableField1 = new JTextField("This is an editable field", 25);
editableField2 = new JTextField("This is an editable field2", 25);
add(editableField1);
add(editableField2);
TextFieldHandler handler = new TextFieldHandler();
editableField1.addActionListener(handler);
editableField2.addActionListener(handler);
}
我的动作事件:
private class TextFieldHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String string1 = "";
String string2 = "";
if (e.getSource() == editableField1)
string1 = String.format("1: %s",
e.getActionCommand());
if (e.getSource() == editableField2)
string2 = String.format("2: %s",
e.getActionCommand());
JOptionPane.showMessageDialog(null, "Your textfields \n" + string1 + string2
);
}
}
我知道我的字符串 concat 是无用的,因为其中一个将是空的,因为 if 语句在满足条件后停止..但我似乎找不到解决方法..
【问题讨论】:
-
"是否可以弹出同时显示两个字段的对话框?" 可以。将两个文本字段放在一个
JPanel中,并使用适当的JLabel组件来识别每个字段,并将面板放在JOptionPane中。一般:如需更好的帮助,请edit添加minimal reproducible example或Short, Self Contained, Correct Example。