第十八章,窗口例题代码

package 例题;

import java.awt.Container;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class 例题1 {

	public static void main(String[] args) {
		JFrame jf = new JFrame();//创建窗体对象
		jf.setTitle("创建一个JFramen窗体");//设置窗体标题
		Container container = jf.getContentPane();//获取主容器
		JLabel jl = new JLabel("这是一个JFrame窗体");//一个文本标签
		jl.setHorizontalAlignment(SwingConstants.CENTER);//让标签文字居中
		container.add(jl);//将标签添加到主容器中
		jf.setSize(300,150);//设置窗体宽高
		jf.setLocation(500,320);//设置窗体在屏幕出现的位置
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体则停止运行
		jf.setVisible(true);//让窗体展示出来
	}

}
package 例题;

import java.awt.Container;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

class MyJDialog extends JDialog{
	public MyJDialog(例题2 frame) {
		super(frame,"第一个JDialog窗体", true);
		Container container = getContentPane();
		container.add(new JLabel("这是一个对话框"));
		setBounds(500, 320, 100, 100);
	}
}
public class 例题2 extends JFrame{
	public 例题2() {
		Container container = getContentPane();
		container.setLayout(null);
		JButton bl = new JButton("弹出对话框");
		bl.setBounds(50, 50, 100, 21);
		bl.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				MyJDialog di = new MyJDialog(例题2.this);
				di.setVisible(true);
				
			}
		});
		container.add(bl);
		setSize(200,200);
		setLocation(500, 320);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setVisible(true);
	}
	public static void main(String[] args) {
		new 例题2();

	}

}
package 例题;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class 例题3 {

	public static void main(String[] args) {
		Object o[] = {
			new JButton("是的"),new JButton("再想想")	
		};
		Icon i = new ImageIcon("");
		JOptionPane.showOptionDialog(null, "你做好准备了吗?", 
				"注意了!",
				JOptionPane.DEFAULT_OPTION,
				JOptionPane.DEFAULT_OPTION, 
				i, o, null);

	}

}
package 例题;

import javax.swing.JOptionPane;

public class 例题4 {

	public static void main(String[] args) {
		int ar = JOptionPane.showConfirmDialog(null, "你确定离开吗?", "标题", 
				JOptionPane.YES_NO_CANCEL_OPTION);

	}

}
package 例题;

import javax.swing.JOptionPane;

public class 例题5 {

	public static void main(String[] args) {
		//
		String name = JOptionPane.showInputDialog(null, "请输入名字");
	}

}
package 例题;

import javax.swing.JOptionPane;

public class 例题6 {

	public static void main(String[] args) {
		// 
		JOptionPane.showConfirmDialog(null, "您与服务器断开了连接!", "发生错误", 
				JOptionPane.ERROR_MESSAGE);

	}

}
package 例题;

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class 例题7 extends JFrame{
	public 例题7() {
		setTitle("本窗体使用绝对布局");
		setLayout(null);
		setBounds(0, 0, 300, 150);
		Container container = getContentPane();
		JButton b1 = new JButton("按钮1");
		JButton b2 = new JButton("按钮2");
		b1.setBounds(10, 30, 80, 30);
		b2.setBounds(60, 70, 100, 20);
		container.add(b1);
		container.add(b2);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}
	public static void main(String[] args) {
		// 
		new 例题7();

	}

}
package 例题;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class 例题8 extends JFrame{
public 例题8() {
	setTitle("本窗口使用流布局管理器");
	Container c = getContentPane();
	setLayout(new FlowLayout(FlowLayout.RIGHT,10, 10));
	for(int i = 0; i < 10; i++) {
		c.add(new JButton("button" + i));
	}
	setSize(300,200);
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	setVisible(true);
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new 例题8();
	}

}
package 例题;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class 例题9 extends JFrame{
	public 例题9() {
		setTitle("这个窗口使用边界布局");
		Container c = getContentPane();//获取主入器
		setLayout(new BorderLayout());//使用边界布局管理器
		JButton centerBtn = new JButton("中");
		JButton northBtn = new JButton("北");
		JButton southBtn = new JButton("南");
		JButton westBtn = new JButton("西");
		JButton eastBtn = new JButton("东");
		c.add(centerBtn, BorderLayout.CENTER);
		c.add(northBtn, BorderLayout.NORTH);
		c.add(southBtn, BorderLayout.SOUTH);
		c.add(westBtn, BorderLayout.WEST);
		c.add(eastBtn, BorderLayout.EAST);
		setSize(350,200);
		setLocation(500,340);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		
		new 例题9();
	}

}
package 例题;

