Swing —— JLabel预览示例
来源:互联网 发布:ax是什么软件 编辑:程序博客网 时间:2024/04/30 03:13
效果图如下:
代码:
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(); }}
- Swing —— JLabel预览示例
- Swing——JLabel美化
- swing JLabel详解以及使用示例
- 【swing】JLabel详解以及使用示例
- swing JLabel详解以及使用示例
- 【swing】JLabel详解以及使用示例
- Swing笔记(一)——JLabel和JButton组件
- JLabel示例
- swing 组件——在JLabel中添加图片的两种方式
- Swing开发之JLabel篇
- Swing开发之JLabel篇
- java swing 创建 jlabel 标签
- Swing —— JButton几个小示例
- JFC/Swing活学活用JLabel文字魔法
- JFC/Swing活学活用JLabel文字魔法
- JFC/Swing活学活用之JLabel文字魔法
- Java Swing 刷新JLabel的背景图片
- swing 组件基础(2) 之 Jlabel
- 如何在 Eclipse 中显示行号
- 仿乐享微信,微信公共平台
- javadoc时候乱码-编码 GBK 的不可映射字符
- the current branch is not configured for pull
- RS100项目进展更新
- Swing —— JLabel预览示例
- Mac系统下搭建Cocos2d-x开发环境及2.20以上版本如何创建HelloWorld
- 复杂程序系统构架方法-设备驱动程序
- 今天接到了第一个小项目
- 向已建JAVA项目添加jar包
- 揭开挖矿面纱 显卡挖矿的设置 及收益估算方法 N卡挖矿欢乐多啊
- java发展历史
- NSNotificationCenter/UILocalNotification/remote notification/notification center
- 【黑马程序员】java中------------Map集合