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

将JLabel放在JPanel上居中

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

将JLabel放在JPanel上居中

以下是使组件居中的四种方法:

import java.awt.*;import javax.swing.*;import javax.swing.border.*;class CenterComponent {    public static JLabel getLabel(String text) {        return getLabel(text, SwingConstants.LEFT);    }    public static JLabel getLabel(String text, int alignment) {        JLabel l = new JLabel(text, alignment);        l.setBorder(new LineBorder(Color.RED, 2));        return l;    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() { public void run() {     JPanel p = new JPanel(new GridLayout(2,2,4,4));     p.setBackground(Color.black);     p.setBorder(new EmptyBorder(4,4,4,4));     JPanel border = new JPanel(new BorderLayout());     border.add(getLabel(         "Border", SwingConstants.CENTER), BorderLayout.CENTER);     p.add(border);     JPanel gridbag = new JPanel(new GridBagLayout());     gridbag.add(getLabel("GridBag"));     p.add(gridbag);     JPanel grid = new JPanel(new GridLayout());     grid.add(getLabel("Grid", SwingConstants.CENTER));     p.add(grid);     // from @0verbose     JPanel box = new JPanel();     box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS ));     box.add(Box.createHorizontalGlue());     box.add(getLabel("Box"));     box.add(Box.createHorizontalGlue());     p.add(box);     Jframe f = new Jframe("Streeeetch me..");     f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     f.setContentPane(p);     f.pack();     f.setLocationByPlatform(true);     f.setVisible(true); }        });    }}


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

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

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