1.继承JFrame
2.定义组件
3.在构造函数中创建组件
4.添加组件
5.对窗体设置属性
6.显示窗体
class MyJFrame extends JFrame {
JButton jb = null;
public MyJFrame() {
jb = new JButton("hello");
this.add(jb);
this.setTitle("hello,world");
this.setSize(200, 200);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JFrame默认是BorderLayout的布局
class MyFrame1 extends JFrame {
JButton jb1 = null;
JButton jb2 = null;
JButton jb3 = null;
JButton jb4 = null;
JButton jb5 = null;
public MyFrame1() {
JButton jb1 = new JButton("east");
JButton jb2 = new JButton("south");
JButton jb3 = new JButton("west");
JButton jb4 = new JButton("north");
JButton jb5 = new JButton("center");
this.add(jb1, BorderLayout.EAST);
this.add(jb2, BorderLayout.SOUTH);
this.add(jb3, BorderLayout.WEST);
this.add(jb4, BorderLayout.NORTH);
this.add(jb5, BorderLayout.CENTER);
this.setTitle("BorderLayout");
this.setVisible(true);
this.setSize(300, 300);
this.setLocation(100, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class TestMain {
public static void main(String[] str) {
MyFrame1 frame = new MyFrame1();
}
}
class MyJFrame2 extends JFrame {
JButton jb1 = null;
JButton jb2 = null;
JButton jb3 = null;
JButton jb4 = null;
JButton jb5 = null;
public MyJFrame2() {
jb1 = new JButton("关羽");
jb2 = new JButton("张飞");
jb3 = new JButton("赵云");
jb4 = new JButton("魏延");
jb5 = new JButton("黄忠");
// 设置布局管理器
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setResizable(false);
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);
this.setTitle("FlowLayout");
this.setSize(300, 200);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyJFrame3 extends JFrame {
private int size = 9;
JButton jb[] = new JButton[size];
public MyJFrame3() {
for (int i = 0; i < size; i++) {
jb[i] = new JButton(String.valueOf(i));
}
// 设置布局管理器
this.setLayout(new GridLayout(3, 3));
this.setResizable(false);
for (int i = 0; i < size; i++) {
this.add(jb[i]);
}
this.setTitle("GridLayout");
this.setSize(300, 300);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package cn.demo;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
class MyJFrame4 extends JFrame {
private int size = 6;
JButton jb[] = new JButton[size];
JPanel jPanel1 = null;
JPanel jPanel2 = null;
public MyJFrame4() {
for (int i = 0; i < size; i++) {
jb[i] = new JButton(String.valueOf(i));
}
jPanel1 = new JPanel();
jPanel2 = new JPanel();
jPanel1.add(jb[0]);
jPanel1.add(jb[1]);
jPanel2.add(jb[2]);
jPanel2.add(jb[3]);
jPanel2.add(jb[4]);
this.add(jPanel1, BorderLayout.NORTH);
this.add(jPanel2, BorderLayout.SOUTH);
this.add(jb[5], BorderLayout.CENTER);
this.setResizable(false);
this.setTitle("GridLayout");
this.setSize(300, 200);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyJFrame5 extends JFrame {
JPanel jPanel1 = null;
JPanel jPanel2 = null;
JPanel jPanel3 = null;
JLabel jlabel1 = null;
JTextField jtf = null;
JLabel jlabel2 = null;
JPasswordField jpf = null;
JButton jb1 = null;
JButton jb2 = null;
public MyJFrame5() {
jlabel1 = new JLabel("管理员");
jlabel2 = new JLabel("密 码");
jtf = new JTextField(10);
jpf = new JPasswordField(10);
jb1 = new JButton("登录");
jb2 = new JButton("取消");
jPanel1 = new JPanel();
jPanel1.setLayout(new FlowLayout());// 默认是FlowLayout
jPanel2 = new JPanel();
jPanel3 = new JPanel();
jPanel1.add(jlabel1);
jPanel1.add(jtf);
jPanel2.add(jlabel2);
jPanel2.add(jpf);
jPanel3.add(jb1);
jPanel3.add(jb2);
this.setLayout(new GridLayout(3, 1));
this.add(jPanel1);
this.add(jPanel2);
this.add(jPanel3);
this.setResizable(false);
this.setTitle("会员管理系统");
this.setSize(300, 200);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyJFrame6 extends JFrame {
JPanel jPanel1;
JPanel jPanel2;
JPanel jPanel3;
JLabel jlabel1;
JCheckBox jcheckbox1, jcheckbox2, jcheckbox3;
JLabel jlabel2;
JRadioButton jrb1, jrb2;
ButtonGroup btnGroup;
JButton jb1 = null;
JButton jb2 = null;
public MyJFrame6() {
jlabel1 = new JLabel("你喜欢的运动");
jlabel2 = new JLabel("你的性别");
jcheckbox1 = new JCheckBox("足球");
jcheckbox2 = new JCheckBox("篮球");
jcheckbox3 = new JCheckBox("网球");
jb1 = new JButton("注册");
jb2 = new JButton("取消");
jrb1 = new JRadioButton("男");
jrb2 = new JRadioButton("女");
btnGroup = new ButtonGroup();
btnGroup.add(jrb1);
btnGroup.add(jrb2);
jPanel1 = new JPanel();
jPanel2 = new JPanel();
jPanel3 = new JPanel();
this.setLayout(new GridLayout(3, 1));
jPanel1.add(jlabel1);
jPanel1.add(jcheckbox1);
jPanel1.add(jcheckbox2);
jPanel1.add(jcheckbox3);
jPanel2.add(jlabel2);
jPanel2.add(jrb1);
jPanel2.add(jrb2);
jPanel3.add(jb1);
jPanel3.add(jb2);
this.add(jPanel1);
this.add(jPanel2);
this.add(jPanel3);
this.setResizable(false);
this.setTitle("用户注册界面");
this.setSize(300, 200);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class JFrame01 extends JFrame {
JPanel jp1, jp2;
JLabel jl1, jl2;
String[] comboxString = { "江苏", "山东" };
String[] listString = { "毛里求斯", "马尔代夫","塞舌尔","德国","西班牙" };
JComboBox jcb = null;
JList jlist = null;
JScrollPane jsp = null;
public JFrame01() {
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("籍贯");
jl2 = new JLabel("旅游地点");
jcb = new JComboBox(comboxString);
jlist = new JList(listString);
jlist.setVisibleRowCount(3);
jsp = new JScrollPane(jlist);
this.setLayout(new GridLayout(2, 1));
jp1.add(jl1);
jp1.add(jcb);
jp2.add(jl2);
jp2.add(jsp);
this.add(jp1);
this.add(jp2);
this.setTitle("用户调查");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyJFrame8 extends JFrame {
JList jlist;
JLabel jlabel;
JSplitPane jSplitPane;
public MyJFrame8() {
String[] str = { "hello", "world", "goodbye" };
jlist = new JList(str);
jlabel = new JLabel(new ImageIcon("1_u014338577.jpg"));
// 拆分窗口
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jlist, jlabel);
jSplitPane.setOneTouchExpandable(true);
// 中间撑满整个界面,是一个BorderLayout
this.add(jSplitPane, BorderLayout.CENTER);
this.add(jSplitPane);
this.setResizable(false);
this.setTitle("金山词霸");
this.setSize(400, 300);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyJFrame9 extends JFrame {
JTextArea jArea;
JPanel jp1;
JComboBox jcb;
JTextField jtf;
JButton jb;
JScrollPane jScrollPane;
public MyJFrame9() {
jArea = new JTextArea(10, 10);
String[] comboxString = { "布什", "拉登" };
jcb = new JComboBox(comboxString);
jtf = new JTextField(10);
jb = new JButton("发送");
jScrollPane = new JScrollPane(jArea);
jp1 = new JPanel();
jp1.add(jcb);
jp1.add(jtf);
jp1.add(jb);
this.add(jScrollPane);
this.add(jp1, BorderLayout.SOUTH);
this.setResizable(false);
this.setTitle("山寨qq");
this.setSize(400, 300);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyJFrame10 extends JFrame {
JList jlist;
JLabel jlabel1;
JLabel jlabel2;
JSplitPane jSplitPane1, jSplitPane2;
JScrollPane jScrollPane;
public MyJFrame10() {
String[] str = { "hello", "world", "goodbye", "framework", "class", "system", "java", "good", "zoo", "border",
"China", "package", "title", "find", "experience", "funk", "new", "Swing", "size", "double", "keyboard",
"listener", "synchronization", "synchronous", "cogradient" };
jlist = new JList(str);
jScrollPane = new JScrollPane(jlist);
jlabel1 = new JLabel(new ImageIcon("image.jpg"));
jlabel2 = new JLabel(new ImageIcon("logo.jpg"));
jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jlabel1, jlabel2);
// 拆分窗口
jSplitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jScrollPane, jSplitPane2);
jSplitPane1.setOneTouchExpandable(true);
// 中间撑满整个界面,是一个BorderLayout
this.add(jSplitPane1, BorderLayout.CENTER);
this.setResizable(false);
this.setTitle("金山词霸");
this.setSize(400, 300);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// JFrame 是一个顶层容器类,可以在容器上添加各种控件
public class TestSwing extends JFrame {
public static void main(String[] str) {
MyJFrame10 frame = new MyJFrame10();
}
}
使用JPanel的paint方法
class MyPanel extends JPanel {
public MyPanel() {
}
public void paint(Graphics g) {
super.paint(g);
g.draw3DRect(100, 100, 50, 50, true);
}
}
class MyJFrame4 extends JFrame {
public MyPanel panel = null;
public MyJFrame4() {
panel = new MyPanel();
this.add(panel);
this.setTitle("PanleOnTheJFrame");
this.setSize(300, 300);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package cn.demo;
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel {
Tank tank = null;
int direction = 0;
public MyPanel(Tank tank, int direction) {
this.tank = tank;
this.direction = direction;
}
public void paint(Graphics g) {
super.paint(g);
drawTank(tank, g, direction);
}
public void drawTank(Tank tank, Graphics g, int direction) {
switch (direction) {
case 0:
g.drawRect(tank.getX(), tank.getY(), 5, 30);
g.drawRect(tank.getX() + 5, tank.getY() + 5, 20, 20);
g.drawRect(tank.getX() + 25, tank.getY(), 5, 30);
g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
g.drawLine(tank.getX() + 15, tank.getY() - 5, tank.getX() + 15, tank.getY() + 15);
break;
case 1:
g.drawRect(tank.getX(), tank.getY(), 5, 30);
g.drawRect(tank.getX() + 5, tank.getY() + 5, 20, 20);
g.drawRect(tank.getX() + 25, tank.getY(), 5, 30);
g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
g.drawLine(tank.getX() + 15, tank.getY() + 15, tank.getX() + 15, tank.getY() + 35);
break;
case 2:
g.drawRect(tank.getX(), tank.getY(), 30, 5);
g.drawRect(tank.getX() + 5, tank.getY() + 5, 20, 20);
g.drawRect(tank.getX(), tank.getY() + 25, 30, 5);
g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
g.drawLine(tank.getX() - 5, tank.getY() + 15, tank.getX() + 15, tank.getY() + 15);
break;
case 3:
g.drawRect(tank.getX(), tank.getY(), 30, 5);
g.drawRect(tank.getX() + 5, tank.getY() + 5, 20, 20);
g.drawRect(tank.getX(), tank.getY() + 25, 30, 5);
g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
g.drawLine(tank.getX() + 15, tank.getY() + 15, tank.getX() + 35, tank.getY() + 15);
break;
}
}
}
class Direction {
public final static int UP = 0;
public final static int DOWN = 1;
public final static int LEFT = 2;
public final static int RIGHT = 3;
}
class MyTestJFrame extends JFrame {
MyPanel panel = null;
MyTestJFrame(Tank tank, int direction) {
panel = new MyPanel(tank, direction);
this.add(panel);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocation(100, 200);
}
}
class Tank {
private int x;
private int y;
public Tank(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
class Hero extends Tank {
public Hero(int x, int y) {
super(x, y);
}
}
public class TestMain {
public static void main(String[] str) {
Tank tank = new Hero(50, 50);
MyTestJFrame frame = new MyTestJFrame(tank, Direction.LEFT);
}
}
package cn.demo;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class TJFrame extends JFrame implements ActionListener {
JButton jb1 = null;
JButton jb2 = null;
JPanel jp = null;
JFileChooser jsf = null;
public TJFrame() {
jb1 = new JButton("JFileChooser");
jb1.addActionListener(this);
jb1.setActionCommand("JFileChooser");
jb2 = new JButton("Button");
jb2.addActionListener(this);
jb2.setActionCommand("Button");
jp = new JPanel();
jp.setBackground(Color.YELLOW);
this.add(jb1, BorderLayout.NORTH);
this.add(jb2, BorderLayout.SOUTH);
this.add(jp);
this.setSize(200, 200);
this.setLocation(100, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void showJFileChooser() {
jsf = new JFileChooser();
jsf.setDialogTitle("open file");
int res = jsf.showOpenDialog(this);
if (res == JFileChooser.APPROVE_OPTION) {
File file = jsf.getSelectedFile();
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
} else if (res == JFileChooser.CANCEL_OPTION) {
System.out.println("JFileChooser.CANCEL_OPTION");
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("JFileChooser"))
showJFileChooser();
else if (e.getActionCommand().equals("Button")) {
jp.setBackground(Color.RED);
}
}
}
public class TestMsgBox {
public static void main(String[] str) {
TJFrame frame = new TJFrame();
}
}