E
E
Evgeny Petryaev2020-07-17 11:43:02
Java
Evgeny Petryaev, 2020-07-17 11:43:02

Why is the button on the whole frame?

I use this code

JFrame frame = new JFrame();
            frame.setLocation(0, 0);
            frame.setSize(300,300);
            JButton button = new JButton();
            button.setSize(100,50);
            button.setLocation(100, 150);
            button.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        frame.dispose();
                    }
                    });
            frame.add(button);
            frame.show();

and the button for the whole form:
5f1164923c646547543103.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petryaev, 2020-07-17
@Gremlin92

The layer in the form of a panel helped

JFrame frame = new JFrame();
            frame.setLocation(0, 0);
            frame.setSize(300,300);
            JButton button = new JButton("close");
            button.setSize(100,50);
            button.setLocation(100, 150);
            Dimension dim = new Dimension();
            dim.width = 100;
            dim.height = 50;
            button.setMaximumSize(dim);
            button.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        frame.dispose();
                    }
                    });
            JPanel panel = new JPanel();
            panel.setSize(frame.getSize());
            panel.add(button);
            frame.add(panel);
            frame.show();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question