第8章 标签与按钮
Swing的标签和按钮分别用JLabel和JButton类表示,它们是能够显示文本或图标的简单组件。缺省时,标签没有边框,可以显示一个字符串,一个图标或同时显示字符串和图标。除了用于修饰文本域等不重要的小事情外,Swing的标签还能起到图像画布(显示一个图像的组件)的作用。由于AWT的图像不是组件,不能把它们添加到一个容器中。因此,使用AWT的开发人员实现了各种不同的图像画布类;然而,在Swing中,可以把JLabel类当作图像画面使用(注:有关图像画面的更多信息参见4.3.1“Swing组件中的定制绘制”一节)。
按钮大概是使用最为普遍的用户界面组件。按钮通常带有某种边框,且可以被鼠标或快捷键激活。Swing按钮比Swing标签要复杂得多,不仅因为能够激活它们来完成某个功能,而且很多其他Swing组件都是AbstractButton类的扩展,而AbstractButton类是Swing按钮的基类。
8.1 JLabel与JBution
8.2 JLabel
例8-1 运行中的JLabel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JLabel imageOnly = new JLabel(new ImageIcon(this.getClass().getResource("dogs.gif")));
JLabel textAndImage = new JLabel("Vote!",
new ImageIcon(this.getClass().getResource("ballot_box.gif")),
JLabel.RIGHT);
JScrollPane scrollPane = new JScrollPane(imageOnly);
scrollPane.setPreferredSize(new Dimension(270,200));
contentPane.setLayout(
new FlowLayout(FlowLayout.CENTER, 25, 25));
contentPane.add(textAndImage);
contentPane.add(scrollPane);
}
}
8.2.1内容排列
例8-2 设置Swing标签的排列属性
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends JApplet implements SwingConstants {
JLabel label = new JLabel("Action!");
JPanel controlPanel = new JPanel();
JComboBox alignmentHorizontal = new JComboBox();
JComboBox alignmentVertical = new JComboBox();
public void init() {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon(this.getClass().getResource("slate.gif"));
label.setIcon(icon);
label.setHorizontalAlignment(CENTER);
label.setFont(new Font("Times-Roman", Font.ITALIC, 20));
label.setMaximumSize(new Dimension(0, 150));
setupComboBoxes();
setupControlPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(controlPanel, "North");
contentPane.add(label, "Center");
alignmentVertical.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int c = getSwingConstantByName(s);
label.setVerticalAlignment(c);
}
});
alignmentHorizontal.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int c = getSwingConstantByName(s);
label.setHorizontalAlignment(c);
}
});
}
void setupComboBoxes() {
alignmentVertical.addItem("Top");
alignmentVertical.addItem("Center");
alignmentVertical.addItem("Bottom");
alignmentHorizontal.addItem("Left");
alignmentHorizontal.addItem("Center");
alignmentHorizontal.addItem("Right");
alignmentVertical.setSelectedItem(
getSwingConstantName(
label.getVerticalAlignment()));
alignmentHorizontal.setSelectedItem(
getSwingConstantName(
label.getHorizontalAlignment()));
}
void setupControlPanel() {
controlPanel.setBorder(
BorderFactory.createTitledBorder("Alignment"));
controlPanel.add(new JLabel(
"Vertical:"));
controlPanel.add(alignmentVertical);
controlPanel.add(Box.createHorizontalStrut(5));
controlPanel.add(Box.createHorizontalStrut(25));
controlPanel.add(new JLabel(
"Horizontal:"));
controlPanel.add(Box.createHorizontalStrut(5));
controlPanel.add(alignmentHorizontal);
}
int getSwingConstantByName(String s) {
if(s.equalsIgnoreCase("left")) return LEFT;
else if(s.equalsIgnoreCase("center")) return CENTER;
else if(s.equalsIgnoreCase("right")) return RIGHT;
else if(s.equalsIgnoreCase("top")) return TOP;
else if(s.equalsIgnoreCase("bottom")) return BOTTOM;
return -1;
}
String getSwingConstantName(int c) {
if(c == LEFT) return "Left";
else if(c == CENTER) return "Center";
else if(c == RIGHT) return "Right";
else if(c == TOP) return "Top";
else if(c == BOTTOM) return "Bottom";
return "undefined";
}
}
8.2.2文本的位置
例8-3 设置标签的文本位置
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends JApplet implements SwingConstants {
JLabel label = new JLabel("Action!");
JPanel controlPanel = new JPanel();
JComboBox alignmentHorizontal = new JComboBox();
JComboBox alignmentVertical = new JComboBox();
public void init() {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon(this.getClass().getResource("penguin.gif"));
label.setIcon(icon);
label.setHorizontalTextPosition(CENTER);
label.setFont(new Font("Times-Roman", Font.ITALIC, 20));
setupComboBoxes();
setupControlPanel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
contentPane.setLayout(new BorderLayout());
contentPane.add(controlPanel, "North");
contentPane.add(label, "Center");
alignmentVertical.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int c = getSwingConstantByName(s);
label.setVerticalTextPosition(c);
}
});
alignmentHorizontal.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int c = getSwingConstantByName(s);
label.setHorizontalTextPosition(c);
}
});
}
void setupComboBoxes() {
alignmentVertical.addItem("Top");
alignmentVertical.addItem("Center");
alignmentVertical.addItem("Bottom");
alignmentHorizontal.addItem("Left");
alignmentHorizontal.addItem("Center");
alignmentHorizontal.addItem("Right");
alignmentVertical.setSelectedItem(
getSwingConstantName(
label.getVerticalTextPosition()));
alignmentHorizontal.setSelectedItem(
getSwingConstantName(
label.getHorizontalTextPosition()));
}
void setupControlPanel() {
controlPanel.setBorder(
BorderFactory.createTitledBorder("Text Position"));
controlPanel.add(new JLabel( "Vertical:"));
controlPanel.add(alignmentVertical);
controlPanel.add(Box.createHorizontalStrut(5));
controlPanel.add(Box.createHorizontalStrut(25));
controlPanel.add(new JLabel("Horizontal:"));
controlPanel.add(Box.createHorizontalStrut(5));
controlPanel.add(alignmentHorizontal);
}
int getSwingConstantByName(String s) {
if(s.equalsIgnoreCase("left")) return LEFT;
else if(s.equalsIgnoreCase("center")) return CENTER;
else if(s.equalsIgnoreCase("right")) return RIGHT;
else if(s.equalsIgnoreCase("top")) return TOP;
else if(s.equalsIgnoreCase("bottom")) return BOTTOM;
return -1;
}
String getSwingConstantName(int c) {
if(c == LEFT) return "Left";
else if(c == CENTER) return "Center";
else if(c == RIGHT) return "Right";
else if(c == TOP) return "Top";
else if(c == BOTTOM) return "Bottom";
return "undefined";
}
}
8.2.3图标/文本间隙
例8-4 设置一个标签的图标/文本间隙
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends JApplet implements SwingConstants {
public void init() {
Container contentPane = getContentPane();
JComboBox iconTextGap = new JComboBox();
JPanel controlPanel = new JPanel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif"));
final JLabel label = new JLabel("Lady Bug", icon, CENTER);
label.setFont(new Font("Times-Roman", Font.ITALIC, 20));
iconTextGap.addItem("4");
iconTextGap.addItem("10");
iconTextGap.addItem("15");
iconTextGap.addItem("20");
iconTextGap.addItem("25");
controlPanel.add(new JLabel("Icon/Text Gap:"));
controlPanel.add(iconTextGap);
contentPane.setLayout(new BorderLayout());
contentPane.add(controlPanel, "North");
contentPane.add(label, "Center");
iconTextGap.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int gap = Integer.parseInt(s);
label.setIconTextGap(gap);
}
});
}
}
8.2.4许可状态
例8-5 设置一个标签的许可状态
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends JApplet implements SwingConstants {
public void init() {
Container contentPane = getContentPane();
JComboBox iconTextGap = new JComboBox();
JPanel controlPanel = new JPanel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif"));
final JLabel label = new JLabel("Lady Bug", icon, CENTER);
label.setFont(new Font("Times-Roman", Font.ITALIC, 20));
iconTextGap.addItem("4");
iconTextGap.addItem("10");
iconTextGap.addItem("15");
iconTextGap.addItem("20");
iconTextGap.addItem("25");
controlPanel.add(new JLabel("Icon/Text Gap:"));
controlPanel.add(iconTextGap);
contentPane.setLayout(new BorderLayout());
contentPane.add(controlPanel, "North");
contentPane.add(label, "Center");
iconTextGap.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int gap = Integer.parseInt(s);
label.setIconTextGap(gap);
}
});
}
}
8.2.5 JLabel属性
8.2.6 JLabel事件
8.2.7 JLabel类总结
例8-6 创建JLabel实例
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends JApplet implements SwingConstants {
public void init() {
Container contentPane = getContentPane();
JComboBox iconTextGap = new JComboBox();
JPanel controlPanel = new JPanel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif"));
final JLabel label = new JLabel("Lady Bug", icon, CENTER);
label.setFont(new Font("Times-Roman", Font.ITALIC, 20));
iconTextGap.addItem("4");
iconTextGap.addItem("10");
iconTextGap.addItem("15");
iconTextGap.addItem("20");
iconTextGap.addItem("25");
controlPanel.add(new JLabel("Icon/Text Gap:"));
controlPanel.add(iconTextGap);
contentPane.setLayout(new BorderLayout());
contentPane.add(controlPanel, "North");
contentPane.add(label, "Center");
iconTextGap.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
JComboBox b = (JComboBox)event.getSource();
String s = (String)b.getSelectedItem();
int gap = Integer.parseInt(s);
label.setIconTextGap(gap);
}
});
}
}
8.3 按钮
8.4 JButtion
例8-7 一个按钮简单例子
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JApplet {
JButton button = new JButton("button ...",
new ImageIcon(this.getClass().getResource("exclaim.gif")));
int actCnt = 0;
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showStatus(event.getActionCommand() +
" activated " + actCnt + " times");
actCnt++;
}
});
}
}
8.4.1 JButtion属性
8.4.2 JButtion事件
例8-8 处理JButton事件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JApplet {
//Icon icon = new ImageIcon("icon.gif");
JButton button = new JButton("button");
public Test() {
Container contentPane = getContentPane();
button.setRolloverIcon(new ImageIcon(this.getClass().getResource("punch.gif")));
button.setIcon(new ImageIcon(this.getClass().getResource("open_hand.gif")));
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("action!");
}
});
button.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println(getButtonState());
}
});
}
private String getButtonState() {
ButtonModel model = button.getModel();
String state = "Button State: ";
state += model.isSelected() ? "selected" : "deselected";
state += model.isPressed() ? ", pressed" :
", not pressed";
state += model.isArmed() ? ", armed" : ", disarmed";
state += model.isRollover() ? ", rollover" :
", not rollover";
return state;
}
}
8.4.3 JButtion类总结
例8-9 创建JButton实例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
Icon icon = new ImageIcon("icon.gif");
JButton noargButton = new JButton(),
textButton = new JButton("text"),
textIconButton = new JButton("text", icon),
iconButton = new JButton(icon);
public Test() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(noargButton);
contentPane.add(textButton);
contentPane.add(iconButton);
contentPane.add(textIconButton);
}
}
例8-10 把一个按钮指定为缺省的按钮
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
Icon icon = new ImageIcon("icon.gif");
JButton noargButton = new JButton(),
textButton = new JButton("text"),
textIconButton = new JButton("text", icon),
iconButton = new JButton(icon);
public Test() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(noargButton);
contentPane.add(textButton);
contentPane.add(iconButton);
contentPane.add(textIconButton);
}
}
例8-11 程序方式单击一个按钮
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
int clickDuration = 68;
public Test() {
Container contentPane = getContentPane();
JPanel controlPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton doClick = new JButton("do click");
final JButton clickMe = new JButton("click me");
final JComboBox comboBox = new JComboBox(new Object[] {
"68", "250", "500", "750", "1000"
});
controlPanel.add(new JLabel("Click Duration:"));
controlPanel.add(comboBox);
buttonPanel.add(doClick);
buttonPanel.add(clickMe);
contentPane.add(controlPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
getRootPane().setDefaultButton(doClick);
doClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickMe.doClick(clickDuration);
}
});
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
clickDuration = Integer.parseInt((String)
comboBox.getSelectedItem());
}
}
});
}
}
例8-12 JButton图标
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
Icon icon = new StringIcon("icon for JButton"),
rolloverIcon = new StringIcon("rollover"),
pressedIcon = new StringIcon("pressed"),
disabledIcon = new StringIcon("disabled"),
selectedIcon = new StringIcon("selected"),
rolloverSelectedIcon =
new StringIcon("rollover selected"),
disabledSelectedIcon =
new StringIcon("disabled selected");
final JButton button = new JButton();
button.setRolloverEnabled(true);
button.setIcon(icon);
button.setRolloverIcon(rolloverIcon);
button.setRolloverSelectedIcon(rolloverSelectedIcon);
button.setSelectedIcon(selectedIcon);
button.setPressedIcon(pressedIcon);
button.setDisabledIcon(disabledIcon);
button.setDisabledSelectedIcon(disabledSelectedIcon);
JComboBox cb = new JComboBox();
cb.addItem("enabled");
cb.addItem("disabled");
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
String item = (String)e.getItem();
if(item.equals("enabled")) {
button.setEnabled(true);
}
else {
button.setEnabled(false);
}
}
}
});
contentPane.setLayout(new FlowLayout());
contentPane.add(cb);
contentPane.add(button);
}
}
class StringIcon implements Icon {
private String s;
public StringIcon(String s) {
this.s = s;
}
public int getIconWidth() { return 100; }
public int getIconHeight() { return 100; }
public void paintIcon(Component c, Graphics g, int x, int y) {
FontMetrics fm = g.getFontMetrics();
g.setColor(c.getForeground());
g.drawString(s, 10, fm.getHeight());
}
}
例8-13 设置按钮边距
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JButton button = new JButton("button");
button.setMargin(new Insets(50,25,10,5));
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
}
}
例8-14 按钮助记符
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JButton button = new JButton("button With Mnemonic");
button.setMnemonic('M');
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
}
}
相关推荐
第9章Java-Swing图形用户界面-Java面向对象程序设计教程-微课视频-程杰-清华大学出版社全文共27页,当前为第8页。 9.3.3GridLayout布局 GridLayout是使用较多的布局,其基本的布局策略是把容器划分成若干行乘若干列...
java语言与面向对象程序设计形考4-0001 java语言与面向对象程序设计形考4-0001全文共12页,当前为第1页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第1页。java语言与面向对象程序设计形考4-0001 java...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
第8章详细讨论AWT(Abstract Window Toolkit )的事件模型。我们将介绍如何编写代码来响 V 应鼠标点击或敲击键盘等事件。同时,还将介绍如何处理基本的GUI元素,比如:按钮和面板。 第9章详细讨论Swing GUI 工具箱...
第8章建立对话框 8 1Dialog类 8 2文件对话框 8 3消息对话框 8 4确认对话框 8 5颜色对话框 习题 第9章Java多线程机制 9 1Java中的线程 9 2Thread类的子类创建线程 9 3使用Runnable接口 9 4线程的常用方法 9 5GUI线程 ...
界面设计 " "界面设计要求用GUI,界面设计有用到swing组件的textField和Button,用到" "awt中的BorderLayout和GridLayout布局管理方式,其图形界面如下: " " " " " " " "2. 功能介绍 " " " "这是计算器中的一种:...
130 7.2.1 异常处理相关语句 130 7.2.2 公共异常 132 7.3 抛出语句 133 7.4 创建自己的异常 135 习题 137 第8章 图形用户界面设计 140 8.1 AWT与Swing 140 8.1.1 AWT包与Swing包 140 8.1.2 组件、...
Java2核心技术第I卷.基础知识 目录: 译者序 前言 第1章Java程序设计概述 1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1简单性 1.2.2面向对象 1.2. 3分布式 1. 2.4健壮性 1. 2.5安仝...
010302_【第3章:Java基础程序设计】_运算符、表达式与语句笔记.pdf 010303_【第3章:Java基础程序设计】_判断与循环语句笔记.pdf 010401_【第4章:数组与方法】_数组的定义及使用笔记.pdf 010402_【第4章:数组与...
Tcp服务端与客户端的JAVA实例源代码 2个目标文件 摘要:Java源码,文件操作,TCP,服务器 Tcp服务端与客户端的JAVA实例源代码,一个简单的Java TCP服务器端程序,别外还有一个客户端的程序,两者互相配合可以开发出超多...
Java语言程序设计(一)课后习题第十二章(附答案) 十二、常用组件之一________命令按钮与标签框 1.判断:标签是是一个容器。() 2.判断:在Swing用户界面的程序设计中,容器可以被添加到其它容器中去。() 3. 用户不...
Tcp服务端与客户端的JAVA实例源代码 2个目标文件 摘要:Java源码,文件操作,TCP,服务器 Tcp服务端与客户端的JAVA实例源代码,一个简单的Java TCP服务器端程序,别外还有一个客户端的程序,两者互相配合可以开发出超多...
第6章 java图形用户界面 6.1 HelloWorldSwing 6.2 按钮计数器 6.3 Swing按钮 6.4 温度转换器 6.5 HtmlDemo 6.6 投票器 6.7 TextSamplerDemo 6.8 Swing菜单 6.9 文件对话框 6.10 月相 6.11 单位转换器 6.12 Swing动画...
Java2核心技术第I卷.基础知识 目录: 译者序 前言 第1章Java程序设计概述 1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1简单性 1.2.2面向对象 1.2. 3分布式 1. 2.4健壮性 1. 2.5安仝...
第6章 java图形用户界面 6.1 HelloWorldSwing 6.2 按钮计数器 6.3 Swing按钮 6.4 温度转换器 6.5 HtmlDemo 6.6 投票器 6.7 TextSamplerDemo 6.8 Swing菜单 6.9 文件对话框 6.10 月相 6.11 单位转换器 6.12 Swing动画...
Java2核心技术第I卷.基础知识 目录: 译者序 前言 第1章Java程序设计概述 1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1简单性 1.2.2面向对象 1.2. 3分布式 1. 2.4健壮性 1. 2.5安仝...
Java2核心技术第I卷.基础知识 目录: 译者序 前言 第1章Java程序设计概述 1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1简单性 1.2.2面向对象 1.2. 3分布式 1. 2.4健壮性 1. 2.5安仝性 1. 2.6...
Java2核心技术第I卷.基础知识 目录: 译者序 前言 第1章Java程序设计概述 1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1简单性 1.2.2面向对象 1.2. 3分布式 1. 2.4健壮性 1. 2.5安仝...