Swing —— JLabel预览示例

原创 2013年12月02日 22:23:49

效果图如下:



代码:

public class JLabelTest extends BaseFrame {
        private JLabel label;
        public JLabelTest() {
                JLabel label = createTestLabel();
                mainWin.add(label);
                JPanel p = createTestPanel();
                mainWin.add(p, BorderLayout.SOUTH);
        }
 
 
        private JLabel createTestLabel() {
                label = new JLabel();
                label.setPreferredSize(new Dimension(600, 300));
                label.setOpaque(true);
                label.setBackground(Color.WHITE);
                return label;
        }
 
 
        private JPanel createTestPanel() {
                JPanel p = new JPanel();
                p.setBorder(BorderFactory.createTitledBorder("测试"));
                // 文本/图片: 显示、调整间距、助记符、禁用与启用时显示的图标
                Box verticalBox = new Box(BoxLayout.Y_AXIS);
                JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
                final JCheckBox showIcon = createShowIconCheckBox();
                final JCheckBox showText = createShowTextCheckBox();
                final JTextField gapTextField = createIconTextGapTextField();
                JLabel gapLabel = new JLabel("iconTextGap:");
                gapLabel.setDisplayedMnemonic('G');
                gapLabel.setLabelFor(gapTextField);
                final JCheckBox disabledIcon = createDisabledIconCheckBox();
                p1.add(showIcon);
                p1.add(showText);
                p1.add(gapLabel);
                p1.add(gapTextField);
                p1.add(disabledIcon);
                verticalBox.add(p1);
                // 文本 水平/垂直 位置
                JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
                final JComboBox horizontalTextPosition = createHorizontalTextPositionComboBox();
                final JComboBox verticalTextPosition = createVerticalTextPositionComboBox();
                p2.add(new JLabel("HorizontalTextPosition"));
                p2.add(horizontalTextPosition);
                p2.add(new JLabel("VerticalTextPosition"));
                p2.add(verticalTextPosition);
                verticalBox.add(p2);
                // 组件 水平/垂直 对齐
                JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
                final JComboBox horizontalAlignment = createHorizontalAlignmentComboBox();
                p3.add(new JLabel("HorizontalAlignment"));
                p3.add(horizontalAlignment);
                final JComboBox verticalAlignment = createVerticalAlignmentComboBox();
                p3.add(new JLabel("VerticalAlignment"));
                p3.add(verticalAlignment);
                verticalBox.add(p3);
                p.add(verticalBox);
                return p;
        }
 
 
        private JCheckBox createDisabledIconCheckBox() {
                label.setDisabledIcon(ImageManger.loadIcon("disabled.png"));
                final JCheckBox checkBox = new JCheckBox("Disabled");
                checkBox.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                JCheckBox checkBox = (JCheckBox) e.getSource();
                                label.setEnabled(!checkBox.isSelected());
                        }
                });
                return checkBox;
        }
 
 
        private JTextField createIconTextGapTextField() {
                // 如果同时设置了图标和文本属性,则此属性定义它们之间的间隔。
                final JTextField gapTextField = new JTextField("4", 10);
                gapTextField.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                int iconTextGap = 4;
                                try {
                                        iconTextGap = Integer.valueOf(gapTextField.getText());
                                } catch (Exception ex) {
                                        ex.printStackTrace();
                                }
                                label.setIconTextGap(iconTextGap);
                        }
                });
                return gapTextField;
        }
 
 
        private JComboBox createVerticalAlignmentComboBox() {
                final JComboBox verticalAlignment = new JComboBox(new Object[]{
                        "SwingConstants.TOP", "SwingConstants.CENTER", "SwingContants.BOTTOM"
                });
                verticalAlignment.setSelectedItem("SwingConstants.CENTER");
                verticalAlignment.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String item = (String) verticalAlignment.getSelectedItem();
                                String neededValue = item.substring(item.indexOf(".") + 1);
                                if(neededValue.equals("TOP")){
                                        label.setVerticalAlignment(SwingUtilities.TOP);
                                }else if(neededValue.equals("CENTER")){
                                        label.setVerticalAlignment(SwingUtilities.CENTER);
                                }else if(neededValue.equals("BOTTOM")){
                                        label.setVerticalAlignment(SwingUtilities.BOTTOM);
                                }
                        }
                });
                return verticalAlignment;
        }
 
 
        private JComboBox createHorizontalAlignmentComboBox() {
                final JComboBox horizontalAlignment = new JComboBox(new Object[]{
                        "SwingConstants.LEFT", "SwingConstants.CENTER", "SwingContants.RIGHT"
                        , "SwingConstants.LEADING", "SwingConstants.TRAILING"
                });
                horizontalAlignment.setSelectedItem("SwingConstants.LEFT");
                horizontalAlignment.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String item = (String) horizontalAlignment.getSelectedItem();
                                String neededValue = item.substring(item.indexOf(".") + 1);
                                int alignment = SwingConstants.LEFT;
                                if(neededValue.equals("LEFT")){
                                        alignment = SwingConstants.LEFT;
                                }else if(neededValue.equals("CENTER")){
                                        alignment = SwingConstants.CENTER;
                                }else if(neededValue.equals("RIGHT")){
                                        alignment = SwingConstants.RIGHT;
                                }else if(neededValue.equals("LEADING")){
                                        alignment = SwingConstants.LEADING;
                                }else if(neededValue.equals("TRAILING")){
                                        alignment = SwingConstants.TRAILING;
                                }
                                label.setHorizontalAlignment(alignment);
                        }
                });
                return horizontalAlignment;
        }
 
 
        private JComboBox createHorizontalTextPositionComboBox() {
                final JComboBox horizontalTextPosition = new JComboBox(new Object[]{
                        "SwingConstants.LEFT", "SwingConstants.CENTER", "SwingContants.RIGHT"
                        , "SwingConstants.LEADING", "SwingConstants.TRAILING"
                });
                horizontalTextPosition.setSelectedItem("SwingConstants.LEFT");
                horizontalTextPosition.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String item = (String) horizontalTextPosition.getSelectedItem();
                                String neededValue = item.substring(item.indexOf(".") + 1);
                                int textPosition = SwingConstants.LEFT;
                                if(neededValue.equals("LEFT")){
                                        textPosition = SwingConstants.LEFT;
                                }else if(neededValue.equals("CENTER")){
                                        textPosition = SwingConstants.CENTER;
                                }else if(neededValue.equals("RIGHT")){
                                        textPosition = SwingConstants.RIGHT;
                                }else if(neededValue.equals("LEADING")){
                                        textPosition = SwingConstants.LEADING;
                                }else if(neededValue.equals("TRAILING")){
                                        textPosition = SwingConstants.TRAILING;
                                }
                                label.setHorizontalTextPosition(textPosition);
                        }
                });
                return horizontalTextPosition;
        }
 
 
        private JComboBox createVerticalTextPositionComboBox() {
                final JComboBox verticalTextPosition = new JComboBox(new Object[]{
                        "SwingConstants.TOP", "SwingConstants.CENTER", "SwingContants.BOTTOM"
                });
                verticalTextPosition.setSelectedItem("SwingConstants.CENTER");
                verticalTextPosition.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String item = (String) verticalTextPosition.getSelectedItem();
                                String neededValue = item.substring(item.indexOf(".") + 1);
                                if(neededValue.equals("TOP")){
                                        label.setVerticalTextPosition(SwingUtilities.TOP);
                                }else if(neededValue.equals("CENTER")){
                                        label.setVerticalTextPosition(SwingUtilities.CENTER);
                                }else if(neededValue.equals("BOTTOM")){
                                        label.setVerticalTextPosition(SwingUtilities.BOTTOM);
                                }
                        }
                });
                return verticalTextPosition;
        }
 
 
        private JCheckBox createShowTextCheckBox() {
                final JCheckBox showText = new JCheckBox("text");
                showText.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String text = null;
                                if(showText.isSelected()){
                                        text = "text";
                                }
                                label.setText(text);
                        }
                });
                showText.doClick();
                return showText;
        }
 
 
        private JCheckBox createShowIconCheckBox() {
                final JCheckBox showIcon = new JCheckBox("icon");
                showIcon.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                Icon icon = null;
                        if(showIcon.isSelected()){
                                icon = ImageManger.loadIcon("clock.png");
                                }
                                label.setIcon(icon);
                        }
                });
                showIcon.doClick();
                return showIcon;
        }
 
        public static void main(String[] args) {
                new JLabelTest().display();
        }
}
 


