Two buttons (with a fixed width/height and fixed position) with JFrame

早过忘川 提交于 2019-12-25 02:16:50

问题


I want to preface that yes, this is a homework question.

We are told to create two buttons that increment their own individual counters. The counter part is done in another class not shown here.

I first create a new JFrame and two buttons with their own text.

JFrame frame = new JFrame();
JButton button1 = new JButton("Click me!");
JButton button2 = new JButton("Click me too!");

I set their bounds.

button1.setBounds(0, 0, 100, 50);
button2.setBounds(0, 50, 100, 50);

Adding these buttons onto the frame.

frame.add(button1);
frame.add(button2);

Attach their individual action listeners (since they each increment their own counter).

ActionListener listener = new ClickListener();
ActionListener listener2 = new ClickListener();
button1.addActionListener(listener);
button2.addActionListener(listener2);

Set the frame size and misc settings.

frame.setSize(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

With that said, this is the output:

As you can see, button1 is behind button2 despite the hardcoded .setBound properties.

I have tried changing different settings for button2.setBounds(..), but it seems nothing I put in actually changes its position or size. Changing the inputs into button1, however, changes its position/size.

What gives?


回答1:


You should avoid use of setBounds(...) and null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.

Instead if you want one JButton on top of the other and that they be equal sides, why not simply use a GridLayout(0, 1)?




回答2:


frame.add(button1);
frame.add(button2);

JFrame has BroderLayout as default and add() methods adds the component in the CENTER by default. i.e you are adding two buttons at the same location. Use overloaded add() method along with location for e.g.:

frame.add(button1,BorderLayout.NORTH);

Or you can use a JPanel. add the buttons on panel first then add the panel on JFrame.

JPanel panel=new JPanel();
panel.add(button1);
panel.add(button2);

frame.add(panel);

You can set the alignment of buttons also

JPanel panel=new JPanel(new FlowLayout(FlowLayout.RIGHT));

or you can define a horizontal/vertical gap between them.

JPanel panel=new JPanel(new FlowLayout(FlowLayout.RIGHT,10,10));

Finally for more info read about Using Layout Managers where you will find the samples.




回答3:


See you have two buttons namely Button1 and Button 2 so if you are using setbound property then use using absolute positioning to place controls over the frame like this way.

JFrame frame = new JFrame();
Container Pane = getContentPane();
Pane.setLayout(null);

JButton button1 = new JButton("Click me!");
JButton button2 = new JButton("Click me too!");

Pane.add(Button1);
Pane.add(Button2);

Insets insets = Pane.getInsets();
Dimension size = Button1.getPreferredSize();
Button1.setBounds(10 + insets.left, 10 + insets.top,size.width, size.height); //use your own constants

size = Button2.getPreferredSize();
Button2.setBounds(75 + insets.left, 5 + insets.top,size.width, size.height + 8);

Try avoid using this as it will only make your code lengthy instead use java built in layout like FlowLayout, BorderLayout,GridBagLayout etc. Find more detail here Layout Types

JFrame frame = new JFrame();
Container Pane = getContentPane();
Pane.setLayout(new FlowLayout());

JButton button1 = new JButton("Click me!");
JButton button2 = new JButton("Click me too!");

Pane.add(Button1);
Pane.add(Button2);



回答4:


The default layout manager for a JFrame is BorderLayout, and a component is by default added to BorderLayout.CENTER if none is specified. Since they are both being added to the center one is covering up the other. You either need to specify the location on the frame for one of them or use a different layout manager.

Also (and this is not related to the question) You can use the same listener for both buttons, you can use an if statement to check where the event came from and process it differently based on that.



来源:https://stackoverflow.com/questions/23038674/two-buttons-with-a-fixed-width-height-and-fixed-position-with-jframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!