关闭

Swing —— JLabel预览示例

标签: SwingJLabel
960人阅读 评论(0) 收藏 举报
分类:

效果图如下:



代码:

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:118547次
    • 积分:2696
    • 等级:
    • 排名:第13352名
    • 原创:61篇
    • 转载:1篇
    • 译文:1篇
    • 评论:16条
    文章分类
    最新评论