Java-GUI练习
程序员文章站
2022-06-28 16:43:47
AWT1.Frame封装实现多个Frameddpackage com.qiang.lesson01;import java.awt.*;public class TestFrame2 { public static void main(String[] args) { //展示多个Frame MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black); MyFrame...
AWT
1.Frame
封装实现多个Framedd
package com.qiang.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个Frame
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.red);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.gray);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);
}
}
class MyFrame extends Frame{
static int id = 0;//窗口计数
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
}
}
2. Panel
解决关闭事件
package com.qiang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* @author ma
*/
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(35, 205, 78, 255));
//面板
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(0xE01141));
//frame.add
frame.add(panel);
frame.setVisible(true);
//监听事件 System.exit()
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
3. 布局管理器
-
流式布局
package com.qiang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件 Button button1 = new B utton("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(400,400); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { //窗口点击关闭 @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } }
-
东西南北中
package com.qiang.lesson01; import java.awt.*; public class TestBoderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button north = new Button("North"); Button south = new Button("South"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.setSize(200,200); frame.setVisible(true); } }
-
表格布局
package com.qiang.lesson01; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); 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();//Java函数 frame.setVisible(true); } }
-
frame =
panel1 ( ( border)button1(east)+panel2(table(2,1)) + button2(west)) )
panel3 ( ( border)button3(east)+pane3l(table(2,2)) + button4(west)) )
public class ExDemo { public static void main(String[] args) { Frame frame = new Frame(); frame.setSize(400,300); frame.setLocation(500,500); frame.setVisible(true); frame.setLayout(new GridLayout(2,1)); //4 panel Panel p1 = new Panel(new BorderLayout()); Panel p2 = new Panel(new GridLayout(2,1)); Panel p3 = new Panel(new BorderLayout()); Panel p4 = new Panel(new GridLayout(2,2)); p1.add(new Button("b1"),BorderLayout.WEST); p1.add(new Button("b2"),BorderLayout.EAST); p2.add(new Button("b5")); p2.add(new Button("b6")); p1.add(p2,BorderLayout.CENTER); p3.add(new Button("b3"),BorderLayout.WEST); p3.add(new Button("b4"),BorderLayout.EAST); p4.add(new Button("b7")); p4.add(new Button("b8")); p4.add(new Button("b9")); p4.add(new Button("b0")); p3.add(p4,BorderLayout.CENTER); frame.add(p1); frame.add(p3); } }
4. 事件监听
1. 按钮事件监听
package com.qiang.lesson02;
import java.awt.*;
import java.awt.event.*;
public class lesson02 {
public static void main(String[] args) {
//按下按钮出发事件
Frame frame = new Frame();
Button button = new Button("message");
//因为 addActionListener()需要一个ActionListener,所以我们构造一个需要一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
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("aaaaaaaaaaa");
}
}
-
多个按钮共享一个事件
package com.qiang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestActionTwo { public static void main(String[] args) { //两个按钮实现同一个监听 //开始 停止 Frame frame = new Frame("开始 停止"); Button button1 = new Button("start"); Button button2 = new Button("stop"); //可以显式定义返回值 //可以多个按钮只写一个监听类! button2.setActionCommand("button-->stop"); MyMonitor myMonitor = new MyMonitor(); button1.addActionListener(myMonitor); button2.addActionListener(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) { //e.getActionCommand()获得按钮的信息 System.out.println("按钮被点击:msg: "+ e.getActionCommand()); if(e.getActionCommand().equals("star")){ } } }
5.输入框TextField监听
package com.qiang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextTex01 {
public static void main(String[] args) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() {
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
MyTextFieldListener myTextFieldListener = new MyTextFieldListener();
//按下enter触发
textField.addActionListener(myTextFieldListener);
//设置替换编码
textField.setEchoChar('@');
pack();
setVisible(true);
}
}
class MyTextFieldListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField filed = (TextField) e.getSource();//获得一些资源,返回对象
System.out.println(filed.getText());//获得输入框文本
filed.setText("");
}
}
6. 简易计算器,组合+内部类优化
-
package com.qiang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class TestCalc { public static void main(String[] args) { new Calculator(); } } //计算器类 class Calculator extends Frame{ public Calculator() { //3 Tex TextField num1 = new TextField(10); TextField num2 = new TextField(10); TextField num3 = new TextField(20); //1 label Label label = new Label("+"); //1 button Button button = new Button("="); button.addActionListener(new MyCalculatorListener(num1,num2,num3)); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //监听器类 class MyCalculatorListener implements ActionListener{ //获取三个变量 private TextField num1,num2,num3; public MyCalculatorListener(TextField num1,TextField num2,TextField num3) { this.num1 = num1; this.num2 = num2; this.num3 = num3; } @Override public void actionPerformed(ActionEvent e) { //1.获得 int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); //2.运算后放到第三个框 num3.setText(""+(n1+n2)); //3.清除前两个 num1.setText(""); num2.setText(""); } }
-
完全改造为面向对象写法:组合
package com.qiang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); } } //计算器类 class Calculator extends Frame{ //属性 TextField num1,num2,num3; Label label; Button button; //方法 public void loadFrame(){ //3 Tex 1 label 1 button num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(20); label = new Label("+"); button = new Button("="); button.addActionListener(new MyCalculatorListener(this)); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //监听器类 class MyCalculatorListener implements ActionListener{ //获取这个对象,在一个类中组合另外一个类 Calculator calculator = null; public MyCalculatorListener(Calculator calculator) { this.calculator = calculator; } @Override public void actionPerformed(ActionEvent e) { //1.获得2.运算后放到第三个框3.清除前两个 int n1 = Integer.parseInt(calculator.num1.getText()); int n2 = Integer.parseInt(calculator.num2.getText()); calculator.num3.setText(""+(n1+n2)); calculator.num1.setText(""); calculator.num2.setText(""); } }
-
完全改造为面向对象写法:内部类 更好的包装
package com.qiang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); } } //计算器类 class Calculator extends Frame{ //属性 TextField num1,num2,num3; Label label; Button button; //方法 public void loadFrame(){ //3 Tex 1 label 1 button num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(20); label = new Label("+"); button = new Button("="); button.addActionListener(new MyCalculatorListener()); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } //内部类监听器类 //畅通无阻的访问外部类的属性和方法! private class MyCalculatorListener 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(""); } } }
7. 画笔
package com.qiang.lesson03;
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,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//颜色,画画
g.setColor(Color.red);
g.fillOval(100,100,100,100);
g.setColor(Color.green);
g.fillRect(100,200,200,200);
//画笔用完还原
}
}
8.鼠标监听
实现鼠标点击画画
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8LmFWi5M-1605884713020)(C:\Users\马永强\AppData\Roaming\Typora\typora-user-images\image-20201120193705794.png)]
package com.qiang.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画画");
}
}
class MyFrame extends Frame{
//画笔,监听鼠标位置,集合存储鼠标位置点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,300);
//存点
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.blue);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
private class MyMouseListener extends MouseAdapter{
//鼠标 按下 弹起 按住
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//点击的时候在界面产生一个点!画
//鼠标的点
myFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击重新画一遍
myFrame.repaint(); //刷新 帧
}
}
}
9.窗口监听
package com.qiang.lesson03;
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(){
setBackground(Color.black);
setBounds(100,100,200,200);
setVisible(true);
addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
WindowFrame windowFrame = (WindowFrame)e.getSource();
windowFrame.setTitle(" ttttttttttt");
}
@Override
public void windowDeactivated(WindowEvent e) {
WindowFrame windowFrame = (WindowFrame)e.getSource();
windowFrame.setTitle(" NNNN");
}
}
);
}
}
10.键盘监听
package com.qiang.lesson03;
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(1,2,300,400);
setVisible(true);
addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的code
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode ==KeyEvent.VK_UP){
System.out.println("wwwwwwwwwwwwww");
}
//拓展 根据按下不同操作,产生不同结果
}
});
}
}
Swing
1.窗口
package com.qiang.lesson04;
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.red);无效
//设置文字
JLabel jlabel = new JLabel("----------------------------------");
jf.add(jlabel);
jf.getContentPane().setBackground(Color.red);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
package com.qiang.lesson04;
import javax.swing.*;
import java.awt.*;
public class JframeDemo02 {
public static void main(String[] args) {
new MyJrame2().init();
}
}
class MyJrame2 extends JFrame{
public void init(){
this.setBounds(100,100,200,500);
this.setVisible(true);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.red);
JLabel jlabel = new JLabel("----------------------------------");
//标签居中
jlabel.setHorizontalAlignment(SwingConstants.CENTER);
this.add(jlabel);
}
}
2.弹窗
默认就有关闭事件
package com.qiang.lesson04;
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 container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出对话框");
button.setBounds(30,30,200,50);
//点击弹出弹窗
button.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
});
container.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("ssssssss"));
}
}
3.标签
label
new JLabel("xxxx")
图标ICON
package com.qiang.lesson04;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}
public IconDemo(int width,int height){
this.width = width;
this.height = 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 container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片
package com.qiang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获得图片路径
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("tx.png");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
4.面板
5.按钮
- 单选按钮
- 复选按钮
6.列表
7.文本框
本文地址:https://blog.csdn.net/weixin_41750881/article/details/109880255
上一篇: SpringBoot入门教程(超详细)
下一篇: 设计模式值适配器模式