java实训记录之一
来源:互联网 发布:现在哪个网络购物好 编辑:程序博客网 时间:2024/06/16 07:24
使用Java GUI包编写图形用户界面。
package com.you.gui;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JToolBar;public class GTest1 extends JFrame {private static final long serialVersionUID = 1L;private JCheckBox jb1 = new JCheckBox("音乐",true);//复选框private JCheckBox jb2 = new JCheckBox("美术");private JCheckBox jb3 = new JCheckBox("体育");private JRadioButton jr1 = new JRadioButton("语文");//单选框private JRadioButton jr2 = new JRadioButton("数学");private JRadioButton jr3 = new JRadioButton("英语");private Student[] stu = new Student[] {new Student("张三", 18),new Student("李四", 28),new Student("王五", 38)};private JComboBox jcb = new JComboBox(stu);//下拉列表private JList jl = new JList(stu);private JTextArea jta = new JTextArea(10, 10);//滚动条private JToolBar jtb = new JToolBar(JToolBar.HORIZONTAL);//工具栏private JButton b1 = new JButton(new ImageIcon(GTest1.class.getResource("/res/b1.png")));private JButton b2 = new JButton(new ImageIcon(GTest1.class.getResource("/res/b2.png")));private JButton b3 = new JButton(new ImageIcon(GTest1.class.getResource("/res/b3.png")));private JMenuBar jb = new JMenuBar();//菜单栏private JMenu jm1 = new JMenu("File");private JMenu jm2 = new JMenu("Edit");private JMenu jm3 = new JMenu("Source");private JMenuItem jt1 = new JMenuItem("Open");//菜单中的实现项private JMenuItem jt2 = new JMenuItem("Save");private JMenuItem jt3 = new JMenuItem("Quit");private JPanel jp = new JPanel();public GTest1() {this.setSize(500, 550);this.setLocation(100, 100);jp.add(jb1);jp.add(jb2);jp.add(jb3);jp.add(jr1);jp.add(jr2);jp.add(jr3);jp.add(jcb);jp.add(jl);jp.add(new JScrollPane(jta));jtb.add(b1);jtb.add(b2);jtb.add(b3);jtb.setFloatable(false);this.add(jtb,"North");this.add(jp);jm1.add(jt1);jm1.add(jt2);jm1.add(jt3);jb.add(jm1);jb.add(jm2);jb.add(jm3);this.setJMenuBar(jb);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }public static void main(String[] args) {new GTest1();}}class Student {private String name;private int age;public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return name;}}运行结果:
table代码为:
package com.you.gui;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;public class GTable extends JFrame {Object[][] studs = new Object[][] {{1, new Student("张三",18),"男"},{2, new Student("李四",28),"女"},{3, new Student("王五",38),"男"}};Object[] title = new Object[] {"id", "name", "sex"};public GTable() {super("表格示例");this.setSize(300, 350);this.setLocation(100, 100);this.add(new JScrollPane(new JTable(studs, title)));this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {new GTable();}}运行结果为:
一个简单的登录窗口的实现:
package com.you.gui;import java.awt.Button;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.Box;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class Login extends JFrame{private static final long serialVersionUID = 1L;private Box container = Box.createVerticalBox();private Box b1 = Box.createHorizontalBox();private Box b2 = Box.createHorizontalBox();private Box b3 = Box.createHorizontalBox();private JLabel luser = new JLabel("用户名:");private JLabel lpwd = new JLabel("密 码:");private JTextField user = new JTextField(10);private JPasswordField pwd = new JPasswordField(10);private Button ok = new Button("登录");private Button cancel = new Button("注册");public Login() {super("用户登录");this.setSize(350, 300);Toolkit tool = this.getToolkit();this.setIconImage(tool.createImage(this.getClass().getResource("/res/1.jpg")));Dimension dtool = tool.getScreenSize();this.setLocation((dtool.width-350)/2, (dtool.height-300)/2-100);b1.add(Box.createHorizontalStrut(50));b1.add(luser);b1.add(Box.createHorizontalStrut(10));b1.add(user);b1.add(Box.createHorizontalStrut(50));b2.add(Box.createHorizontalStrut(50));b2.add(lpwd);b2.add(Box.createHorizontalStrut(10));b2.add(pwd);b2.add(Box.createHorizontalStrut(50));b3.add(Box.createHorizontalStrut(80));b3.add(ok);b3.add(Box.createHorizontalStrut(10));b3.add(cancel);b3.add(Box.createHorizontalStrut(80));container.add(Box.createVerticalStrut(80));container.add(b1);container.add(Box.createVerticalStrut(10));container.add(b2);container.add(Box.createVerticalStrut(10));container.add(b3);container.add(Box.createVerticalStrut(80));this.add(container);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {new Login();}}
- java实训记录之一
- java cache 学习记录之一
- java实训项目记录
- Fedora10使用记录之一
- NxOgre学习记录之一
- uboot移植记录之一
- uboot移植记录之一
- uboot移植记录之一
- 编程调试记录之一
- oracle学习记录之一
- uboot移植记录之一
- Android Framework 记录之一
- Android Framework 记录之一
- Android Framework 记录之一
- Android Framework 记录之一
- Android Framework 记录之一
- Android Framework 记录之一
- Android Framework 记录之一
- (分组背包)ACboy needs your help(hdu1712)
- 表,栈,队列程序代码片段
- (百例编程)45.真分数分解为埃及分数
- POJ3261 Milk Patterns,后缀数组, 出现k次的重复子串
- 微软正式发布Windows 8 Release Preview (下载地址)
- java实训记录之一
- 进行Android单元测试
- netty初步使用2
- (百例编程)46.列出真分数序列
- Windows Server 2008 R2 更新后远程桌面无法连接
- (百例编程)47.计算分数的精确值
- Spring AfterReturningAdvice Demo
- (百例编程)48.新娘和新郎
- (百例编程)49.委派任务