GUI:如何正确设置布局

GUI:如何正确设置布局,第1张

GUI:如何正确设置布局

GridBagLayout

该答案中所建议的那样,可以为左部分提供所需的全部控制。如果您愿意为简单起见而牺牲一些控制权,则可以
FlowLayout
在a里面放两个面板
GridLayout

import java.awt.BorderLayout;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;class Grid extends Jframe{    private JTextField t1, t2;    private JCheckBox c1, c2;    private JButton b1, b2, b3;    private JPanel topPanel, bottomPanel;    private JPanel eastPanel, centerPanel;    private JLabel l1, l2;    public Grid()    {         c1 = new JCheckBox("Snap to Grid");         l1 = new JLabel("X:");         t1 = new JTextField("8",3);         topPanel = new JPanel();//uses flow layout by default         topPanel.add(c1); topPanel.add(l1); topPanel.add(t1);         c2 = new JCheckBox("Show Grid");         l2 = new JLabel("Y:");         t2 = new JTextField("8",3);         bottomPanel = new JPanel();         bottomPanel.add(c2);  bottomPanel.add(l2); bottomPanel.add(t2);         centerPanel = new JPanel(new GridLayout(2,1));         centerPanel.add(topPanel);         centerPanel.add(bottomPanel);         add(centerPanel,BorderLayout.CENTER);         b1 = new JButton("Ok");         b2 = new JButton("Cancel");         b3 = new JButton("Help");         eastPanel = new JPanel(new GridLayout(3,1));         eastPanel.add(b1);         eastPanel.add(b2);         eastPanel.add(b3);         add(eastPanel, BorderLayout.EAST);    }    public static void main(String[] args) {        Grid app = new Grid();        app.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        app.setSize(300,150);        app.setVisible(true);    }}



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5488510.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存