当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Swing 键盘练习

发表于: 2014-01-09   作者:不懂事的小屁孩   来源:转载   浏览:
摘要: 在swing界面中写一个键盘,使用前记得放置背景图片 import java.awt.*; import javax.swing.*; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class fra
在swing界面中写一个键盘,使用前记得放置背景图片

import java.awt.*;

import javax.swing.*;

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

public class framebg {

	private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	private static JFrame frame;
	private static int screen_wid;
	private static int screen_hei;
	private static Container cp ;
	private static JTextField username = new JTextField();
	private static JTextField password = new JTextField();
	private static JButton[] but = new JButton[76];
	public framebg() {
	}

	public static void main(String[] args) {
		initialize();
	}

	private static void initialize() {
		frame = new JFrame();
		frame.setUndecorated(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		screen_wid = (int) screenSize.getWidth();
		screen_hei = (int) screenSize.getHeight();

		ImageIcon img = new ImageIcon("img/login1.jpg");
		JLabel imgLabel = new JLabel(img);
		frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
		imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
		cp = frame.getContentPane();
		((JPanel) cp).setOpaque(true);

		frame.setSize(screen_wid, screen_hei);
		frame.setVisible(true);

		JLabel titlelable = new JLabel();
		ImageIcon title_icon = new ImageIcon("img/jiudao1.jpg");
		titlelable.setIcon(title_icon);
		titlelable.setBounds(57, 118, 591, 216);
		cp.add(titlelable);

		JPanel login_panel = new JPanel();
		login_panel.setOpaque(true);
		login_panel.setBounds(675, 118, 311, 216);
		login_panel.setLayout(null);


		username.setBounds(105, 45, 140, 29);
		login_panel.add(username);

		password.setBounds(105, 75, 140, 29);
		login_panel.add(password);

		JButton login_button = new JButton("登陆");
		login_button.setFont(new Font("微软雅黑", Font.BOLD, 14));
		login_button.setBounds(10, 132, 63, 36);
		login_button.setBackground(new Color(240,255,255));
		login_panel.add(login_button);

		JButton exit_button = new JButton("退出");
		exit_button.setFont(new Font("微软雅黑", Font.BOLD, 14));
		exit_button.setBounds(97, 132, 63, 36);
		exit_button.setBackground(new Color(240,255,255));
		login_panel.add(exit_button);

		JButton update_button = new JButton("更新");
		update_button.setFont(new Font("微软雅黑", Font.BOLD, 14));
		update_button.setBounds(182, 132, 63, 36);
		update_button.setBackground(new Color(46, 139, 87));
		login_panel.add(update_button);
		cp.add(login_panel);

		loadkeyboard();
	}

	private static void loadkeyboard() {
		JPanel panel = new JPanel();
		panel.setOpaque(true);
		panel.setBounds(57, 359, 710, 216);
		panel.setLayout(null);

		but[0] = new JButton("`");
		but[0].setBounds(5, 5, 41, 33);
		//but[0].setEnabled(false);
		panel.add(but[0]);

		for(int i =1;i<10;i++){
			but[i] = new JButton(i+"");
			but[i].setBounds(49+44*(i-1), 5, 41, 33);
			final String text = but[i].getText();
			but[i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					username.setText(username.getText()+text);
				}
			});
			panel.add(but[i]);
		}