import java.awt.Container;
import java.awt.GridLayout;
import java.nio.channels.WritableByteChannel;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class 例题10 extends JFrame{
	public 例题10() {
		Container c = getContentPane();//获取主容器
		//
		setLayout(new GridLayout(7, 3, 5, 5));
		for(int i = 0; i < 20; i++) {
			c.add(new JButton("button" + i));
			
		}
		setSize(300, 300);
		setLocation(500,340);
		setTitle("这是一个使用网格布局管理器是窗体");
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		
		new 例题10();
	}

}
package 例题;

import java.awt.*;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class 例题11 extends JFrame{
	public 例题11() {
		Container c = getContentPane();
		c.setLayout(new GridLayout(2, 2, 10, 10));
		JPanel p1 = new JPanel(new GridLayout(1, 4, 10, 10));
		JPanel p2 = new JPanel(new BorderLayout());
		JPanel p3 = new JPanel(new GridLayout(1, 2, 10, 10));
		JPanel p4 = new JPanel(new GridLayout(2, 1, 10, 10));
		
		p1.setBorder(BorderFactory.createTitledBorder("面板1"));
		p2.setBorder(BorderFactory.createTitledBorder("面板2"));
		p3.setBorder(BorderFactory.createTitledBorder("面板3"));
		p4.setBorder(BorderFactory.createTitledBorder("面板4"));

		p1.add(new JButton("b1"));
		p1.add(new JButton("b1"));
		p1.add(new JButton("b1"));
		p1.add(new JButton("b1"));
		
		p2.add(new JButton("b2"), BorderLayout.WEST);
		p2.add(new JButton("b2"), BorderLayout.EAST);
		p2.add(new JButton("b2"), BorderLayout.NORTH);
		p2.add(new JButton("b2"), BorderLayout.SOUTH);
		p2.add(new JButton("b2"), BorderLayout.CENTER);
		
		p3.add(new JButton("b3"));
		p3.add(new JButton("b3"));
		
		p4.add(new JButton("b4"));
		p4.add(new JButton("b4"));
		
		c.add(p1);
		c.add(p2);
		c.add(p3);
		c.add(p4);
		
		setTitle("在这个窗口使用了面板");
		setSize(500,300);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	public static void main(String[] args) {
		例题11 l = new 例题11();
		l.setVisible(true);
	}

}
package 例题;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class 例题12 extends JFrame{
public 例题12() {
	Container c = getContentPane();
	JTextArea ta = new JTextArea(20,50);
	JScrollPane sp = new JScrollPane(ta);
	c.add(sp);
	setTitle("带滚动条的文字编译器");
	setSize(400, 200);
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	
}
	public static void main(String[] args) {
		// 
		例题12 l = new 例题12();
		l.setVisible(true);

	}

}
package 例题;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

public class 例题13 extends JFrame{
public 例题13() {
	Container c = getContentPane();
	JLabel jl = new JLabel("这是一个JFrame窗体");
	c.add(jl);
	setSize(200, 100);
	setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	setVisible(true);
	
}
	public static void main(String[] args) {
	new 例题13();	
	}

}
package 例题;

import java.awt.Container;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class l例题14 extends JFrame{
	public l例题14() {
		Container container = getContentPane();
		JLabel jl = new JLabel();
		URL url = l例题14.class.getResource("fi.png");
		Icon icon = new ImageIcon(url);
		jl.setIcon(icon);
		jl.setHorizontalAlignment(SwingConstants.CENTER);
		jl.setOpaque(true);
		container.add(jl);
		setSize(300,200);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new l例题14();
	}

}
package 例题;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;

public class 例题15 extends JFrame{
private static final Color Color = null;
private static final int RED = 0;
public 例题15() {
	// 
	Icon ic = new ImageIcon("fi.png");
	setLayout(new GridLayout(3, 2, 5, 5));
	Container c = getContentPane();
	JButton btn[] = new JButton[6];
	for(int i = 0; i < btn.length; i++) {
		btn[i] = new JButton();
		c.add(btn[i]);
	}
	btn[0].setText("不可用");
	btn[0].setEnabled(false);
	btn[1].setText("有背景色");
	btn[1].setBackground(Color.YELLOW);
	btn[2].setText("无边框");
	btn[2].setBorderPainted(false);
	btn[3].setText("有边框");
	btn[3].setBorder(BorderFactory.createLineBorder(Color, RED));
	btn[4].setIcon(ic);
	btn[4].setToolTipText("图片按钮");
	btn[5].setText("可点击");
	btn[5].addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(例题15.this, "点击按钮");			
		}
	});
	
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setVisible(true);
	setTitle("创建不同样式的按钮");
	setBounds(100, 100, 400, 200);
}
	public static void main(String[] args) {
		new 例题15();

	}

}
package 例题;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class 例题16 extends JFrame{
	public 例题16() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("单选按钮的使用");
		setBounds(100, 100, 240, 120);
		getContentPane().setLayout(null);
		JLabel jl = new JLabel("请选择性别:");
		jl.setBounds(5, 5, 120, 15);
		getContentPane().add(jl);
		JRadioButton rl = new JRadioButton("男");
		rl.setSelected(true);
		rl.setBounds(40, 30, 75, 22);
		getContentPane().add(rl);
		JRadioButton rl1 = new JRadioButton("女");
		rl1.setBounds(120, 30, 75, 22);
		getContentPane().add(rl1);
		ButtonGroup bg = new ButtonGroup();
		bg.add(rl);
		bg.add(rl1);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		例题16 l = new 例题16();
		l.setVisible(true);
	}

}
package 例题;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class 例题18 extends JFrame{
	public  例题18() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("下拉列表框的使用");
		setBounds(100, 100, 317, 147);
		getContentPane().setLayout(null);
		JLabel jl = new JLabel("请选择证件:");
		jl.setBounds(28, 14, 80, 15);
		getContentPane().add(jl);
		JComboBox jc = new JComboBox();
		jc.setBounds(110, 11, 80, 21);
		jc.addItem("身份证");
		jc.addItem("军人证");
		jc.addItem("学生证");
		jc.addItem("工作证");
		jc.setEditable(true);
		getContentPane().add(jc);
		JLabel jl1 = new JLabel("");
		jl1.setBounds(200, 10, 67, 15);
		getContentPane().add(jl1);
		JButton jb = new JButton("确定");
		jb.setBounds(200, 10, 67, 23);
		getContentPane().add(jb);
		jb.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jl1.setText("您的选择是:" + jc.getSelectedItem());
				
			}
		});
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		例题18 l = new 例题18();
		l.setVisible(true);
	}

}
package 例题;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class 例题24 extends JFrame{
	private DefaultTableModel table;
	private JTable table2;
	private JTextField field;
	private JTextField field2;
	public static void main(String[] args) {
	例题24 l = new 例题24();
	l.setVisible(true);

	}
	public 例题24() {
		setTitle("维护表格模型");
		setBounds(100, 100, 520, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JScrollPane js = new JScrollPane();
		getContentPane().add(js, BorderLayout.CENTER);
		String[] cn = {"A","B"};
		String[][] tv = {{"A1","B1"},{"A2","B2"},{"A3","B3"}};
		table = new DefaultTableModel(tv,cn);
		table2 = new JTable(table);
		
	}

}
package 例题;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class 例题25 extends JFrame{
	private JButton jb = new JButton("我是按钮,点击我");
	
	public 例题25() {
		setLayout(null);
		setSize(200, 100);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.add(jb);
		jb.setBounds(10, 10, 150, 30);
		jb.addActionListener(new jbAction());
		setVisible(true);
		setLocation(500, 350);
	}
	class jbAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			jb.setText("我被点击了");
			
		}
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new 例题25();
	}

}
package 例题;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JButton;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JTextField;

