使用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;
}
@Override
public 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();
}
}