简体   繁体   English

JFrame中JPanel的大小

[英]Size of JPanel in JFrame

I am trying to add a JPanel into my JFrame with a specific size.我正在尝试将 JPanel 添加到具有特定大小的 JFrame 中。 But whatever size I add to the JPanel, it always fills the whole entire JFrame.但无论我添加到 JPanel 的大小,它总是填满整个 JFrame。 And I also tried to reposition the JButton according to my set position but it also doesn't work.而且我还尝试根据我的设置 position 重新定位 JButton 但它也不起作用。 Any recommendations or explanations anyone?任何建议或解释任何人? Tq:P时间:P

import javax.swing.*;
import java.awt.event.*;
import java.awt.; 
import java.awt.image.;

public class Login {

//Creating a method just for the login page0
static void Login(){
    JFrame LoginFrame = new JFrame();
    JPanel Panel = new JPanel();

    Panel.setBounds(40,80,100,50);
    Panel.setBackground(Color.black);

    JButton Enter = new JButton();
    Enter.setBackground(Color.cyan);
    Enter.setBounds(50,100,80,30);
    Enter.setText("Enter");

    JButton Enter2 = new JButton();
    Enter.setBackground(Color.cyan);
    Enter.setBounds(50,100,80,30);
    Enter.setText("Enter");

    LoginFrame.setSize(420,720);
    LoginFrame.setBackground(Color.white);
    LoginFrame.setTitle("LoginFrame");
    LoginFrame.setVisible(true);
    LoginFrame.setLayout(null);
 }

public static void main(String[]args) {
    Login();
}
}

First of all, variable names should NOT start with an upper case character.首先,变量名不应以大写字符开头。 These are Java conventions you will find in any text book or tutorial.这些是您可以在任何教科书或教程中找到的 Java 约定。 Follow the examples.遵循示例。

I am trying to add a JPanel into my JFrame with a specific size我正在尝试将 JPanel 添加到具有特定大小的 JFrame 中

Why use a random size?为什么要使用随机尺寸? Swing is designed so that each component will determine its own size so it will work on any platform with any Look and Feel. Swing 的设计使每个组件都可以确定自己的尺寸,因此它可以在具有任何外观和感觉的任何平台上工作。

Any recommendations任何建议

Don't use a null layout.不要使用 null 布局。 Don't use setBounds().不要使用 setBounds()。 Swing was designed to be used with layout managers. Swing 旨在与布局管理器一起使用。 Layout managers make coding easier and more maintainable.布局管理器使编码更容易和更易于维护。 Read the Swing tutorial on Layout Managers阅读有关布局管理器的 Swing 教程

Also, you actually need to "add" the buttons to the panel and "add" the panel to the frame.此外,您实际上需要将按钮“添加”到面板并将面板“添加”到框架。 The above tutorial demos show how to do that.上面的教程演示展示了如何做到这一点。

If you are simply trying to change the size of the buttons then use:如果您只是尝试更改按钮的大小,请使用:

button.setMargin( new Insets(20, 20, 20, 20) );

The button will be sized properly based on the text AND the extra space specified.该按钮将根据文本和指定的额外空间适当调整大小。

If you want your panel to have extra space then you can use:如果您希望您的面板有额外的空间,那么您可以使用:

panel.setBorder( new EmptyBorder(20, 20, 20, 20) );

Read the Swing tutorial on How to Use Borders for more information and examples.阅读有关如何使用边框的 Swing 教程以获取更多信息和示例。

or explanations或解释

The JPanel uses a FlowLayout by default. JPanel默认使用FlowLayout So the layout manager is resetting the size/location based on the rules of the layout manager.因此布局管理器正在根据布局管理器的规则重置大小/位置。 If you want a different layout, then use a different layout manager.如果您想要不同的布局,请使用不同的布局管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM