Swing
AWT
包含了很多类和接口
元素:窗口,按钮,文本框
Frame(弹窗)
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame JDK 看源码
Frame frame=new Frame("我的第一个Java图像界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(129, 8, 8));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:无法关闭窗口,只能停止运行程序
import java.awt.*;
//封装
//展示多个窗口
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame= new MyFrame(100, 100, 200, 200, Color.white);
MyFrame myFrame1= new MyFrame(300, 100, 200, 200, Color.black);
MyFrame myFrame2= new MyFrame(100, 300, 200, 200, Color.blue);
MyFrame myFrame3= new MyFrame(300, 300, 200, 200, Color.red);
}
}
class MyFrame extends Frame{
static int id=0;//可能存在多个窗口,我们需要一个计数器
public MyFrame (int x,int y,int w,int h,Color color ){
super("MyFrame+"+(++id));
setBackground(color);//颜色
setVisible(true);//可见性
setBounds(x,y,w,h);//大小宽高
}
}
Panel(面板)
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看成一个空间,但是不能单独存在
public class TestPanel1 {
public static void main(String[] args) {
Frame frame=new Frame();
//Panel
Panel panel =new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(208, 36, 98));
//panel坐标 相对于frame
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(12, 113, 215));
//frame.add(panal)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//关闭窗口需要做的事
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
布局管理器
流式布局(Flow)
import java.awt.*;
//流式布局
public class TestFlowLayout1 {
public static void main(String[] args) {
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(500,500);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东西南北中(Border)
import java.awt.*;
//东西南北中
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("东西南北中");
Button east = new Button("East");
Button west = new Button("West");
Button north = new Button("North");
Button south = new Button("South");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(500,500);
frame.setVisible(true);
}
}
列表布局(Grid)
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("GridLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();
frame.setBounds(500,500,300,300);
frame.setBackground(new Color(0xD7B1B1));
frame.setVisible(true);
}
}
总结
Frame是一个顶级窗口
Panel无法单独显示,必须添加到某个容器中
布局管理器
大小,定位,可见性
事件监听
事件监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame = new Frame();
Button button = new Button();
//因为 addActionListener()需要一个ActionListener,所以我们要构造一个ActionListener
MyActionListener myActionListener=new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//关闭窗口的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("qqqq");
}
}
------------------------------------------------------
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//两个按钮实现同一个监听
public class TestActionEvent1 {
public static void main(String[] args) {
Frame frame = new Frame("开始-停止");
Button button1 = new Button("开始");
Button button2 = new Button("结束");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
button2.setActionCommand("button-stop");
MyMonitor myMonitor=new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.WEST);
frame.add(button2,BorderLayout.EAST);
frame.setVisible(true);
frame.pack();
}
}
class MyMonitor implements ActionListener{
//获得按钮的信息
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("开始")){
System.out.println("按钮被点击:message--"+e.getActionCommand());
}else{
System.out.println("按钮被点击:message--"+e.getActionCommand());
}
}
}
输入框TextField监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
new MyFrame1();
}
}
class MyFrame1 extends Frame{
public MyFrame1(){
TextField textField=new TextField();
add(textField);
//监听文本框输入的文字
MyActionListener2 myActionListener2=new MyActionListener2();
//按下enter,就会触发输入框事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField=(TextField) e.getSource();//获得一些资源,返回的一个对象
System.out.println(textField.getText());//获得输入框的文本
}
}
简易计算器
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Mycalculator();
}
}
class Mycalculator extends Frame{
public Mycalculator() {
//三个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//一个按钮
Button button = new Button("=");
//监听事件
button.addActionListener(new MyCalcListener(num1,num2,num3));
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//适应大小
pack();
//可见性
setVisible(true);
}
}
class MyCalcListener implements ActionListener{
//获取三个变量
public TextField num1,num2,num3;
public MyCalcListener(TextField num1,TextField num2,TextField num3){
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//将值加法运算后放入第三个框
num3.setText(""+(n1+n2));
//清除前两个框
num1.setText(" ");
num2.setText(" ");
}
}
完全改造为面向对象写法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Mycalculator().loadFrame();
}
}
//计算器类
class Mycalculator extends Frame{
//属性
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalcListener(this));
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//适应大小
pack();
//可见性
setVisible(true);
}
}
//监听器类
class MyCalcListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另外一个类
Mycalculator mycalculator=null;
public MyCalcListener(Mycalculator mycalculator){
this.mycalculator=mycalculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
// 将值加法运算后放入第三个框
// 清除前两个框
int n1 = Integer.parseInt(mycalculator.num1.getText());
int n2 = Integer.parseInt(mycalculator.num2.getText());
mycalculator.num3.setText(""+(n1+n2));
mycalculator.num1.setText(" ");
mycalculator.num2.setText(" ");
}
}
内部类
更好的包装
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Mycalculator().loadFrame();
}
}
//计算器类
class Mycalculator extends Frame{
//属性
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalcListener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//适应大小
pack();
//可见性
setVisible(true);
}
//监听器类
//内部类的好处,可以畅通的访问外部类的属性
private class MyCalcListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
// 将值加法运算后放入第三个框
// 清除前两个框
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText(" ");
num2.setText(" ");
}
}
}
画笔
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//有颜色
g.setColor(new Color(0x2727D9));
g.drawOval(100,100,100,100);//空心圆
g.fillOval(200,200,50,50);//实心圆
}
}
鼠标监听
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//测试鼠标键监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame2("画图");
}
}
class MyFrame2 extends Frame {
//画画需要画笔,需要监听鼠标当前的当前位置,需要集合来存储这些点
ArrayList points;
public MyFrame2(String title) {
super(title);
setBounds(200,200,200,200);
//存鼠标点击的点
points= new ArrayList<>();
//鼠标监听在窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point=(Point) iterator.next();
g.setColor(Color.BLACK);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面
public void addPoint(Point point){
points.add(point);
}
private class MyMouseListener extends MouseAdapter{
//鼠标按压
@Override
public void mousePressed(MouseEvent e) {
MyFrame2 myFrame2 =(MyFrame2) e.getSource();
//点击会在界面上产生一个点
myFrame2.addPoint(new Point(e.getX(),e.getY()));
//每次点击鼠标重画一次
myFrame2.repaint();//刷新
}
}
}
窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
setVisible(true);
setBounds(200,100,200,100);
setBackground(Color.BLACK);
//addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("已经关闭");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("激活");
}
});
}
}
键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame() {
setBounds(200,200,300,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获取当前键盘的码
int keyCode = e.getKeyCode();
if (keyCode==KeyEvent.VK_0){
System.out.println("你按了0建");
}
}
});
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
public class TestJframe {
//init() 初始化
public void init(){
//顶级窗口
JFrame jFrame=new JFrame("这是一个JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(200,100,200,300);
jFrame.setBackground(Color.BLACK);
//设置文字
JLabel jLabel=new JLabel("林夕的花海");
jFrame.add(jLabel);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new TestJframe().init();
}
}
默认有关闭事件
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestDialog extends JFrame {
public TestDialog(){
this.setVisible(true);
this.setBounds(200,200,100,200);
this.setBackground(Color.BLACK);
//Jframe 放东西,容器
Container container=this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton jButton=new JButton("点击弹出一个对话框");
jButton.setBounds(30,20,20,20);
//点击这个按钮的时候,弹出一个弹窗
jButton.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new Mydialog();
}
});
container.add(jButton);
}
public static void main(String[] args) {
new TestDialog();
}
}
//弹窗的一个窗口
class Mydialog extends JDialog{
public Mydialog() {
this.setVisible(true);
this.setBounds(50,50,50,50);
Container container=this.getContentPane();
container.setLayout(null);
container.add(new Label("花海"));
}
}
import javax.swing.*;
import java.awt.*;
public class TestIcon extends JFrame implements Icon {
private int width;
private int heigth;
public TestIcon(){};
public TestIcon(int width,int heigth){
this.width=width;
this.heigth=heigth;
}
//初始化
public void init(){
TestIcon testIcon = new TestIcon(15, 15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("icontest", testIcon, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestIcon().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,width);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.heigth;
}
}
//图片标签
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
public TestImageIcon(){
//获取图片地址
JLabel label = new JLabel("image");
URL url = TestImageIcon.class.getResource("tx.png");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
import javax.swing.*;
import java.awt.*;
public class TestJpanel extends JFrame {
public TestJpanel() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
JPanel jPanel = new JPanel(new GridLayout(1,3));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500,500);
container.add(jPanel);
}
public static void main(String[] args) {
new TestJpanel();
}
}
文本域JScorll面板
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(40, 40);
jTextArea.setText("林夕的花海");
//scrollm面板
JScrollPane scrollPane = new JScrollPane(jTextArea);
container.add(scrollPane);
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton extends JFrame {
public TestJButton() {
Container container = this.getContentPane();
//将一个图片变为图标
URL url = TestJButton.class.getResource("tx.png");
Icon icon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton();
}
}
单选按钮
public class TestJButton1 extends JFrame {
public TestJButton1() {
Container container = this.getContentPane();
//将一个图片变为图标
URL url = TestJButton.class.getResource("tx.png");
Icon icon = new ImageIcon(url);
// 单选框
JRadioButton radioButton1 = new JRadioButton("01");
JRadioButton radioButton2= new JRadioButton("02");
JRadioButton radioButton3 = new JRadioButton("03");
//由于单选框只能选择一个,分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.SOUTH);
container.add(radioButton3,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton1();
}
}
多选按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton2 extends JFrame{
public TestJButton2() {
Container container = this.getContentPane();
//将一个图片变为图标
URL url = TestJButton.class.getResource("tx.png");
Icon icon = new ImageIcon(url);
//多选框
JCheckBox checkBox1 = new JCheckBox("01");
JCheckBox checkBox2 = new JCheckBox("02");
JCheckBox checkBox3 = new JCheckBox("03");
container.add(checkBox1,BorderLayout.CENTER);
container.add(checkBox2,BorderLayout.SOUTH);
container.add(checkBox3,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton2();
}
}
下拉框
import javax.swing.*;
import java.awt.*;
public class TestCombobox extends JFrame {
public TestCombobox() {
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem("下拉框");
comboBox.addItem("花海");
comboBox.addItem("枫");
comboBox.addItem("花");
container.add(comboBox);
this.setVisible(true);
setBounds(20,20,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestCombobox();
}
}
列表框
import javax.swing.*;
import java.awt.*;
public class TestCombobox1 extends JFrame {
public TestCombobox1() {
Container container = this.getContentPane();
//列表框
String[] contents={"1","2","3"};
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
setBounds(20,20,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestCombobox1();
}
}
文本框
import javax.swing.*;
import java.awt.*;
public class TestText1 extends JFrame{
public TestText1() {
Container container = this.getContentPane();
//文本框
JTextField textField1 = new JTextField("花海");
JTextField textField2 = new JTextField("枫",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
setBounds(20,20,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText1();
}
}
密码框
import javax.swing.*;
import java.awt.*;
public class TestText2 extends JFrame{
public TestText2() {
Container container = this.getContentPane();
//密码框
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('@');
container.add(passwordField);
this.setVisible(true);
setBounds(20,20,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText2();
}
}
文本域
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(40, 40);
jTextArea.setText("林夕的花海");
//scrollm面板
JScrollPane scrollPane = new JScrollPane(jTextArea);
container.add(scrollPane);
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}