java - Java Swing 组件的基本问题?

标签 java swing layout jpanel

我的问题是对于理解这个简单的代码来说可能是非常基本的。我自己编写了这段代码,从各处抓取一些代码来理解。我想实际逐行遵循此代码,了解每一行的含义? 我在代码行上方添加了我的理解作为注释,它可能是错误的,或者其中一些标记为 **** 意味着我只是不知道它的含义。如果你能在这里帮助我,那就太好了。 谢谢

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestingSwingComponents {

    public TestingSwingComponents() {
        //Create a frame which is the window that pops up
        JFrame myframe = new JFrame();
        //*****
        myframe.getContentPane().setLayout(new BorderLayout());
        //set the frame size to be 600 X 600 size
        myframe.setSize(600, 600);

        // create Pane1
        JPanel myPanel = new JPanel();
        //set the Layout component of Panel, as how you would like it to be
        //here it is 2 rows and 15 columns
        myPanel.setLayout(new GridLayout(2, 15));
        //create a button with text in it
        JButton letterButton = new JButton("click Me");
        //add the created button component to the panel
        myPanel.add(letterButton);
        //******
        myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);

        // create another panel
        JPanel panelFormat = new JPanel();
        //create a textfield
        JTextField txtfield = new JTextField();
        //create a label for the textfield
        JLabel label = new JLabel("Guesss");
        //set the layout type for this panel
        panelFormat.setLayout(new BorderLayout());
        //add label to panel
        panelFormat.add(label);
        //add textfield to panel
        panelFormat.add(txtfield);
        //I dont know the difference between the below two
        //BorderLayout.CENTER still does not center the panel in the frame, I dont know why
        myframe.getContentPane().add(panelFormat, BorderLayout.CENTER);
        myframe.add(panelFormat);

        // default settings
        myframe.setTitle("Get buttons");
        myframe.setVisible(true);
        myframe.setLocationRelativeTo(null);
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws ParseException {
        new TestingSwingComponents();
    }
}

最佳答案

myframe.getContentPane().setLayout(new BorderLayout());

要回答这个问题,您需要了解 Swing 窗口的结构。 JFrame(实际上是任何 Swing 窗口)由一系列生成窗口 View 的组件组成。

enter image description here

(图片来自How to use Root Panes)

JRootPane 构成了 View 的基础,其顶部是 JLayeredPane 以及所谓的“玻璃 Pane ”。 JLayeredPane 负责管理 JMenuBar 和“内容 Pane ”。

内容 Pane 是组件在窗口上驻留的位置。

因此,这一行的意思是,“获取框架的内容 Pane 并将其布局设置为使用 BorderLayout

布局 API 本身就是一个完整的问题,您可以阅读 Laying out components within a container对于更深入的描述,但基本上,布局管理器消除了您(很大程度上)关心不同系统所使用的渲染技术差异的需要......

//******
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);

这又回到了布局管理器。因为您可以拥有任意数量的布局管理器,所以 Swing 允许您在添加组件时向布局管理器传递“约束”,从而使布局管理器了解您可能希望如何添加该组件。

如果你仔细看看BorderLayout您将看到它有五个可以添加组件的位置。

enter image description here

该行基本上是说,“请将 myPanel 添加到框架/内容 Pane 内的SOUTH 位置”

评论更新

如果你看一下这个片段...

panelFormat.setLayout(new BorderLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);

它将 panelFormat 的布局管理器设置为 BorderLayoutBorderLayout 在其五个可用位置中的任何一个中只能有一个组件。当您使用 add(Component) 而不向其传递布局约束时,BorderLayout 使用 CENTER 作为默认位置,这意味着您正在尝试添加将两个组件添加到 CENTER 位置,这是不可能的,因此 BorderLayout 仅使用最后添加的组件。

why not borderlayout fix the size of textfield instead of stretching it all window

因为这就是 BorderLayout 的工作原理,不,GridLayout 可能会做一些类似的事情。

您可以尝试FlowLayoutGridBagLayout

根据评论更新

您确实需要花时间阅读链接的(和其他建议的)教程...但基本上,您可以使用 GridBagLayout 就像任何其他布局一样,您创建一个实例并将其应用到容器中...

enter image description here

// create another panel
JPanel panelFormat = new JPanel();
//create a textfield
JTextField txtfield = new JTextField(10);
//create a label for the textfield
JLabel label = new JLabel("Guesss");
//set the layout type for this panel
panelFormat.setLayout(new GridBagLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
//I dont know the difference between the below two
//BorderLayout.CENTER still does not center the panel in the frame, I dont know why
myframe.getContentPane().add(panelFormat, BorderLayout.CENTER);
myframe.add(panelFormat);

关于java - Java Swing 组件的基本问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394262/

相关文章:

html - 根据 flex 子项数确定布局?

node.js - MVC 模式与带有公共(public)布局和部分的 Node.js

java - 我如何做出 if 语句,如果一个是一对,另一个不是,则不幸运,但如果两者都是一对,则幸运?

java - "java.net.SocketTimeoutException: connect timed out"我的客户端无法调用网络服务

java - 暂停图形?

java - JTable 不显示列

java - 我需要在图片的范围内创建一个可点击的图像。然后我需要图片在文件夹中的图片之间切换

java - 接口(interface)作为方法参数

java - 为什么编译器不给出 super.staticMethod() 警告

algorithm - Spring Graph Algorithm w节点大小