sid*_*u99 1 java swing billing multiple-instances jbutton
我的节目是关于一家超市.我在delivery()方法中创建了一个名为b1的JButton.我希望当用户按下按钮b1时关闭JFrame窗口.但不幸的是我不知道该怎么做.请帮忙.以下是我的程序的delivery()方法:
public static void delivery()
{
JFrame f = new JFrame("Name");
f.setVisible(true);
f.setSize(600,200);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocation(700,450);
JPanel p = new JPanel();
final JLabel l = new JLabel("Enter your name: ");
final JTextField jt = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input = jt.getText();
}
});
p.add(b1);
p.add(l);
p.add(jt);
f.add(p);
String b = JOptionPane.showInputDialog(null, "Please enter your address in one single line:");
JOptionPane.showMessageDialog(null, "The ordered stuff will be delivered to " +input+ " who lives in: " +b , "Delivery" , JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Thank you for shopping at Paradise 24/7. Hope to see you again." , "Shopping Done!" , JOptionPane.PLAIN_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
如果需要,可以使用setModal,ModalityTypes来防止Mouse和Keyboard外部事件JDialog
只创建一个JDialog局部变量,setDefaultCloseOperations(HIDE_ON_CLOSE),然后你将使用setVisible(true/false)整个应用程序生命周期