/**
 * 虚拟键盘(键盘的按下与释放)
 */
public class 例题26 extends JFrame { // 创建“键盘”类继承JFrame
	// 声明窗体中的成员组件
	private JPanel contentPane;
	private JTextField textField;
	private JButton btnQ;
	private JButton btnW;
	private JButton btnE;
	private JButton btnR;
	private JButton btnT;
	private JButton btnY;
	private JButton btnU;
	private JButton btnI;
	private JButton btnO;
	private JButton btnP;
	private JButton btnA;
	private JButton btnS;
	private JButton btnD;
	private JButton btnF;
	private JButton btnG;
	private JButton btnH;
	private JButton btnJ;
	private JButton btnK;
	private JButton btnL;
	private JButton btnZ;
	private JButton btnX;
	private JButton btnC;
	private JButton btnV;
	private JButton btnB;
	private JButton btnN;
	private JButton btnM;
	Color green = Color.GREEN;// 定义Color对象,用来表示按下键的颜色
	Color white = Color.WHITE;// 定义Color对象,用来表示释放键的颜色

	ArrayList btns = new ArrayList();// 定义一个集合,用来存储所有的按键ID
	// 自定义一个方法,用来将容器中的所有JButton组件添加到集合中

	private void addButtons() {
		for (Component cmp : contentPane.getComponents()) {// 遍历面板中的所有组件
			if (cmp instanceof JButton) {// 判断组件的类型是否为JButton类型
				btns.add((JButton) cmp);// 将JButton组件添加到集合中
			}
		}
	}