相关文章推荐

JPanel左对齐

我测试了下,把你的panel1.setAlignmentX(JPanel.LEFT_ALIGNMENT);改为 panel1.setLayout(new FlowLayout(FlowLayout....

JFrame,JPanel,JLabel详解

JFrame是一个顶层的框架类,好比一个窗户的框子。也是一个容器类。这个框子可以嵌入几个玻璃窗。   JPanel是一个容器类,相当于一大玻璃窗。   JLabel等是一些基础...

【swing】JLabel详解以及使用示例

JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对...

Swing开发之JLabel篇

JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对...
  • SJF0115
  • SJF0115
  • 2011年11月19日 19:27
  • 10102

JAVA学习Swing章节标签JLabel中图标的使用

package com.swing; import java.awt.Color; import java.awt.Component; import java.awt.Container;...

JFC/Swing活学活用之JLabel文字魔法

前言   本文将讲述如何借助Java 2D的"魔力"来帮助JFC/Swing的JLabel组件创建更加丰富多彩的应用。   JLabel作为JFC/Swing架构的基础组件之一,应用非常广...
  • ycb1689
  • ycb1689
  • 2012年08月13日 15:23
  • 591

php+js实现图片的上传、裁剪、预览、提交示例

首先用到的语言是php、插件imgareaselect没有太多花哨的样式,index.php代码如下: 1 2 3 4 5 6 7 ...

jQuery 单图单击预览示例

  • 2017年08月03日 14:36
  • 424KB
  • 下载

MSFlexGrid控件打印及预览VB示例

  • 2013年05月16日 01:00
  • 11KB
  • 下载

使用uploadify3上传图片的示例支持图片预览

首先是前端代码。 导入jquery是必须的。省略。 其次是: link href="/js/jquery/plugins/uploadify/uploadify.css" rel="st...
  • yaerfeng
  • yaerfeng
  • 2014年09月11日 10:40
  • 11146
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Swing —— JLabel预览示例
举报原因:
原因补充:

(最多只允许输入30个字)