栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

GUI:如何正确设置布局

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);    }}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号