Swing.JFrame基础
弹出对话框
/*
* TreeFrame.java
*
* Created on __DATE__, __TIME__
*/
package com.jframe_1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author 弹出对话框
*/
class MyJDialog extends JDialog {
public MyJDialog(num1 frame) {
super(frame,"第一个JDialog窗体",true);
Container contianer = getContentPane();
contianer.add(new JLabel("这是一个对话框"));
setBounds(120,120,100,100);
}
}
public class num1 extends JFrame {
public num1() {
JFrame jf = new JFrame();
Container container = jf.getContentPane();//创建一个容器
container.setLayout(null);//使该窗体取消布局管理器设置,即绝对布局
JLabel jl = new JLabel("这是一个JFrame窗体");//在窗体中设置一个标签
jl.setHorizontalAlignment(SwingConstants.CENTER);//标签文字在标签中居中
container.add(jl);//把标签添加到容器中
JButton bl = new JButton("弹出对话框");//定义一个按钮
bl.setBounds(100, 10, 100, 21);//x;y;wight;heigth
bl.addActionListener((ActionListener) new ActionListener() {//为按钮添加鼠标单击事件
public void actionPerformed(ActionEvent e) {
new MyJDialog(num1.this).setVisible(true);
}
});
container.add(bl);//添加到容器中
container.setBackground(Color.white);//容器背景色
jf.setVisible(true);//使窗体可视
jf.setSize(400,300);//窗体大小
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体关闭方式
}
public void CreateJFrame(String title) {
JFrame jf = new JFrame(title);
Container container = jf.getContentPane();
JLabel jl = new JLabel("这是一个窗体");
jl.setHorizontalAlignment(SwingConstants.HORIZONTAL);
jl.setSize(100, 20);
container.add(jl);
container.setBackground(Color.white);
jf.setVisible(true);
jf.setSize(400,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String arg[]) {
new num1();
}
}
图标的使用
package com.jframe_1;
import java.awt.*;
import javax.swing.*;
/**
*
* @author 设置带图标的标签
*/
public class DrawIcon implements Icon{
private int width;
private int height;
public int getIconHeight() {
return this.height;
}
public int getIconWidth() {
return this.width;
}
public DrawIcon(int width, int height) {
this.width = width;
this.height = height;
}
public void paintIcon(Component arg0,Graphics arg1,int x,int y) {
arg1.fillOval(x, y, width, height);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawIcon icon = new DrawIcon(15,15);
JLabel j = new JLabel("测试",icon,SwingConstants.CENTER);
JFrame jf = new JFrame();
Container c = jf.getContentPane();
c.add(j);
c.setBackground(Color.white);
jf.setVisible(true);
jf.setSize(400,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
绝对布局
package com.jframe_1;
import javax.swing.*;
import java.awt.*;
public class AbsolutePosition extends JFrame{
public AbsolutePosition() {
setTitle("本窗体使用绝对布局");
getContentPane().setLayout(null);//使窗体取消布局管理器设置
setBounds(0, 0, 400, 300);
Container c = getContentPane();
JButton b1 = new JButton("按钮_1");
JButton b2 = new JButton("按钮_2");
b1.setBounds(30, 25, 80, 30);
b2.setBounds(200, 100, 100, 20);
c.add(b1);
c.add(b2);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new AbsolutePosition();
}
}
流布局管理器
package com.jframe_1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FlowLayoutPosition extends JFrame{
public FlowLayoutPosition() {
setTitle("本窗体使用流布局管理器");
Container c = getContentPane();
setLayout(new FlowLayout(2,10,10));//0:向左对齐,2:向右对齐;水平间隔;垂直间隔。
for(int i = 0; i < 10; i++) {
c.add(new JButton("button" + i));
}
JButton jb = new JButton();
jb.setText("jb");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "弹出对话框");
}
});
c.add(jb);
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new FlowLayoutPosition();
}
}
单选按钮
package com.jframe_1;
import java.awt.*;
import javax.swing.*;
public class JRadioBtn extends JFrame{
public JRadioBtn() {
setTitle("单选按钮");
setLayout(null);
Container c = getContentPane();
setBounds(0, 0, 400, 300);
JRadioButton b1 = new JRadioButton("B1");
JRadioButton b2 = new JRadioButton("B2");
JRadioButton b3 = new JRadioButton("B3");
b1.setBounds(50, 50, 40, 30);
b2.setBounds(100, 50, 40, 30);
b3.setBounds(150, 50, 40, 30);
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
c.add(b2);
c.add(b3);
c.add(b1);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JRadioBtn();
}
}
复选框
package com.jframe_1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CheckBoxTest extends JFrame{
public CheckBoxTest() {
setTitle("复选框");
Container c = getContentPane();
setSize(500,500);
c.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
c.add(panel1,BorderLayout.NORTH);
JTextArea jt = new JTextArea("文本域\n",10,20);
jt.setLineWrap(true);//文本域自动换行
final JScrollPane scrollpane = new JScrollPane(jt);
panel1.add(scrollpane);
c.add(panel2,BorderLayout.SOUTH);
JCheckBox jc1 = new JCheckBox("1");
JCheckBox jc2 = new JCheckBox("2");
JCheckBox jc3 = new JCheckBox("3");
panel2.add(jc1);
panel2.add(jc2);
panel2.add(jc3);
jc1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jt.append("复选框1被选中");
}
});
jc2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jt.append("复选框2被选中");
}
});
jc3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jt.append("复选框3被选中");
}
});
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new CheckBoxTest();
}
}
下拉列表框
package com.jframe_1;
import java.awt.*;
import javax.swing.*;
public class JComboBoxTest extends JFrame {
public JComboBoxTest() {
JComboBox<String> jc = new JComboBox();
JLabel ji = new JLabel("请选择证件:");
setTitle("下拉列表框");
setSize(500,400);
jc.addItem("身份证");
jc.addItem("学生证");
jc.addItem("工作证");
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(ji);
c.add(jc);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JComboBoxTest();
}
}
列表框组件、文本框组件、密码框
package com.jframe_1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JlistTest extends JFrame {
public JlistTest() {
setTitle("列表框组件、文本框组件、密码框");
setBounds(0, 0, 500, 300);
Container c = getContentPane();
c.setLayout(null);
//列表框
String[] contents = {"列表1","列表2","列表3","列表4","列表5","列表6","列表7","列表8"};
JList<String> jl = new JList(contents);
JScrollPane js = new JScrollPane(jl);
js.setBounds(10, 10, 100, 100);
c.add(js);
//文本框
JTextField jtf = new JTextField("文本框",20);
JButton jb = new JButton("清除");
jtf.setBounds(200, 20, 200, 20);
jb.setBounds(250, 50, 80, 20);
c.add(jb);
c.add(jtf);
jtf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jtf.setText("触发事件");
}
});
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jtf.setText("");
jtf.requestFocus();
}
});
//密码框
JPasswordField jp = new JPasswordField(20);
jp.setEchoChar('*');
jp.setBounds(200, 100, 200, 20);
c.add(jp);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JlistTest();
}
}
边界布局管理器
设置容器为边界布局管理器:setLayout(new BorderLayout());
添加组件时的位置:BorderLayout.NORTH/SOUTH/EAST/WEST/CENTER 上下右左中
添加 列 :container.add(BorderLayout.NORTH,button);
网格布局管理器
设置容器为网格布局管理器: setLayout(new GridLayout(网格行数,网格列数,网格之间水平间距,垂直间距));
JPanel面板
也是一种容器,继承自java.awt.Container类,可在JPanel中设置布局管理器,在添加至容器中。
JScrollPane面板
带滚动条的面板,也是容器,只能放置一个组件,可见多个组件放置到JPanel中,在将JPanel添加到JScrollPane中,在将面板添加到容器中。
常用的Swing组件
JButton 按钮
JCheckBix 复选框
JComBox 下拉列表框
JFrame 框架类
JDialog 对话框
JLabel 标签
JRadioButton 单选按钮
JList 列表
JTextField 文本框
JPasswordField 密码框
JTextArea 文本区域
JOptionPane 对话框