1

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

3 回答 3

7
于 2013-06-30T14:48:13.237 回答
2

setVisible() 应该为你解决问题

    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();
            f.setVisible(false);
        }
    });
    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);
}
于 2013-06-30T20:36:18.037 回答
1

只需调用f.dispose(),以便您可以关闭 JFrame

于 2013-06-30T14:45:12.510 回答