	/**
	 * 主方法
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() { // 使得Runnable中的的run()方法在the system EventQueue的指派线程中被调用
			public void run() {
				try {
					例题26 frame = new 例题26(); // 创建KeyBoard对象
					frame.setVisible(true); // 使frame可视
					frame.addButtons();// 初始化存储所有按键的集合
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * 创建JFrame窗体
	 */
	public 例题26() { // KeyBoard的构造方法
		setTitle("\u865A\u62DF\u952E\u76D8\uFF08\u6A21\u62DF\u952E\u76D8\u7684\u6309\u4E0B\u4E0E\u91CA\u653E\uFF09"); // 设置窗体题目
		setResizable(false); // 不可改变窗体宽高
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭的方式
		setBounds(100, 100, 560, 280); // 设置窗体的位置和宽高
		/**
		 * 创建JPanel面板contentPane置于JFrame窗体中,并设置面板的背景色、边距和布局
		 */
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		/**
		 * 创建按钮button置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
		 */
		btnQ = new JButton("Q");
		btnQ.setBackground(white);
		btnQ.setVerticalAlignment(SwingConstants.TOP);
		btnQ.setHorizontalAlignment(SwingConstants.LEADING);
		btnQ.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnQ.setBounds(0, 60, 47, 45);
		contentPane.add(btnQ);
		/**
		 * 创建按钮button_2置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
		 */
		btnW = new JButton("W");
		btnW.setBackground(white);
		btnW.setVerticalAlignment(SwingConstants.TOP);
		btnW.setHorizontalAlignment(SwingConstants.LEADING);
		btnW.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnW.setBounds(55, 60, 49, 45);
		contentPane.add(btnW);
		/**
		 * 创建按钮button_3置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
		 */
		btnE = new JButton("E");
		btnE.setBackground(white);
		btnE.setVerticalAlignment(SwingConstants.TOP);
		btnE.setHorizontalAlignment(SwingConstants.LEADING);
		btnE.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnE.setBounds(110, 60, 45, 45);
		contentPane.add(btnE);
		/**
		 * 创建按钮button_4置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
		 */
		btnR = new JButton("R");
		btnR.setBackground(white);
		btnR.setVerticalAlignment(SwingConstants.TOP);
		btnR.setHorizontalAlignment(SwingConstants.LEADING);
		btnR.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnR.setBounds(165, 60, 45, 45);
		contentPane.add(btnR);
		/**
		 * 创建按钮button_5置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
		 */
		btnF = new JButton("F");
		btnF.setBackground(white);
		btnF.setVerticalAlignment(SwingConstants.TOP);
		btnF.setHorizontalAlignment(SwingConstants.LEADING);
		btnF.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnF.setBounds(195, 125, 45, 45);
		contentPane.add(btnF);
		/**
		 * 创建按钮button_6置于面板contentPane中,设置按钮的背景色、位置、宽高以及按钮中的字体位置、内容、样式
		 */
		btnD = new JButton("D");
		btnD.setBackground(white);
		btnD.setVerticalAlignment(SwingConstants.TOP);
		btnD.setHorizontalAlignment(SwingConstants.LEADING);
		btnD.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnD.setBounds(137, 125, 45, 45);
		contentPane.add(btnD);

		btnT = new JButton("T");
		btnT.setVerticalAlignment(SwingConstants.TOP);
		btnT.setHorizontalAlignment(SwingConstants.LEADING);
		btnT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnT.setBackground(white);
		btnT.setBounds(220, 60, 45, 45);
		contentPane.add(btnT);

		btnY = new JButton("Y");
		btnY.setVerticalAlignment(SwingConstants.TOP);
		btnY.setHorizontalAlignment(SwingConstants.LEADING);
		btnY.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnY.setBackground(white);
		btnY.setBounds(275, 60, 45, 45);
		contentPane.add(btnY);

		btnU = new JButton("U");
		btnU.setVerticalAlignment(SwingConstants.TOP);
		btnU.setHorizontalAlignment(SwingConstants.LEADING);
		btnU.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnU.setBackground(white);
		btnU.setBounds(330, 60, 45, 45);
		contentPane.add(btnU);

		btnI = new JButton("I");
		btnI.setVerticalAlignment(SwingConstants.TOP);
		btnI.setHorizontalAlignment(SwingConstants.LEADING);
		btnI.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnI.setBackground(white);
		btnI.setBounds(385, 60, 45, 45);
		contentPane.add(btnI);

		btnO = new JButton("O");
		btnO.setVerticalAlignment(SwingConstants.TOP);
		btnO.setHorizontalAlignment(SwingConstants.LEADING);
		btnO.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnO.setBackground(white);
		btnO.setBounds(440, 60, 46, 45);
		contentPane.add(btnO);

		btnP = new JButton("P");
		btnP.setVerticalAlignment(SwingConstants.TOP);
		btnP.setHorizontalAlignment(SwingConstants.LEADING);
		btnP.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnP.setBackground(white);
		btnP.setBounds(495, 60, 45, 45);
		contentPane.add(btnP);

		btnA = new JButton("A");
		btnA.setVerticalAlignment(SwingConstants.TOP);
		btnA.setHorizontalAlignment(SwingConstants.LEADING);
		btnA.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnA.setBackground(white);
		btnA.setBounds(23, 125, 45, 45);
		contentPane.add(btnA);

		btnS = new JButton("S");
		btnS.setVerticalAlignment(SwingConstants.TOP);
		btnS.setHorizontalAlignment(SwingConstants.LEADING);
		btnS.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnS.setBackground(white);
		btnS.setBounds(82, 125, 45, 45);
		contentPane.add(btnS);

		btnG = new JButton("G");
		btnG.setVerticalAlignment(SwingConstants.TOP);
		btnG.setHorizontalAlignment(SwingConstants.LEADING);
		btnG.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnG.setBackground(white);
		btnG.setBounds(251, 125, 45, 45);
		contentPane.add(btnG);

		btnH = new JButton("H");
		btnH.setVerticalAlignment(SwingConstants.TOP);
		btnH.setHorizontalAlignment(SwingConstants.LEADING);
		btnH.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnH.setBackground(white);
		btnH.setBounds(306, 125, 45, 45);
		contentPane.add(btnH);

		btnJ = new JButton("J");
		btnJ.setVerticalAlignment(SwingConstants.TOP);
		btnJ.setHorizontalAlignment(SwingConstants.LEADING);
		btnJ.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnJ.setBackground(white);
		btnJ.setBounds(361, 125, 45, 45);
		contentPane.add(btnJ);

		btnK = new JButton("K");
		btnK.setVerticalAlignment(SwingConstants.TOP);
		btnK.setHorizontalAlignment(SwingConstants.LEADING);
		btnK.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnK.setBackground(white);
		btnK.setBounds(416, 125, 47, 45);
		contentPane.add(btnK);

		btnL = new JButton("L");
		btnL.setVerticalAlignment(SwingConstants.TOP);
		btnL.setHorizontalAlignment(SwingConstants.LEADING);
		btnL.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnL.setBackground(white);
		btnL.setBounds(471, 125, 45, 45);
		contentPane.add(btnL);

		btnZ = new JButton("Z");
		btnZ.setVerticalAlignment(SwingConstants.TOP);
		btnZ.setHorizontalAlignment(SwingConstants.LEADING);
		btnZ.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnZ.setBackground(white);
		btnZ.setBounds(39, 190, 45, 45);
		contentPane.add(btnZ);

		btnX = new JButton("X");
		btnX.setVerticalAlignment(SwingConstants.TOP);
		btnX.setHorizontalAlignment(SwingConstants.LEADING);
		btnX.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnX.setBackground(white);
		btnX.setBounds(107, 190, 45, 45);
		contentPane.add(btnX);

		btnC = new JButton("C");
		btnC.setVerticalAlignment(SwingConstants.TOP);
		btnC.setHorizontalAlignment(SwingConstants.LEADING);
		btnC.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnC.setBackground(white);
		btnC.setBounds(178, 190, 45, 45);
		contentPane.add(btnC);

		btnV = new JButton("V");
		btnV.setVerticalAlignment(SwingConstants.TOP);
		btnV.setHorizontalAlignment(SwingConstants.LEADING);
		btnV.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnV.setBackground(white);
		btnV.setBounds(250, 190, 45, 45);
		contentPane.add(btnV);

		btnB = new JButton("B");
		btnB.setVerticalAlignment(SwingConstants.TOP);
		btnB.setHorizontalAlignment(SwingConstants.LEADING);
		btnB.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnB.setBackground(white);
		btnB.setBounds(315, 190, 45, 45);
		contentPane.add(btnB);

		btnN = new JButton("N");
		btnN.setVerticalAlignment(SwingConstants.TOP);
		btnN.setHorizontalAlignment(SwingConstants.LEADING);
		btnN.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnN.setBackground(white);
		btnN.setBounds(382, 190, 47, 45);
		contentPane.add(btnN);

		btnM = new JButton("M");
		btnM.setVerticalAlignment(SwingConstants.TOP);
		btnM.setHorizontalAlignment(SwingConstants.LEADING);
		btnM.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		btnM.setBackground(white);
		btnM.setBounds(449, 190, 48, 45);
		contentPane.add(btnM);
		/**
		 * 创建面板panel置于面板contentPane中,设置面板panel的位置、宽高、TitledBorder、背景色以及布局方式(边界布局)
		 */
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "文本显示区", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBackground(Color.WHITE);
		panel.setBounds(0, 0, 540, 45);
		contentPane.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		/**
		 * 创建文本框textField置于面板panel的中间
		 */
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() { // 文本框添加键盘事件的监听
			char word;

			@Override
			public void keyPressed(KeyEvent e) { // 按键被按下时被触发
				word = e.getKeyChar();// 获取按下键表示的字符
				for (int i = 0; i < btns.size(); i++) {// 遍历存储按键ID的ArrayList集合
					// 判断按键是否与遍历到的按键的文本相同
					if (String.valueOf(word).equalsIgnoreCase(btns.get(i).getText())) {
						btns.get(i).setBackground(green);// 将指定按键颜色设置为绿色
					}
				}
			}

			@Override
			public void keyReleased(KeyEvent e) { // 按键被释放时被触发
				word = e.getKeyChar();// 获取释放键表示的字符
				for (int i = 0; i < btns.size(); i++) {// 遍历存储按键ID的ArrayList集合
					// 判断按键是否与遍历到的按键的文本相同
					if (String.valueOf(word).equalsIgnoreCase(btns.get(i).getText())) {
						btns.get(i).setBackground(white);// 将指定按键颜色设置为白色
					}
				}
			}
		});
		panel.add(textField, BorderLayout.CENTER);
		textField.setColumns(10);
	}
}
package 例题;


