04747_Java语言程序设计(一)_第5章_图形界面设计(一)

例5.1一个用JFrame类创建窗口的Java应用程序。

import javax.swing.*;

public class Example5_1 {
	public static void main(String[] args) {
		JFrame mw = new JFrame("我的第一个窗口");// 创建一个窗口容器对象。
		mw.setSize(250, 200);// 设定窗口的宽和窗口的高,单位是像素
		JButton button = new JButton("我是一个按钮");
		mw.getContentPane().add(button);/* 获得窗口的内容面板,并将按钮添加在这个内容面板中 */
		mw.setVisible(true);
	}
}

例5.2定义JFrame派生的子类MyWindowDemo创建JFrame窗口。类MyWindowDemo的构造方法有五个参数:窗口的标题名,加入窗口的组件,窗口的背景颜色以及窗口的高度和宽度。在主方法中,利用类MyWindowDemo创建两个类似的窗口。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Example5_2 {
	public static MyWindowDemo mw1;
	public static MyWindowDemo mw2;

	public static void main(String[] args) {
		JButton butt1 = new JButton("我是一个按钮");
		String name1 = "我的第一个窗口";
		String name2 = "我的第二个窗口";
		mw1 = new MyWindowDemo(name1, butt1, Color.blue, 350, 450);
		mw1.setVisible(true);
		JButton butt2 = new JButton("我是另一个按钮");
		mw2 = new MyWindowDemo(name2, butt2, Color.magenta, 300, 400);
		mw2.setVisible(true);
	}
}

class MyWindowDemo extends JFrame {
	public MyWindowDemo(String name, JButton button, Color c, int w, int h) {
		super();
		setTitle(name);
		setSize(w, h);
		Container contentPane = getContentPane();// 获得窗口内容面板
		contentPane.add(button);// 将按钮添加在内容面板中
		contentPane.setBackground(c);// 设置背景颜色
	}
}

例5.3处理按钮事件实例。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Example5_3 {
	public static void main(String[] args) {
		ButtonDemo myButtonGUI = new ButtonDemo();// 声明并创建按钮对象
		myButtonGUI.setVisible(true);
	}
}

class ButtonDemo extends JFrame implements ActionListener {
	public static final int Width = 250;
	public static final int Height = 200;

	ButtonDemo() {
		setSize(Width, Height);
		setTitle("按钮事件样例");
		Container conPane = getContentPane();
		conPane.setBackground(Color.BLUE);
		conPane.setLayout(new FlowLayout());// 采用FlowLayout布局
		JButton redBut = new JButton("Red");
		redBut.addActionListener(this);// 给Red按钮注册监视器
		conPane.add(redBut);// 在窗口添加Red按钮
		JButton greenBut = new JButton("Green");
		greenBut.addActionListener(this);// 给Green按钮注册监视器
		conPane.add(greenBut);// 在窗口添加Green按钮
	}

	public void actionPerformed(ActionEvent e)// 实现接口处理事件的方法
	{
		Container conPane = getContentPane();
		if (e.getActionCommand().equals("Red"))// 是Red按钮事件
		{
			conPane.setBackground(Color.RED);
		} else if (e.getActionCommand().equals("Green"))// 是Green按钮事件
		{
			conPane.setBackground(Color.GREEN);
		} else {

		}
	}
}

例5.4小应用程序有两个JPanel子类对象和一个按钮。

import java.applet.*;
import javax.swing.*;

class MyPanel extends JPanel {
	JButton button1, button2;
	JLabel label;

	MyPanel(String s1, String s2, String s3)// Panel对象被初始化为有两个按钮和一个文本框。
	{
		button1 = new JButton(s1);
		button2 = new JButton(s2);
		label = new JLabel(s3);
		add(button1);
		add(button2);
		add(label);
	}
}

public class Example5_4 extends Applet {
	MyPanel panel1, panel2;
	JButton button;

	public void init() {
		panel1 = new MyPanel("确定", "取消", "标签,我们在面板1中");
		panel2 = new MyPanel("确定", "取消", "标签,我们在面板2中");
		button = new JButton("我不是在面板中的按钮");
		add(panel1);
		add(panel2);
		add(button);
		setSize(300, 200);
	}
}

例5.5应用程序设有五个标签、分别放于窗口的东、西、南、北和中五个区域。

