我的节目是关于超市的。我在 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);
}