import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class 例题27 extends JFrame { // 继承窗体类JFrame

	public static void main(String args[]) {
		例题27 frame = new 例题27();
		frame.setVisible(true); // 设置窗体可见,默认为不可见
	}

	/**
	 * 判断按下的鼠标键,并输出相应提示
	 * 
	 * @param e 鼠标事件
	 */
	private void mouseOper(MouseEvent e) {
		int i = e.getButton();
		if(i == MouseEvent.BUTTON1) {
			System.out.println("按下的是鼠标左键");
		}
		else if(i == MouseEvent.BUTTON2) {
			System.out.println("按下的是鼠标滚轮");
		}
		else if(i == MouseEvent.BUTTON3) {
			System.out.println("按下的是鼠标右键");
		}
	}

	public 例题27() {
		super(); // 继承父类的构造方法
		setTitle("鼠标事件示例"); // 设置窗体的标题
		setBounds(100, 100, 500, 375); // 设置窗体的显示位置及大小
		// 设置窗体关闭按钮的动作为退出
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JLabel label = new JLabel();
		label.addMouseListener(new MouseListener() {

			@Override
			public void mouseEntered(MouseEvent e) {
				System.out.println("光标移入组件");
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				System.out.println("鼠标按键被按下");
				mouseOper(e);
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				System.out.println(" 鼠标按键被释放");
				mouseOper(e);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("单击了鼠标按键");
				mouseOper(e);
				int c = e.getClickCount();
				System.out.println("单击次数为" + c + "下");
			}

			@Override
			public void mouseExited(MouseEvent e) {
				System.out.println("光标移除组件");
				
			}
			
		});
		getContentPane().add(label, BorderLayout.CENTER);
	}

}

你可能感兴趣的:(java)