代码如下:
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(); } }
相关推荐
java JLabel组件 java JLabel组件 java JLabel组件
java Icon应用到JLabel中 java Icon应用到JLabel中 java Icon应用到JLabel中
主要介绍了Java编程swing组件JLabel详解以及使用示例,具有一定借鉴价值,需要的朋友可以参考下
JAVA开发中,swing的运用,用JLabel剪接图片
在JavaSE,给swing中的控件实现超链接效果其实很简单,代码是拿JLabel来举的例子
对java 中 JLabel 控件的应用实例的集合,都是一些效果实例。
String[] text={"Font Demo","Monospaced,斜体,24号","宋体字示例","黑体","Serif,粗体,斜体,18号"}; //显示的文本 Container container=getContentPane(); //得到容器 Box boxLayout=Box....
主要介绍了Jlabel实现内容自动换行简单实例,具有一定借鉴价值,需要的朋友可以参考下
杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 1/16 页 1.1 Java Swing GUI 组件技术教学参考示例(第 2 部分) 1.1.1 Swing ToolBar 组件的应用实例 1、窗口内的...
Java JLabel显示多种字体样式,其实是向大家演示如何在Java的窗口中实现自定义字体,示例中使用了多种字体样式,定义了不同的字体模式,比如字体名称、粗细、是否斜体等,这些都是Java的基础知识范畴吧。
动态提取系统当前时间并放到容器基本部件JLabel上,用于显示系统的动态时间
jLabel2.setBounds(new Rectangle(214, 8, 63, 28)); jLabel2.setText("选择班号"); jLabel2.setFont(new java.awt.Font("Dialog", 0, 15)); jLabel3.setBounds(new Rectangle(7, 54, 67, 28)); jLabel3....
JLabel是Java Swing库中的一个组件类,它用于显示简单的文本或图像。
主要介绍了Java Swing JLabel标签的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
package clock; /** *//** * MainFrame.java * Summary 万年历主类 * Created on * @author * remark */ ...import java.awt.BorderLayout;...import javax.swing.JLabel;... JLabel y_label = new JLabel("年份");
我已经在我的JFrame对象上添加了JLabel。 我想在JLabel上实现一个关键侦听器。 我可以实施吗? 如果是,我该怎么做?
JLabel移动动画 Java类,允许通过重新绘制直到达到正确的坐标来操纵JLabel坐标。 您可以选择移动速度。 用法:对象 JLabelMoveAnimation move_animation =新的JLabelMoveAnimation(); 用法:方法 move_animation....
14.8.1标签(Jlabel)使用示例488 14.8.2按钮(JButton)使用示例491 14.8.3文本框(JTextField)和密码框(JPasswordField)使用示例492 14.8.4文本区(JTextArea)使用示例496 14.8.5复选框(JcheckBox)使用...
滚动条多个JLabel的Java Swing字幕功能演示
以开发人员在项目开发中经常遇到的问题和必须掌握的技术为中心,介绍了应用Java进行程序开发的多方面的知识和技巧,主要包括Java的图形图像、动画游戏、网络应用技术、Applet应用、XML、JFree...