import javax.swing.*;
import java.awt.*;

public class Example5_5 {
	public static void main(String args[]) {
		JLabel label1, label2, label3, label4, label5;
		JFrame mw = new JFrame("我是一个窗口");
		mw.setSize(250, 200);
		Container con = mw.getContentPane();
		con.setLayout(new BorderLayout());
		label1 = new JLabel("东标签");
		label2 = new JLabel("南标签", JLabel.CENTER);
		label3 = new JLabel("西标签");
		label4 = new JLabel("北标签", JLabel.CENTER);
		label5 = new JLabel("中标签", JLabel.CENTER);
		con.add(label1, "East");
		con.add(label2, "South");
		con.add(label3, "West");
		con.add(label4, "North");
		con.add(label5, "Center");
		mw.setVisible(true);
	}
}

例5.6小应用程序先将若干个按钮和若干个标签放入JPanel中,然后将JPanel放入JScrollPane中,最后,将JScrollPane放入小程序的窗口中。程序所创建的JScrollPane总是带水平和垂直滚动条,滚动面板的可视范围小于面板的实际要求,可以移动滚动条的滑块显示面板原先不在可视范围内的区域。

class MyWindow extends JFrame {
	public MyWindow(int w, int h) {
		setTitle("滚动面板实例");
		Container con = getContentPane();
		con.setPreferredSize(new Dimension(w, h));
		con.setLayout(new BorderLayout());
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(6, 6));
		for (int i = 0; i < 6; i++) {
			p.add(new JLabel());
			for (int j = 1; j <= 2; j++) {
				p.add(new JButton("按钮" + (2 * i + j)));
				p.add(new JLabel("标签" + (2 * i + j)));
			}
			p.add(new JLabel());
		}
		p.setBackground(Color.blue);
		p.setPreferredSize(new Dimension(w + 60, h + 60));
		JScrollPane scrollPane = new JScrollPane(p);
		scrollPane.setPreferredSize(new Dimension(w - 60, h - 60));
		add(scrollPane, BorderLayout.CENTER);// 小程序添加滚动面板
		setVisible(true);
		pack();
	}
}

class ScrollPane extends JScrollPane {
	public ScrollPane(Component p) {
		super(p);
		setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		setVerticalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	}
}

public class Example5_6 extends Applet {
	MyWindow myWindow;

	public void init() {
		myWindow = new MyWindow(400, 350);
	}
}

例5.7小应用程序使用CardLayout布局,面板容器p使用CardLayout布局策略设置10个标签组件。窗口设有4个按钮,分别负责显示p的第一个组件、最后一个组件、当前组件的前一个组件和当前组件的后一个组件。

class MyPanel extends JPanel {
	int x;
	JLabel label;

	MyPanel(int a) {
		x = a;
		getSize();
		label = new JLabel("我是第" + x + "个标签");
		add(label);
	}

	public Dimension getPreferredSize() {
		return new Dimension(200, 50);
	}
}

public class Example5_7 extends Applet implements ActionListener {
	CardLayout mycard;
	MyPanel myPanel[];
	JPanel p;

	private void addButton(JPanel pan, String butName, ActionListener listener) {
		JButton aButton = new JButton(butName);
		aButton.setActionCommand(butName);
		aButton.addActionListener(listener);
		pan.add(aButton);
	}

	public void init() {
		setLayout(new BorderLayout());// 小程序的布局是边界布局
		mycard = new CardLayout();
		this.setSize(400, 150);
		p = new JPanel();
		p.setLayout(mycard);// p的布局设置为卡片式布局
		myPanel = new MyPanel[10];
		for (int i = 0; i < 10; i++) {
			myPanel[i] = new MyPanel(i + 1);
			p.add("A" + i, myPanel[i]);
		}
		JPanel p2 = new JPanel();
		addButton(p2, "第一个", this);
		addButton(p2, "最后一个", this);
		addButton(p2, "前一个", this);
		addButton(p2, "后一个", this);
		add(p, "Center");
		add(p2, "South");
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("第一个")) {
			mycard.first(p);
		} else if (e.getActionCommand().equals("最后一个")) {
			mycard.last(p);
		} else if (e.getActionCommand().equals("前一个")) {
			mycard.previous(p);
		} else if (e.getActionCommand().equals("第后一个")) {
			mycard.next(p);
		}
	}
}

