gpt4 book ai didi

java - java中的边框布局

转载 作者:行者123 更新时间:2023-12-02 05:40:13 24 4
gpt4 key购买 nike

我正在关注一本java编程书,但我刚刚发现了一些我没有得到的东西。在下面的代码中,更具体地说是在“MainFrame”类的构造函数中如果我理解正确的话,我们会添加标签并同时设置框架的布局。我认为我们需要首先设置布局的类型,然后向框架添加元素。

package fontframe;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class MainFrame extends JFrame{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;

private JLabel label;
private JCheckBox italicCheckBox;
private JCheckBox boldCheckBox;
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
private JComboBox facenameCombo;
private ActionListener listener;

public MainFrame(){
label = new JLabel("Big Java");
add(label,BorderLayout.CENTER);
.......
}

最佳答案

I thought we need to set out the type of the layout first and than to add elements to the frame.

是的,你是对的,但是JFrame的默认布局是BorderLayout,标签的默认布局是FlowLayout。因此不需要为 JFrame 指定布局,它已经是一个 BorderLayout。

来自documentation :

And the child will be added to the contentPane. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it.

关于java - java中的边框布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24581128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com