例6.1声明一个面板子类,面板子类对象有3个选择框。
class Panel1 extends JPanel {
JCheckBox box1, box2, box3; Panel1() {
box1 = new JCheckBox("足球");
box2 = new JCheckBox("排球");
box3 = new JCheckBox("篮球");
add(box1);
add(box2);
add(box3);
}
}
例6.2处理选择项目事件的小应用程序。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; class Panel1 extends JPanel {// 扩展Panel类
JRadioButton box1, box2, box3;
ButtonGroup g; Panel1() {// 3个单选按钮为一组
setLayout(new GridLayout(1, 3));
g = new ButtonGroup();
box1 = new JRadioButton(MyWindow.fName[0] + "计算机", false);
box2 = new JRadioButton(MyWindow.fName[1] + "计算机", false);
box3 = new JRadioButton(MyWindow.fName[2] + "计算机", false);
g.add(box1);
g.add(box2);
g.add(box3);
add(box1);
add(box2);
add(box3);
add(new JLabel("计算机3选1"));
}
} class Panel2 extends JPanel {// 扩展Panel类
JCheckBox box1, box2, box3;
ButtonGroup g; Panel2() {// 3个选择框为一组
setLayout(new GridLayout(1, 3));
g = new ButtonGroup();
box1 = new JCheckBox("购买1台");
box2 = new JCheckBox("购买2台");
box3 = new JCheckBox("购买3台");
g.add(box1);
g.add(box2);
g.add(box3);
add(box1);
add(box2);
add(box3);
add(new JLabel("选择1、2或3"));
}
} class MyWindow extends JFrame implements ItemListener {
Panel1 panel1;
Panel2 panel2;
JLabel label1, label2;
JTextArea text1, text2;
static String fName[] = { "HP", "IBM", "DELL" };// 公司名称表
static double priTbl[][] = { { 1.20, 1.15, 1.10 }, { 1.70, 1.65, 1.60 }, { 1.65, 1.60, 1.58 } };// 产品数量价格对照表
static int production = -1;// 产品标志 MyWindow(String s) {
super(s);
Container con = this.getContentPane();
con.setLayout(new GridLayout(3, 2));
this.setLocation(100, 100);
this.setSize(400, 100);
panel1 = new Panel1();
panel2 = new Panel2();
label1 = new JLabel("产品介绍", JLabel.CENTER);
label2 = new JLabel("产品价格", JLabel.CENTER);
text1 = new JTextArea();
text2 = new JTextArea();
con.add(label1);
con.add(label2);
con.add(panel1);
con.add(panel2);
con.add(text1);
con.add(text2);
panel1.box1.addItemListener(this);
panel1.box2.addItemListener(this);
panel1.box3.addItemListener(this);
panel2.box1.addItemListener(this);
panel2.box2.addItemListener(this);
panel2.box3.addItemListener(this);
this.setVisible(true);
this.pack();
} public void itemStateChanged(ItemEvent e) {
if (e.getItemSelectable() == panel1.box1) {
production = 0;
text1.setText(fName[0] + "公司生产");
text2.setText("");
} else if (e.getItemSelectable() == panel1.box2) {
production = 1;
text1.setText(fName[1] + "公司生产");
text2.setText("");
} else if (e.getItemSelectable() == panel1.box3) {
production = 2;
text1.setText(fName[2] + "公司生产");
text2.setText("");
} else {
if (production == -1) {
return;
}
if (e.getItemSelectable() == panel2.box1) {
text2.setText("" + priTbl[production][0] + "万元/台");
} else if (e.getItemSelectable() == panel2.box2) {
text2.setText("" + priTbl[production][1] + "万元/台");
} else if (e.getItemSelectable() == panel2.box3) {
text2.setText("" + priTbl[production][2] + "万元/台");
}
}
}
} public class Example6_2 {
MyWindow myWin = new MyWindow("选择项目处理示例程序");
}
例6.3小应用程序有两个列表,第一个只允许单选,第二个列表允许多选。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*; class MyWindow extends JFrame implements ListSelectionListener {
JList list1, list2;
String news[] = { "人民日报", "新民晚报", "浙江日报", "文汇报" };
String sports[] = { "足球", "排球", "乒乓球", "篮球" };
JTextArea text; MyWindow(String s) {
super(s);
Container con = getContentPane();
con.setBackground(Color.BLUE);
con.setLayout(new GridLayout(2, 2));
con.setSize(200, 500);
list1 = new JList(news);
list1.setVisibleRowCount(3);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list1.addListSelectionListener(this);
list2 = new JList(sports);
list2.setVisibleRowCount(2);
list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list2.addListSelectionListener(this);
con.add(list1);
con.add(list2);
text = new JTextArea(10, 20);
con.add(text);
this.setVisible(true);
this.pack();
} public void valueChanged(ListSelectionEvent e) {
if (e.getSource() == list1) {
text.setText(null);
Object listValue = ((JList) e.getSource()).getSelectedValue();
String seleName = listValue.toString();
for (int i = 0; i < news.length; i++) {
if (news[i].equals(seleName)) {
text.append(seleName + ":被选中\n");
}
}
} else if (e.getSource() == list2) {
text.setText(null);
int tempList[] = list2.getSelectedIndices();// 获得选中索引
for (int i = 0; i < tempList.length; i++) {
text.append(sports[tempList[i]] + ":被选中\n");
}
}
}
} public class Example6_3 extends Applet {
MyWindow myWin = new MyWindow("列表示例");
}
例6.4一个说明组合框用法的应用程序。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; public class Example6_4 {
public static void main(String[] args) {
ComboBoxDemo myComboBoxGUI = new ComboBoxDemo();
}
} class ComboBoxDemo extends JFrame implements ActionListener, ItemListener {
public static final int Width = 350;
public static final int Height = 150;
String proList[] = { "踢足球", "打篮球", "打排球" };
JTextField text;
JComboBox comboBox; public ComboBoxDemo() {
setSize(Width, Height);
setTitle("组合框使用示意程序");
Container conPane = getContentPane();
conPane.setBackground(Color.BLUE);
conPane.setLayout(new FlowLayout());
comboBox = new JComboBox(proList);
comboBox.addActionListener(this);
comboBox.addItemListener(this);
comboBox.setEditable(true);
conPane.add(comboBox);
text = new JTextField(10);
conPane.add(text);
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == comboBox) {
text.setText(comboBox.getSelectedItem().toString());
}
} public void itemStateChanged(ItemEvent e) {
if (e.getSource() == comboBox) {
text.setText(comboBox.getSelectedItem().toString());
}
}
}
例6.5小应用程序示意窗口有菜单条的实现方法。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; class MenuWindow extends JFrame implements ActionListener {
public static JTextField text; private void addItem(JMenu menu, String menuName, ActionListener listener) {
JMenuItem anItem = new JMenuItem(menuName);
anItem.setActionCommand(menuName);
anItem.addActionListener(listener);
menu.add(anItem);
} public MenuWindow(String s, int w, int h) {
setTitle(s);
Container con = this.getContentPane();
con.setLocation(100, 100);
this.setSize(w, h);
JMenu menu1 = new JMenu("体育");
addItem(menu1, "跑步", this);
addItem(menu1, "跳绳", this);
addItem(menu1, "打球", this);
JMenu menu2 = new JMenu("娱乐");
addItem(menu2, "唱歌", this);
addItem(menu2, "跳舞", this);
addItem(menu2, "游戏", this);
JMenuBar menubar = new JMenuBar();
text = new JTextField();
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
con.add(text, BorderLayout.NORTH);
} public void actionPerformed(ActionEvent e) {
text.setText(e.getActionCommand() + "菜单项被选中!");
}
} public class Example6_5 extends Applet implements ActionListener { MenuWindow window;
JButton button;
boolean bflg; public void init() {
button = new JButton("打开我的体育娱乐之窗");
bflg = true;
window = new MenuWindow("体育娱乐之窗", 100, 100);
button.addActionListener(this);
add(button);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
if (bflg) {
window.setVisible(true);
bflg = false;
button.setLabel("关闭我的体育娱乐之窗");
} else {
window.setVisible(false);
bflg = true;
button.setLabel("打开我的体育娱乐之窗");
}
}
}
}
例6.6小应用程序声明一个用户窗口类和对话框类,用户窗口有两个按钮和两个文本框,当点击某个按钮时,对应的对话框被激活。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; class MyWindow extends JFrame implements ActionListener {
private JButton button1, button2;
private static int flg = 0;
private static JTextField text1, text2; MyWindow(String s)// 窗口内有两个按钮
{
super(s);
Container con = this.getContentPane();
con.setLayout(new GridLayout(2, 2));
this.setSize(200, 100);
this.setLocation(100, 100);
button1 = new JButton("选择水果");
button2 = new JButton("选择食品");
button1.addActionListener(this);
button2.addActionListener(this);
text1 = new JTextField(20);
text2 = new JTextField(20);
con.add(button1);
con.add(button2);
con.add(text1);
con.add(text2);
this.setVisible(true);
this.pack();
} public static void returnName(String s) {
if (flg == 1) {
text1.setText("选择的水果是:" + s);
} else if (flg == 2) {
text2.setText("选择的食品是:" + s);
}
} public void actionPerformed(ActionEvent e) {
MyDialog dialog;
if (e.getSource() == button1) {
dialog = new MyDialog(this, "水果");
dialog.setVisible(true);
flg = 1;
} else if (e.getSource() == button2) {
dialog = new MyDialog(this, "食品");
dialog.setVisible(true);
flg = 2;
}
}
} class MyDialog extends JDialog implements ActionListener {
JLabel title;
JTextField text;
JButton done; MyDialog(JFrame F, String s) {
super(F, s, true);
Container con = this.getContentPane();
title = new JLabel("输入" + s + "名称");
text = new JTextField(10);
text.setEditable(true);
con.setLayout(new FlowLayout());
con.setSize(200, 100);
setModal(false);
done = new JButton("确定");
done.addActionListener(this);
con.add(title);
con.add(text);
con.add(done);
con.setVisible(true);
this.pack();
} public void actionPerformed(ActionEvent e) {
MyWindow.returnName(text.getText());
setVisible(false);
dispose();
}
} public class Example6_6 extends Applet {
MyWindow window;
MyDialog diaglog; public void init()// 程序的主窗口暂没有组件
{
window = new MyWindow("带对话框窗口");// 创建一个窗口
}
}
例6.7应用程序将滚动条作为值的选择。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; class MyScrollBar extends JScrollBar {
public MyScrollBar(int init, int len, int low, int high) {
super(JScrollBar.HORIZONTAL, init, len, low, high);
} public Dimension getPreferredSize() {
return new Dimension(125, 20);
}
} class MyWindow extends JFrame implements ActionListener, AdjustmentListener {
private JButton button;
private JTextField text;
private boolean barOpened; MyWindow(String s) {
super(s);
MyScrollBar tempBar = new MyScrollBar(10, 10, 0, 255);
Container con = this.getContentPane();
con.setLayout(new GridLayout(2, 1));
this.setSize(200, 100);
this.setLocation(100, 100);
button = new JButton("开/闭滚动条");
button.addActionListener(this);
barOpened = false;
tempBar.addAdjustmentListener(this);
text = new JTextField("滚动条关闭", 20);
con.add(button);
con.add(text);
con.add(tempBar);
this.setVisible(true);
this.pack();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
if (barOpened) {
text.setText("滚动条关闭");
} else {
text.setText("滚动条打开");
}
barOpened = !barOpened;
}
} public void adjustmentValueChanged(AdjustmentEvent e) {
if (barOpened) {
MyScrollBar myBar = (MyScrollBar) e.getAdjustable();
text.setText("选择的值是:" + myBar.getValue());
}
}
} public class Example6_7 {
public static void main(String[] args) {
MyWindow myWindow = new MyWindow("滚动条实例");
}
}
例6.8小应用程序设置了一个文本区,用于记录一系列鼠标事件。
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; class MyPanel extends JPanel {
public void print(int r) {
Graphics g = getGraphics();// 获得系统给予小应用程序的图形对象
g.clearRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
g.fillOval(10, 10, r, r);// 用红色填充一个圆块
}
} class MyWindow extends JFrame implements MouseListener {
JTextArea text;
MyPanel panel;
int x, y, r = 10;
int mouseFlg = 0;
static String mouseStates[] = { "鼠标键按下", "鼠标松开", "鼠标进来", "鼠标走开", "鼠标双击" }; MyWindow(String s) {
super(s);
Container con = this.getContentPane();
con.setLayout(new GridLayout(2, 1));
this.setSize(200, 300);
this.setLocation(100, 100);
panel = new MyPanel();
con.add(panel);
text = new JTextArea(10, 20);
text.setBackground(Color.blue);
con.add(text);
addMouseListener(this);
this.setVisible(true);
this.pack();
} public void paint(Graphics g) {
r = r + 4;
if (r > 80) {
r = 10;
}
text.append(mouseStates[mouseFlg] + "了,位置是:" + x + "," + y + "\n");
panel.print(r);
} public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
mouseFlg = 0;
repaint();
} public void mouseReleased(MouseEvent e) {
x = e.getX();
y = e.getY();
mouseFlg = 1;
repaint();
} public void mouseEntered(MouseEvent e) {
x = e.getX();
y = e.getY();
mouseFlg = 2;
repaint();
} public void mouseExited(MouseEvent e) {
x = e.getX();
y = e.getY();
mouseFlg = 3;
repaint();
} public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
x = e.getX();
y = e.getY();
mouseFlg = 4;
repaint();
} else { }
}
} public class Example6_8 extends Applet {
public void init() {
MyWindow myWindow = new MyWindow("鼠标事件示意程序");
}
}
例6.9一个滚动条与显示窗口同步变化的应用程序。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; class MyWindow extends JFrame {
public MyWindow(String s) {
super(s);
Container con = this.getContentPane();
con.setLayout(new GridLayout());
this.setLocation(100, 100);
JScrollBar xAxis = new JScrollBar(JScrollBar.HORIZONTAL, 50, 1, 0, 100);
JScrollBar yAxis = new JScrollBar(JScrollBar.VERTICAL, 50, 1, 0, 100);
MyListener listener = new MyListener(xAxis, yAxis, 238, 118);
JPanel scrolledCanvas = new JPanel();
scrolledCanvas.setLayout(new BorderLayout());
scrolledCanvas.add(listener, BorderLayout.CENTER);
scrolledCanvas.add(xAxis, BorderLayout.SOUTH);
scrolledCanvas.add(yAxis, BorderLayout.EAST);
con.add(scrolledCanvas, BorderLayout.CENTER);
this.setVisible(true);
this.pack();
} public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
} class MyListener extends JComponent implements MouseListener, MouseMotionListener, AdjustmentListener {
private int x, y;
private JScrollBar xScrollBar;
private JScrollBar yScrollBar; private void updateScrollBars(int x, int y) {
int d;
d = (int) (((float) x / (float) getSize().width) * 100.0);
xScrollBar.setValue(d);
d = (int) (((float) y / (float) getSize().height) * 100.0);
yScrollBar.setValue(d);
} public MyListener(JScrollBar xaxis, JScrollBar yaxis, int x0, int y0) {
xScrollBar = xaxis;
yScrollBar = yaxis;
x = x0;
y = y0;
xScrollBar.addAdjustmentListener(this);
yScrollBar.addAdjustmentListener(this);
this.addMouseListener(this);// 监视鼠标点击事件
this.addMouseMotionListener(this);// 监视鼠标拖动事件
} public void paint(Graphics g) {
g.setColor(getBackground());
Dimension size = getSize();
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.blue);
g.fillRect(x, y, 50, 50);
} public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
updateScrollBars(x, y);
repaint();
} public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
updateScrollBars(x, y);
repaint();
} public void adjustmentValueChanged(AdjustmentEvent e) {
if (e.getSource() == xScrollBar) {
x = (int) ((float) (xScrollBar.getValue() / 100.0) * getSize().width);
} else if (e.getSource() == yScrollBar) {
y = (int) ((float) (yScrollBar.getValue() / 100.0) * getSize().height);
}
repaint();
}
} public class Example6_9 {
public static void main(String[] args) {
MyWindow myWindow = new MyWindow("滚动条示意程序");
}
}
例6.10小应用程序有一个按钮和一个文本区,按钮作为发生键盘事件的事件源,并对它实施监视。
import java.applet.*;
import java.awt.*;
import java.awt.event.*; public class Example6_10 extends Applet implements KeyListener {
int count = 0;
Button button = new Button();
TextArea text = new TextArea(5, 20); public void init() {
button.addKeyListener(this);
add(button);
add(text);
} public void keyPressed(KeyEvent e) {
int t = e.getKeyCode();
if (t >= KeyEvent.VK_A && t <= KeyEvent.VK_Z) {
text.append((char) t + " ");
count++;
if (count % 10 == 0) {
text.append("\n");
}
}
} public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { }
}