Answer the question
In order to leave comments, you need to log in
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();
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question