布局管理器preferredSize Java
发布时间:2020-05-24 19:07:59 所属栏目:Java 来源:互联网
导读:我还在努力学习布局管理器的工作方式.我用两个JPanel制作了一个Frame. 第一个包含带有boxLayout的textArea. 第二个包含带按钮的流程布局. 我相应地设置了每个面板的preferredSize,打包它们,但得到了意想不到的结果. import java.awt.*;import javax.swing.*;p
|
我还在努力学习布局管理器的工作方式.我用两个JPanel制作了一个Frame.
我相应地设置了每个面板的preferredSize,打包它们,但得到了意想不到的结果. import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
TableBasic frame = new TableBasic();
frame.setDefaultCloSEOperation( EXIT_ON_CLOSE );
frame.setVisible(true);
frame.getContentPane().setLayout(new GridLayout(2,1));
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane,BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200,200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,20));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.getContentPane().add(controlPane,BorderLayout.NORTH);
frame.getContentPane().add(buttonPane,BorderLayout.SOUTH);
frame.setSize(new Dimension(500,500));
frame.pack();
}
}
无论我做什么,如果我使用网格布局,它似乎总是为每个控件分配一半的可用空间.有人告诉我:
buttonpane的高度为20.它的分配远远超过它: 这段代码出了什么问题? 解决方法这是使用GridLayout作为布局管理器的结果.将其更改为BorderLayout:frame.getContentPane().setLayout(new BorderLayout()); 例如,这段代码(我从原版中改变了一点): import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloSEOperation( JFrame.EXIT_ON_CLOSE );
//frame.setVisible(true);
//frame.getContentPane().setLayout(new BorderLayout());
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane,40));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.add(controlPane,BorderLayout.NORTH);
frame.add(buttonPane,BorderLayout.SOUTH);
//frame.setSize(new Dimension(500,500));
frame.pack();
frame.setVisible(true);
}
}
生成此框架: (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
