简体   繁体   English

Java Swing JPanel BorderLayout无法正常工作

[英]Java Swing JPanel BorderLayout doesn't work as I expected

First of all I have to say that I started programming in Java only 3 days ago. 首先,我必须说,三天前我才开始用Java编程。

So please be patient and try to give me a detailed explanation. 因此,请耐心等待,并尝试给我详细的解释。

So I am trying to create this demo GUI using Swing. 因此,我尝试使用Swing创建此演示GUI。 I just want to initially test the layout of different components before coding the complete design. 我只想在编码整个设计之前先测试不同组件的布局。 So I wrote this small code to add 3 buttons to a JPanel. 因此,我编写了此小代码,为JPanel添加了3个按钮。

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

public class demoGUI_v1{
    JButton button1,button2,button3;
    JFrame frame;
    JPanel panel,panel2;    
    public static void main(String[] args){
        demoGUI_v1 gui = new demoGUI_v1();
        gui.framework();
    }

    public void framework(){
        frame = new JFrame();
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        panel2 = new JPanel();
        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        panel2.add(BorderLayout.CENTER,panel);
        frame.getContentPane().add(panel, BorderLayout.WEST);
        frame.setVisible(true);
    }
}

在此处输入图片说明

Add some vertical glue, to center the components vertically: 添加一些垂直胶水,以使组件垂直居中:

    panel.add(Box.createVerticalGlue());
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(Box.createVerticalGlue());

Not directly related to your question but: 与您的问题没有直接关系,但:

frame.getContentPane().add(BorderLayout.WEST,panel);

Don't use that format of the add(...) method. 不要使用add(...)方法的格式。 As the API says: 正如API所说:

This method is obsolete as of 1.1. 从1.1开始,此方法已过时。 Please use the method add(Component, Object) instead. 请改用add(Component,Object)方法。

Also, since JDK4 you don't need to get the content pane. 另外,由于JDK4,您不需要获取内容窗格。 So you can use: 因此,您可以使用:

frame.add(panel, BorderLayout.LINE_START); // preferred over "WEST"

Class names should start with an upper case character and should not use "_" in the class name. 类名应以大写字母开头,并且不应在类名中使用“ _”。 Again, just look at the API to see the class names used. 同样,只需查看API即可查看所使用的类名。 Don't make up your own conventions. 不要编造自己的约定。

I would suggest the tutorial you got your original code from is very old. 我建议您从中获取原始代码的教程非常古老。 I would suggest you start by using the Swing tutorial for examples and explanations. 我建议您首先使用Swing教程获取示例和解释。 The tutorial covers all the layout managers and should help explain why Eric's suggestion works. 本教程涵盖了所有布局管理器,应有助于解释Eric的建议为何有效。

Here you have a simpler way to achieve what you need: 在这里,您可以采用一种更简单的方法来实现所需的功能:

public void framework(){
    frame = new JFrame();
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();
    panel2 = new JPanel();
    button1 = new JButton("Button1");
    button2 = new JButton("Button2");
    button3 = new JButton("Button3");
    panel.add(Box.createVerticalGlue());
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(Box.createVerticalGlue());
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(panel, BorderLayout.LINE_START);
    frame.setVisible(true);
}

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

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