例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) { }
}

04747_Java语言程序设计(一)_第6章_图形界面设计(二)的更多相关文章

  1. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  2. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  3. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  4. python 教程 第十九章、 图形界面编程

    第十九章. 图形界面编程 import Tkinter top = Tkinter.Tk() hello = Tkinter.Label(top, text='Hello World!') hello ...

  5. 04747_Java语言程序设计(一)_第3章_面向对象编程基础

    链式编程 每次调用方法后,返回的是一个对象 /* * 链式编程 * 每次调用方法后,返回的是一个对象 */ class Student { public void study() { System.o ...

  6. 04747_Java语言程序设计(一)_第1章_Java语言基础

    二进制0b开头 八进制0开头 十六进制0x开头 package com.jacky; public class Aserver { public static void main(String arg ...

  7. 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础

    例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...

  8. 04747_Java语言程序设计(一)_第9章_输入和输出流

    例9.1一个文件复制应用程序,将某个文件的内容全部复制到另一个文件. import java.io.*; public class Example9_1 { public static void ma ...

  9. 04747_Java语言程序设计(一)_第8章_多线程

    例8.1应用程序用Thread子类实现多线程. import java.util.Date; public class Example8_1 { static Athread threadA; sta ...

  10. 04747_Java语言程序设计(一)_第7章_图形、图像与多媒体

    例7.1小应用程序用6种字型显示字符串,显示内容说明本身的字型. import java.applet.*; import java.awt.*; public class Example7_1 ex ...

随机推荐

  1. c++ 依据输入动态声明数组(一维,二维)

    较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #includ ...

  2. MVC4 成员资格、 身份验证

    SimpleMembership,成员资格提供程序. 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板 ASP.NET MVC 4 互联网模板中添加一些新的 ...

  3. 标准I/O介绍

    标准I/O库 1. 标准I/O介绍 不仅是在UNIX系统中,很多操作系统上都实现了标准I/O库. 标准I/O库由ANSI C 标准说明 标准 I/O 库处理很多细节,例如带有缓冲分配.以优化长度执行的 ...

  4. 搭建LAMP架构

    1. 为什么下载源码包需要到官网上去下载?简单说就是为了安全,如果是非官方下载的源码包,有可能被别有用心的人动过手脚. 2. 64位机器是否可以安装32位rpm包?64位机器是否可以安装32位的mys ...

  5. 应用highcharts做直观数据统计

    最近在看上了统计类的东东,发现以前端图表神器:highcharts Highcharts是一款功能强大.开源.美观.图表丰富.兼容绝大多数浏览器的纯Js图表库,Highcharts支持的图表类型有直线 ...

  6. java 静态文件使用注解

    spring框架为我们代码的编写带来了极大的便利,特别是注解的使用.但是有个问题,当我们在静态文件中使用注解的时候,这个时候就会报错了.如以下代码: @Autowired private UserSe ...

  7. linux服务器伪分布模式安装hadoop-1.1.2

    1:环境准备    1台linux服务器, hadoop安装包(apache官方网下载) jdk1.6+   2:安装jdk ,配置好环境变量(etc/profile),java -version 测 ...

  8. javascript作用域和作用域链

    1.作用域 作用域,它是指对某一变量和方法具有访问权限的代码空间.当我们在定义变量的时候,会定义两种变量,一种是在全局环境下定义的变量,叫全局变量,一种是在函数中定义的变量叫局部变量.全局变量的作用域 ...

  9. hdu find the safest road

    算法:多源最短路(floyd) 题意:每条通路有一个安全系数,求始点到终点的最大的安全系数并输出,如果没有输出What a pity! c++超时啊 Problem Description XX星球有 ...

  10. C++中使用stringstream简化类型转换

    C++标准库中的<sstream>提供了一个stringstream,以前基本没用过,突然发现很好用(^-^)V 参见 http://www.cplusplus.com/reference ...