例5.8小应用程序有两个文本框。

public class Example5_8 extends Applet {
	static JTextField text1, text2;
	Sqr s = new Sqr();

	public void init() {
		text1 = new JTextField(10);
		text2 = new JTextField(10);
		add(text1);
		add(text2);
		text1.addActionListener(s);// 类Sqr的实例s作为text1的监视器
	}
}

class Sqr implements ActionListener {
	public void actionPerformed(ActionEvent e)// 实现接口ActionEvent
	{
		if (e.getSource() == Example5_8.text1) {
			long n = Long.parseLong(Example5_8.text1.getText());// 将text1的文本转换成long型数据。
			Example5_8.text2.setText(String.valueOf(n * n));// 将n*n转化成字符串。
		} else {

		}
	}
}

例5.9小应用程序设置一个文本区、一个文本框和两个按钮。

import java.util.*;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Example5_9 extends Applet implements ActionListener {
	JTextArea textA;
	JTextField textF;
	JButton b1, b2;

	public void init() {
		setSize(250, 150);
		textA = new JTextArea("", 5, 10);
		textA.setBackground(Color.cyan);
		textF = new JTextField("", 10);
		textF.setBackground(Color.pink);
		b1 = new JButton("求 和");
		b2 = new JButton("重新开始");
		textF.setEditable(false);
		b1.addActionListener(this);
		b2.addActionListener(this);
		add(textA);
		add(textF);
		add(b1);
		add(b2);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) {
			String s = textA.getText();
			StringTokenizer tokens = new StringTokenizer(s);// 使用默认的分隔符激集合:空格、换行、Tab符和回车做分隔符。
			int n = tokens.countTokens(), sum = 0, i;
			for (i = 0; i <= n - 1; i++) {
				String temp = tokens.nextToken();// 从文本区取下一个数据。
				sum += Integer.parseInt(temp);
			}
			textF.setText("" + sum);
		} else if (e.getSource() == b2) {
			textA.setText(null);
			textF.setText(null);
		}
	}
}

例5.10小程序计算从起始整数到终止整数中是因子倍数的所有数。

import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Panel1 extends JPanel {
	JTextField text1, text2, text3;

	Panel1()// 构造方法。当创建Panel对象时,Panel被初始化为有三个标签。
	{
		// 三个文本框,布局为GridLayout(1,6)。
		text1 = new JTextField(10);
		text2 = new JTextField(10);
		text3 = new JTextField(10);
		setLayout(new GridLayout(1, 6));
		add(new JLabel("起始数", JLabel.RIGHT));
		add(text1);
		add(new JLabel("终止数", JLabel.RIGHT));
		add(text2);
		add(new JLabel("因子", JLabel.RIGHT));
		add(text3);
	}
}

class Panel2 extends JPanel// 扩展Panel类
{
	JTextArea text;
	JButton button;

	Panel2()// 构造方法。当创建Panel对象时,Panel被初始化为有一个标签。
	{
		// 一个文本框,布局为GridLayout(1,4)
		text = new JTextArea(4, 10);
		text.setLineWrap(true);
		JScrollPane jsp = new JScrollPane(text);
		button = new JButton("开始计算");
		setLayout(new GridLayout(1, 4));
		add(new JLabel("计算结果:", JLabel.RIGHT));
		add(jsp);
		add(new Label());
		add(button);
	}
}

public class Example5_10 extends Applet implements ActionListener {
	Panel1 panel1;
	Panel2 panel2;

	public void init() {
		setLayout(new GridLayout(3, 1));
		setSize(400, 200);
		panel1 = new Panel1();
		panel2 = new Panel2();
		add(new JLabel("计算从起始数到终止数是因子倍数的数", JLabel.CENTER));
		add(panel1);
		add(panel2);
		(panel2.button).addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == (panel2.button)) {
			long n1, n2, f, count = 0;
			n1 = Long.parseLong(panel1.text1.getText());
			n2 = Long.parseLong(panel1.text2.getText());
			f = Long.parseLong(panel1.text3.getText());
			for (long i = n1; i <= n2; i++) {
				if (i % f == 0) {
					panel2.text.append(String.valueOf(i) + " ");
				}
			}
		}
	}
}
原文地址:https://www.cnblogs.com/denggelin/p/6238157.html