流式布局
package com.kuang.lesson01;
import java.awt.*;
public class TestFlowLayout {
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.add(button1);
frame.add(button2);
frame.add(button3);
frame.setSize(200,200);
frame.setVisible(true);
}
}
东西南北中
package com.kuang.lesson01;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
}
}
表格布局 Grid
package com.kuang.lesson01;
import java.awt.*;
public class TestGrid {
public static void main(String[] args) {
Frame frame = new Frame();
Button bt_1 = new Button("bt_1");
Button bt_2 = new Button("bt_2");
Button bt_3 = new Button("bt_3");
Button bt_4 = new Button("bt_4");
Button bt_5 = new Button("bt_5");
Button bt_6 = new Button("bt_6");
frame.setLayout(new GridLayout(3,2));
frame.add(bt_1);
frame.add(bt_2);
frame.add(bt_3);
frame.add(bt_4);
frame.add(bt_5);
frame.add(bt_6);
frame.pack();
frame.setVisible(true);
}
}
总结:
package com.kuang.lesson01;
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 LessonAction {
public static void main(String[] args) {
//按下按钮 触发一些事情
Frame frame = new Frame();
Button button = new Button();
button.addActionListener(new MyActionListener());
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
WindowsClose(frame);
}
private static void WindowsClose(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(11111111);
}
}
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestAction2 {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
//可以多个按钮只写一个监听
button2.setActionCommand("button2-stop");
button1.addActionListener(new MYMonitor());
button2.addActionListener(new MYMonitor());
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MYMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:msg "+e.getActionCommand());
}
}
多个按钮,共享一个监听事件
package com.kuang.lesson01;
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 TestText1 {
public static void main(String[] args) {
new MyFrame1();
}
}
class MyFrame1 extends Frame {
public MyFrame1(){
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字,按下enter就会触发监听事件
textField.addActionListener(new MyListener());
//文本设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
close(this);
}
public static void close(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField=(TextField) e.getSource();
System.out.println(textField.getText());//获得输入框中的文本
textField.setText("");
}
}
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame {
public Calculator() {
//3个文本框,1个按钮,一个标签
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
Button button = new Button("=");
//button加监听事件
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
button.addActionListener(new MyCalcListener(num1,num2,num3));
}
}
//监听器类
class MyCalcListener implements ActionListener{
//获取三个变量
TextField num1=null;
TextField num2=null;
TextField num3=null;
public MyCalcListener(TextField num1,TextField num2,TextField num3){
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
int i1=Integer.parseInt(num1.getText());
int i2=Integer.parseInt(num2.getText());
int sum=i1+i2;
num3.setText(Integer.toString(sum));
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator {
public static void main(String[] args) {
new Calculator().load();
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void load(){
//3个文本框,1个按钮,一个标签
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
//button加监听事件
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
button.addActionListener(new MyCalcListener(this));
}
/* public Calculator() {
}*/
}
//监听器类
class MyCalcListener implements ActionListener{
//获取三个变量
Calculator calculator=null;
public MyCalcListener(Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int i1 = Integer.parseInt(calculator.num1.getText());
int i2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(i1+i2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator {
public static void main(String[] args) {
new Calculator().load();
}
}
//计算器类
class Calculator extends Frame {
public Calculator() {
}
//属性
TextField num1, num2, num3;
//方法
public void load() {
//3个文本框,1个按钮,一个标签
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
//button加监听事件
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
button.addActionListener(new MyCalcListener());
}
private class MyCalcListener implements ActionListener {
//获取三个变量
@Override
public void actionPerformed(ActionEvent e) {
int i1 = Integer.parseInt(num1.getText());
int i2 = Integer.parseInt(num2.getText());
num3.setText("" + (i1 + i2));
num1.setText("");
num2.setText("");
}
}
}
内部类的好处就是可以畅通无阻的访问外部属性(private class xxx )
package com.kuang.lesson01;
import java.awt.*;
public class Lesson03 {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setVisible(true);
setBounds(200,200,600,400);
}
@Override
public void paint(Graphics g) {
//画笔需要有颜色,可以画画
g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);
g.setColor(Color.GREEN);
g.fillRect(200,200,100,100);
//养成习惯,画笔用完还原颜色
g.setColor(Color.black);
}
}
目的:想要鼠标实现画画
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 MyMouseListener("画图");
}
}
//自己的类
class MyMouseListener extends Frame{
//需要画笔需要监听鼠标当前的位置\
ArrayList points=new ArrayList<>();
public MyMouseListener(String title) {
super(title);
setBounds(200,200,400,300);
setVisible(true);
//存鼠标的点
//points=new ArrayList<>();
//鼠标监听器,针对这个窗口的
addMouseListener(new MyMouseListener1());
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point =(Point) iterator.next();
g.fillOval(point.x,point.y,10,10);
}
}
/*//添加一个点到界面上
public void addPaint(Point point){
}*/
private class MyMouseListener1 extends MouseAdapter {
//鼠标点击,按压
@Override
public void mousePressed(MouseEvent e) {
Frame frame = (Frame) e.getSource();
//点击的时候就会在界面产生点
//这个点就是鼠标的点
Point point = new Point(e.getX(), e.getY());
points.add(point);
frame.repaint();
}
}
}
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame("使用窗口");
}
}
class WindowFrame extends Frame{
public WindowFrame(String title){
super(title);
setVisible(true);
setBackground(Color.blue);
setBounds(100,100,200,200);
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
Frame source = (Frame)e.getSource();
System.out.println(source.getTitle()+"被激活了");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
});
}
}
package com.kuang.lesson01;
import jdk.swing.interop.SwingInterOpUtils;
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(String title) {
super(title);
setBounds(1,2,300,400);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按压的哪个键,当前键盘的码
System.out.println(e.getKeyCode());
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init():初始化
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.CYAN);
//设置文字JLabel
JLabel label = new JLabel("欢迎来到狂神说Java系列节目");
jf.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
标签居中
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame2().init();
}
}
class MyJFrame2 extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(10,10,200,300);
JLabel label = new JLabel("欢迎来到狂神说Java系列节目");
this.add(label);
//让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container contentPane = getContentPane();
contentPane.setBackground(Color.blue);
}
}
JDialog 用来被弹出,默认就有关闭事件
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击一个按钮的时候弹出另一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
contentPane.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new JLabel("秦老师带你学java"));
}
}
Jlabel
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
//图标 需要实现类
public class ICONDemo extends JFrame implements Icon {
private int width;
private int height;
public static void main(String[] args) {
new ICONDemo().init();
}
public void init(){
ICONDemo iconDemo = new ICONDemo(15, 15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container contentPane = this.getContentPane();
contentPane.add(label);
this.setVisible(true);
this.setBounds(100,100,200,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public ICONDemo(){//无参构造
}
public ICONDemo(int width,int height){//有参构造
this.height=height;
this.width=width;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,this.width,this.height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图标ICON
package com.kuang.SWING;
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 panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel();
}
}
JScrollPanel
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class TestJScrollPanel extends JFrame {
public TestJScrollPanel(){
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("欢迎学习Java");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJScrollPanel();
}
}
图片按钮
package com.kuang.SWING;
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.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("这是一个图片按钮");
container.add(jButton);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton();
}
}
单选按钮
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class TextJButton2 extends JFrame {
public TextJButton2(){
Container container = this.getContentPane();
//单选框
JRadioButton JRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton JRadioButton02 = new JRadioButton("JRadioButton02");
JRadioButton JRadioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,所以创建一个组,把单选框放到组里面进行单选
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(JRadioButton01);
buttonGroup.add(JRadioButton02);
buttonGroup.add(JRadioButton03);
// container.setLayout(new GridLayout(1,3));
container.add(JRadioButton01,BorderLayout.NORTH);
container.add(JRadioButton02,BorderLayout.CENTER);
container.add(JRadioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextJButton2();
}
}
多选框
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class TextJButton3 extends JFrame {
public TextJButton3(){
Container container = this.getContentPane();
//单选框
//JRadioButton JRadioButton01 = new JRadioButton("JRadioButton01");
// JRadioButton JRadioButton02 = new JRadioButton("JRadioButton02");
//JRadioButton JRadioButton03 = new JRadioButton("JRadioButton03");
Checkbox checkbox1 = new Checkbox("Checkbox1");
Checkbox checkbox2 = new Checkbox("Checkbox2");
Checkbox checkbox3 = new Checkbox("Checkbox3");
container.add(checkbox1,BorderLayout.NORTH);
container.add(checkbox2,BorderLayout.CENTER);
container.add(checkbox3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextJButton3();
}
}
下拉框
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class TestComboBox extends JFrame {
public TestComboBox() {
Container container = this.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("正在热映");
jComboBox.addItem("已经下架");
jComboBox.addItem("即将上映");
container.setLayout(null);
jComboBox.setSize(100,10);
container.add(jComboBox,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox();
}
}
列表框
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestJList extends JFrame {
public TestJList() {
Container container = this.getContentPane();
Vector content = new Vector();
JList jList = new JList(content);
content.add("1");
content.add("2");
content.add("3");
container.add(jList);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJList();
}
}
下拉框应用场景
* 选择地区或则一些单个选项
* 列表,一般是展示信息,一般是动态来增加选项的
普通文本框
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestText extends JFrame {
public TestText() {
Container container = this.getContentPane();
container.setLayout(null);
TextField textField1= new TextField("Hello");
TextField textField2 = new TextField("World",20);
textField1.setBounds(10,10,150,20);
textField2.setBounds(10,100,150,20);
container.add(textField1);
container.add(textField2);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText();
}
}
文本域
package com.kuang.SWING;
import javax.swing.*;
import java.awt.*;
public class TestText2 extends JFrame {
public TestText2() {
Container container = this.getContentPane();
//container.setLayout(null);
TextArea textArea = new TextArea(20, 50);
textArea.setFont(new Font("微软雅黑",20,20));
textArea.setText("hello");
container.add(textArea);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText2();
}
}
帧,如果时间片足够小就是动画,一秒30帧,就是一秒刷新30次,连起来就是动画,拆开就是图片
需要
键盘监听
定时器Timer