Question

My program is about a supermarket. I have created a JButton called b1 in the delivery() method. I want the JFrame window to close when the user presses the button b1. But unfortunately i do not know how to do that. Please help. Below is the delivery() method of my program:

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);
}
Was it helpful?

Solution 3

simply call f.dispose() so you can close the JFrame

OTHER TIPS

setVisible() should do the trick for ya

    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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top