第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事件