是否可以在 Swing 中创建一个 JDialog,当单击 OK 按钮时会返回一个对象?
例如,对话框中的文本字段包含构成地址的组件(街道名称、国家/地区等)
单击确定按钮时,将返回一个地址对象。
为什么我认为这可能是因为这个。但我想要的是我上面提到的东西。
关于如何完成这项工作的任何指示都会非常有帮助。
就像前面的人建议的那样:JOptionPane 可以帮助你做你想做的事。但是,如果您决定从头开始实现这一点,那么这里有一个 SSCCE,它可以完全满足您的需求(嗯,它返回一个字符串,但可以轻松修改它以满足您的需求):
import java.awt.Font;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class MyDialog
{
private JFrame parent;
private JDialog dialog;
private String information;
MyDialog (JFrame parent)
{
this.parent = parent;
}
private JPanel createEditBox ()
{
JPanel panel = new JPanel ();
JLabel dialogtitlelabel = new JLabel ("Enter Info");
panel.add (dialogtitlelabel);
dialogtitlelabel.setFont (new Font ("Arial", Font.BOLD, 20));
final JTextArea informationtxt = new JTextArea ();
informationtxt.setEditable (true);
informationtxt.setLineWrap (true);
informationtxt.setWrapStyleWord (true);
JScrollPane jsp = new JScrollPane (informationtxt);
jsp.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize (new Dimension (180, 120));
panel.add (jsp);
JButton btnok = new JButton ("OK");
panel.add (btnok);
JButton btncancel = new JButton ("Cancel");
panel.add (btncancel);
btnok.addActionListener (new ActionListener ()
{
@Override public void actionPerformed (ActionEvent a)
{
if (informationtxt.getText () == null || informationtxt.getText ().isEmpty ())
{
return;
}
information = informationtxt.getText ();
dialog.dispose ();
}
});
btncancel.addActionListener (new ActionListener ()
{
@Override public void actionPerformed (ActionEvent a)
{
dialog.dispose ();
}
});
return panel;
}
void display ()
{
final int DWIDTH = 200;
final int DHEIGHT = 240;
dialog = new JDialog (parent, "Information", true);
dialog.setSize (DWIDTH, DHEIGHT);
dialog.setResizable (false);
dialog.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);
dialog.setContentPane (createEditBox ());
dialog.setLocationRelativeTo (parent);
dialog.setVisible (true);
}
String getInformation ()
{
return information;
}
}
public class ReturningDialogTest
{
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
final JFrame frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
final JPanel panel = new JPanel ();
JButton btn = new JButton ("show dialog");
panel.add (btn);
final JLabel lab = new JLabel ("");
panel.add (lab);
frame.add (panel);
btn.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
MyDialog diag = new MyDialog (frame);
diag.display ();
String info = diag.getInformation ();
lab.setText (info);
frame.pack ();
}
});
frame.setLocationRelativeTo (null);
frame.pack ();
frame.setVisible (true);
}
});
}
}
当您按 OK 时,您在该文本区域中输入的内容会显示在主窗口中,只是为了证明它有效:)。
将保存地址的 GUI 组件放入面板中。将面板提供给对话框。关闭对话框后,从面板上的组件中读取值以构造Address
对象。
是否可以在 Swing 中创建一个 JDialog,当单击 OK 按钮时会返回一个对象?
单击确定按钮时,将返回一个地址对象。