为什么我的JButton会显示JFrame的完整大小? [英] Why does my JButton appear the full size of the JFrame?

查看:114
本文介绍了为什么我的JButton会显示JFrame的完整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我有一个问题.我正在尝试使用Eclipse编写一个Java Windows应用程序,其中将有一个主窗口,该窗口将包含诸如仪表板之类的几项内容,并且将具有一些按钮,例如将记录添加到数据库中. ,然后按此按钮将在顶部打开一个新的相关窗口.

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.

我试图开始,我用Java编写了这段代码,由于某种原因,按钮的大小与框架的大小...全屏相同!我该如何解决?

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 ?

谢谢

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);  
}

推荐答案

我试图开始,我用Java编写了这段代码,由于某种原因,按钮的大小与框架的大小...全屏!如何解决?"

"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 ?"

按钮伸展的原因是JFrame具有默认的BorderLayout,该默认值BorderLayout不考虑子组件的首选大小.

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.

此外,BorderLayout也是您的JLabel不显示的原因.默认情况下,添加到BorderLayout的每个组件都未指定位置,例如BorderLayout.SOUTH,将自动放置在BorderLayout.CENTER位置.每个职位可能只有一个组成部分.因此,当您添加JLabel时,它会转到CENTER,但是当您添加JButton时,它也会同时到CENTER,从而踢出JLabel.

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

这篇关于为什么我的JButton会显示JFrame的完整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