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);
}
}
8.4.4 AWT兼容
8.5 本章回顾