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

边框和网格布局

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

边框和网格布局

一些其他建议:

不要使用空格进行布局;使用对齐方式。

让布局通过使用组件的首选大小来完成工作。

for-each尽可能使用该构造。

从EDT开始。

import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class FurnitureTest {    private static final class FurniturePanel        extends JPanel implements ActionListener {        private static final int N = 3;        private static final Icon icon = UIManager.getIcon("OptionPane.informationIcon");        private JPanel westPanel = new JPanel();        private JPanel centerPanel = new JPanel();        private JButton[] commandButtons = { new JButton("Add Chair"), new JButton("Add Table"), new JButton("Add Desk"), new JButton("Clear All"), new JButton("Total Price"), new JButton("Save"), new JButton("Load"), new JButton("Summary")        };        FurniturePanel() { this.setLayout(new GridLayout()); westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS)); for (JButton b : commandButtons) {     b.setAlignmentX(JButton.CENTER_ALIGNMENT);     westPanel.add(b);     b.addActionListener(this); } this.add(westPanel, BorderLayout.WEST); centerPanel.setLayout(new GridLayout(N, N, N, N)); for (int i = 0; i < N * N; i++) {     centerPanel.add(new JLabel(icon)); } this.add(centerPanel, BorderLayout.CENTER);        }        @Override        public void actionPerformed(ActionEvent e) { System.out.println(e);        }    }    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     Jframe application = new Jframe();     FurniturePanel panel = new FurniturePanel();     application.add(panel);     application.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     application.pack();     application.setLocationByPlatform(true);     application.setVisible(true); }        });    }}


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

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

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