		but[10] = new JButton("0");
		but[10].setBounds(451, 5, 41, 33);
		but[10].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"0");
			}
		});
		panel.add(but[10]);

		but[11] = new JButton("-");
		but[11].setBounds(495, 5, 41, 33);

		but[11].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"-");
			}
		});
		panel.add(but[11]);

		but[12] = new JButton("+=");
		but[12].setBounds(541, 5, 52, 33);
		but[12].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"+=");
			}
		});
		panel.add(but[12]);

		but[13] = new JButton("←Backspace");
		but[13].setBounds(599, 5, 111, 33);
		but[13].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(username.getText()==null ||username.getText().length()<=0){
					username.setText("");
				}else{
					username.setText(username.getText().substring(0, username.
							getText().length()-1));
				}
			}
		});
		panel.add(but[13]);

		but[14] = new JButton("Tab");
		but[14].setBounds(5, 44, 56, 33);
		but[14].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[14]);

		but[15] = new JButton("q");
		but[15].setBounds(64, 44, 44, 33);
		but[15].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"q");
			}
		});
		panel.add(but[15]);

		but[16] = new JButton("w");
		but[16].setBounds(114, 44, 44, 33);
		but[16].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"w");
			}
		});
		panel.add(but[16]);

		but[17] = new JButton("e");
		but[17].setBounds(161, 44, 41, 33);
		but[17].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"e");
			}
		});
		panel.add(but[17]);

		but[18] = new JButton("r");
		but[18].setBounds(208, 44, 41, 33);
		but[18].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"r");
			}
		});
		panel.add(but[18]);

		but[19] = new JButton("t");
		but[19].setBounds(255, 44, 41, 33);
		but[19].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"t");
			}
		});
		panel.add(but[19]);

		but[20] = new JButton("y");
		but[20].setBounds(302, 44, 41, 33);
		but[20].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"y");
			}
		});
		panel.add(but[20]);

		but[21] = new JButton("u");
		but[21].setBounds(349, 44, 41, 33);
		but[21].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"u");
			}
		});
		panel.add(but[21]);

		but[22] = new JButton("i");
		but[22].setBounds(396, 44, 41, 33);
		but[22].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"i");
			}
		});
		panel.add(but[22]);

		but[23] = new JButton("o");
		but[23].setBounds(443, 44, 42, 33);
		but[23].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"o");
			}
		});
		panel.add(but[23]);

		but[24] = new JButton("p");
		but[24].setBounds(489, 44, 42, 33);
		but[24].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"p");
			}
		});
		panel.add(but[24]);

		but[25] = new JButton("[");
		but[25].setBounds(537, 44, 42, 33);
		but[25].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"[");
			}
		});
		panel.add(but[25]);

		but[26] = new JButton("]");
		but[26].setBounds(585, 44, 42, 33);
		but[26].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"]");
			}
		});
		panel.add(but[26]);

		but[27] = new JButton("\\");
		but[27].setBounds(633, 44, 77, 33);
		but[27].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"\\");
			}
		});
		panel.add(but[27]);

		but[28] = new JButton("CapsLock");
		but[28].setBounds(5, 83, 91, 33);
		but[28].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[28]);

		but[29] = new JButton("a");
		but[29].setBounds(102, 83, 41, 33);
		but[29].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"a");
			}
		});
		panel.add(but[29]);

		but[30] = new JButton("s");
		but[30].setBounds(149, 83, 41, 33);
		but[30].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"s");
			}
		});
		panel.add(but[30]);

		but[31] = new JButton("d");
		but[31].setBounds(196, 83, 42, 33);
		but[31].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"d");
			}
		});
		panel.add(but[31]);

		but[32] = new JButton("f");
		but[32].setBounds(243, 83, 42, 33);
		but[32].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"f");
			}
		});
		panel.add(but[32]);

		but[33] = new JButton("g");
		but[33].setBounds(290, 83, 42, 33);
		but[33].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"g");
			}
		});
		panel.add(but[33]);

		but[34] = new JButton("h");
		but[34].setBounds(337, 83, 42, 33);
		but[34].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"h");
			}
		});
		panel.add(but[34]);

		but[35] = new JButton("j");
		but[35].setBounds(384, 83, 42, 33);
		but[35].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"j");
			}
		});
		panel.add(but[35]);

		but[36] = new JButton("k");
		but[36].setBounds(431, 83, 42, 33);
		but[36].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"k");
			}
		});
		panel.add(but[36]);

		but[37] = new JButton("l");
		but[37].setBounds(478, 83, 42, 33);
		but[37].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"l");
			}
		});
		panel.add(but[37]);

		but[38] = new JButton(";");
		but[38].setBounds(525, 83, 42, 33);
		but[38].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+";");
			}
		});
		panel.add(but[38]);

		but[39] = new JButton("\"");
		but[39].setBounds(572, 83, 42, 33);
		but[39].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"\"");
			}
		});
		panel.add(but[39]);

		but[40] = new JButton("Enter");
		but[40].setBounds(619, 83, 91, 33);
		but[40].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[40]);

		but[41] = new JButton("Shift");
		but[41].setBounds(5, 125, 67, 33);
		but[41].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[41]);

		but[42] = new JButton("z");
		but[42].setBounds(78, 125, 45, 33);
		but[42].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"z");
			}
		});
		panel.add(but[42]);

		but[43] = new JButton("x");
		but[43].setBounds(129, 125, 45, 33);
		but[43].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"x");
			}
		});
		panel.add(but[43]);

		but[44] = new JButton("c");
		but[44].setBounds(181, 125, 45, 33);
		but[44].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"c");
			}
		});
		panel.add(but[44]);

		but[45] = new JButton("v");
		but[45].setBounds(229, 125, 45, 33);
		but[45].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"v");
			}
		});
		panel.add(but[45]);

		but[46] = new JButton("b");
		but[46].setBounds(279, 125, 46, 33);
		but[46].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"b");
			}
		});
		panel.add(but[46]);

		but[47] = new JButton("n");
		but[47].setBounds(330, 125, 47, 33);
		but[47].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"n");
			}
		});
		panel.add(but[47]);

		but[48] = new JButton("m");
		but[48].setBounds(381, 125, 48, 33);
		but[48].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"m");
			}
		});
		panel.add(but[48]);

		but[49] = new JButton(",");
		but[49].setBounds(432, 125, 49, 33);
		but[49].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+",");
			}
		});
		panel.add(but[49]);

		but[50] = new JButton(".");
		but[50].setBounds(483, 125, 50, 33);
		but[50].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+".");
			}
		});
		panel.add(but[50]);

		but[51] = new JButton("/");
		but[51].setBounds(533, 125, 45, 33);
		but[51].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"/");
			}
		});
		panel.add(but[51]);

		but[52] = new JButton("Shift");
		but[52].setBounds(581, 125, 129, 33);
		but[52].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[52]);

		but[53] = new JButton("Ctrl");
		but[53].setBounds(5, 164, 67, 33);
		but[53].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[53]);

		but[54] = new JButton("Win");
		but[54].setBounds(78, 164, 67, 33);
		but[54].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[54]);

		but[55] = new JButton("Alt");
		but[55].setBounds(153, 164, 67, 33);
		but[55].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[55]);

		but[56] = new JButton("");
		but[56].setBounds(225, 164, 329, 33);
		but[56].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+" ");
			}
		});
		panel.add(but[56]);

		but[57] = new JButton("Alt");
		but[57].setBounds(560, 164, 67, 33);
		but[57].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[57]);

		but[58] = new JButton("Ctrl");
		but[58].setBounds(635, 164, 75, 33);
		but[58].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(but[58]);

		cp.add(panel);

		JPanel panel_num = new JPanel();
		panel_num.setOpaque(true);
		panel_num.setBounds(777, 359, 209, 216);
		panel_num.setLayout(null);
		cp.add(panel_num);

		but[59] = new JButton("Num");
		but[59].setBounds(5, 5, 63, 33);
		but[59].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel_num.add(but[59]);

		but[60] = new JButton("/");
		but[60].setBounds(74, 5, 40, 33);
		but[60].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"/");
			}
		});
		panel_num.add(but[60]);

		but[61] = new JButton("*");
		but[61].setBounds(119, 5, 40, 33);
		but[61].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"*");
			}
		});
		panel_num.add(but[61]);

		but[62] = new JButton("-");
		but[62].setBounds(160, 5, 43, 68);
		but[62].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"-");
			}
		});
		panel_num.add(but[62]);

		for(int i=7;i<10;i++){
			but[56+i] = new JButton(i+"");
			but[56+i].setBounds(5+(i-7)*50, 44, 44, 33);
			final String text = i+"";
			but[56+i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					username.setText(username.getText()+text);
				}
			});
			panel_num.add(but[56+i]);
		}
		for(int i=4;i<7;i++){
			but[62+i] = new JButton(i+"");
			but[62+i].setBounds(5+(i-4)*50, 84, 44, 33);
			final String text = i+"";
			but[62+i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					username.setText(username.getText()+text);
				}
			});
			panel_num.add(but[62+i]);
		}
		for(int i=1;i<4;i++){
			but[68+i] = new JButton(i+"");
			but[68+i].setBounds(5+(i-1)*50, 124, 44, 33);
			final String text = i+"";
			but[68+i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					username.setText(username.getText()+text);
				}
			});
			panel_num.add(but[68+i]);
		}

		but[72] = new JButton("+");
		but[72].setBounds(160, 84, 43, 72);
		but[72].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"+");
			}
		});
		panel_num.add(but[72]);

		but[73] = new JButton("0");
		but[73].setBounds(5, 162, 60, 33);
		but[73].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"0");
			}
		});
		panel_num.add(but[73]);
		but[74] = new JButton(".");
		but[74].setBounds(72, 162, 44, 33);
		but[74].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+".");
			}
		});
		panel_num.add(but[74]);
		but[75] = new JButton("Enter");
		but[75].setBounds(122, 162, 81, 33);
		but[75].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText(username.getText()+"Enter");
			}
		});
		panel_num.add(but[75]);

		JPanel panel_ka = new JPanel();
		panel_ka.setOpaque(true);
		panel_ka.setBounds(57, 359, 710, 216);
		panel_ka.setLayout(null);
		cp.add(panel_ka);
	}

}

Swing 键盘练习

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
只是一个简单的界面,使用JDBC来完成数据存储和读取的,而没有使用JPA,netbeans能够使用JPA来直接
2 Swing
控制布局 BorderLayout Applet在缺省的情况下是BorderLayout,它对加入组件的处理方式是, 放置在中
3 swing
摘自: http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html Writing Swing Applicat
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespa
5 练习
火柴游戏的功能时通过用户与计算机相互对拿火柴的游戏程序,通过与计算机的对拿来训练人脑的逻辑思
6 练习
/* * 程序的版权和版本声明部分: * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生
选项卡是SWING开发中用的比较多的组件之一,SWING中的选项卡通过JTabbedPane来实现。 它允许用户通
Swing是什么 Swing属于JFC(Java Foundation Classes)的一部分。JFC包含了AWT,Swing,辅助功能API,Jav
入门推荐 http://java.sun.com/docs/books/tutorial/uiswing/index.html -------------------------
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号