Why does my JButton appear the full size of the JFrame?

青春壹個敷衍的年華 提交于 2019-12-13 06:27:16

问题


I am a newbie, I have a question. I am trying to use Eclipse to write a Java windows application, in which I will have a main window, which will contain several things, like a dashboard sort of thing, and it will have buttons, for example to add a record to a database, and this button when pressed, will open a new relevant window on top.

I tried to start, I wrote this code in Java, and for some reason, the button is in the size of the frame...full screen ! How do I fix it ?

Can you suggest me better ideas for a design than what I specified ?

Thank you

public class MainClass {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    JFrame  jfrm = new JFrame("Frame1");
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    jfrm.setSize(screenSize.width, screenSize.height);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel jlab = new JLabel("Hello");
    jfrm.add(jlab);

    JButton button = new JButton("Button");
    button.setSize(new Dimension(50, 50));
    button.setLocation(500, 350);
    jfrm.getContentPane().add(button);

    jfrm.setVisible(true);  
}

回答1:


Add a layout to the JFrame first. For example FlowLayout:

jfrm.setLayout(new FlowLayout());



回答2:


"I tried to start, I wrote this code in Java, and for some reason, the button is in the size of the frame...full screen ! How do I fix it ?"

The reason the button stretches is because JFrame has a default BorderLayout that does not respect the preferred sizes of child components.

The solution is to set the Layout Manager to layout that does respect preferred sizes. The image blow from this example shows the most common Layout Managers and show visually which one respect the preferred size of child components.

Also, the BorderLayout is also the reason your JLabel does not show. By default, every component that is added to a BorderLayout without a position specified e.g. BorderLayout.SOUTH, will automatically be placed in the BorderLayout.CENTER position. Each position may only have one component. So when you add the JLabel it goes to the CENTER, but when you add the JButton, it also goes the CENTER, kicking out the JLabel.

If you've never encountered Layout Managers, this is probably all confusing to you. You should take the time to go over How to Layout Components Within a Container




回答3:


You need to use some form of layoutmanager, you can use this information: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html




回答4:


You must use Layout manager.

Use this code..

 public static void main(String[] args) 
{

JFrame  jfrm = new JFrame("Frame1");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jfrm.setSize(screenSize.width, screenSize.height);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jlab = new JLabel("Hello");
jfrm.add(jlab);

JButton button = new JButton("Button");
button.setSize(new Dimension(50, 50));
button.setLocation(500, 350);
jfrm.add(button);
jfrm.setVisible(true);  
jfrm.setLayout(new FlowLayout());
jfrm.pack();

you forgot to add pack(). so use this code to get Jlabel and JButtion side by side.

Thanks...



来源:https://stackoverflow.com/questions/21577712/why-does-my-jbutton-appear-the-full-size-of-the-jframe

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