[英]Getting the variables from a JOptionPane
I have created a JOptionPane that pulls up a custom JPanel that takes in 2 JTextField's & A JComboBox. 我创建了一个JOptionPane,它拉起了一个自定义的JPanel,它接受了2个JTextField和一个JComboBox。 Upon hitting Save I would like to have the 3 values stored into global variables but have little experience with JOptionPane and making this work I have the following method that instantiates it:
按下Save后,我想将3个值存储到全局变量中,但是对JOptionPane经验不足,因此我可以通过以下方法实例化它:
public void add() {
JOptionPane.showOptionDialog(null,
getPanel(),
"Add A Shipment ",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null,
new String[]{"Add Shipment", "Cancel"},"default");
}
and the method that creates the custom popup 以及创建自定义弹出窗口的方法
@SuppressWarnings("unchecked")
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout (8,8));
//NORTH PANEL
JPanel name = new JPanel();
name.setLayout(new BorderLayout(8,8));
name.add(new JLabel("Title: "), BorderLayout.WEST);
JTextField titleIn = new JTextField();
titleIn.setPreferredSize(new Dimension(150, 20));
name.add(titleIn, BorderLayout.EAST);
//CENTER PANEL
JPanel trackID = new JPanel();
trackID.setLayout(new BorderLayout(8,8));
trackID.add(new JLabel("Tracking #: "), BorderLayout.WEST);
JTextField trackIn = new JTextField();
trackIn.setPreferredSize(new Dimension(150, 20));
trackID.add(trackIn, BorderLayout.EAST);
//BOTTOM PANEL
JPanel ship = new JPanel();
ship.setLayout(new BorderLayout(8,8));
String[] services = { "USPS", "UPS", "FedEx", "DHL" };
@SuppressWarnings("rawtypes")
JComboBox service = new JComboBox(services);
service.setSelectedIndex(0);
ship.add(service);
panel.add(name, BorderLayout.NORTH);
panel.add(trackID, BorderLayout.CENTER);
panel.add(ship, BorderLayout.SOUTH);
return panel;
}
Any easy way of assigning these as variables with the given code? 用给定的代码将它们分配为变量的任何简单方法? Thanks
谢谢
您需要创建一个扩展JPanel的类,将控件(两个JTextField和JComboBox)存储为此类的字段,将该类的实例传递给JOptionPane.showOptionDialog()
并在调用后从这些字段中检索值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.