当前位置:网站首页>Low level version of drawing interface (explain each step in detail)
Low level version of drawing interface (explain each step in detail)
2022-07-03 15:54:00 【Zhangjian Tianya 2.0】
package com.lk.login1029;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
// Don't look at the source documents and references in the whole process, plus some of your own ideas
// First create a form
// The size of the form needs to be set ; In the middle ; When the form closes, the program closes itself and ends ;
// You need to add buttons to the form ; Note that the button can only be added when the form is displayed ; Brushes and the like ;
// When the demand reaches the button to be added, you will think about where to add it , So you need to add a layout manager
// When you need to add multiple buttons, you find that their steps are similar ; So use functions instead of repetitive work
// Need to add text ; The button is created by itself ; Form required ; Need a listener
// You need to install a listener on the form ; By looking at the original code, it is found that the contents in brackets need an interface data type ; When you need interface data types, you need to rewrite a class as data
// You need an array to match str Fill the occupied position
// When you need to add color buttons, you find ; The contents of the array have changed ; So create a function to add a button with all colors
public class DrawUI2 {
String[] strs = {" A straight line "," Square "," The ellipse "," an isosceles triangle "," Any triangle "," Freeform "};// Note that it is stored in array form, not in list form
Color[] color = {Color.black,Color.blue,Color.red,Color.green,Color.yellow};// Color is also a type
public void showUI() {
JFrame jf =new JFrame();
jf.setSize(900, 900);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
FlowLayout flow =new FlowLayout();
jf.setLayout(flow);
//JButton jb =new JButton("text");
//jf.add(jb);
//jb.addActionListener(mouse);
Mouse2 mou =new Mouse2();
// Add listeners to the interface
jf.addMouseListener(mou);
// Add multiple buttons on the interface
// This kind of general belt xxx. Is either an attribute or a method
for (int i=0;i<strs.length;i++) {
AddButton(strs[i],jf,mou);}
for (int i=0;i<color.length;i++) {
AddColor(color[i],jf,mou);
}
Graphics gr = jf.getGraphics();
mou.g = gr;
}
public void AddButton(String str,JFrame jf,ActionListener al) {
JButton jb = new JButton(str);
jf.add(jb);
jb.addActionListener(al);
}
public void AddColor(Color color,JFrame jf,ActionListener al) {
JButton jb = new JButton(" ");// The space function increases the size of the button
jb.setBackground(color);
jf.add(jb);
jb.addActionListener(al);
}
public static void main(String[] args) {
DrawUI2 ui=new DrawUI2();
ui.showUI();
}
}
package com.lk.login1029;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
public class Mouse2 implements ActionListener,MouseListener{
Graphics g;
String typestr,str ;
int x1,y1,x2,y2;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println(" The button is clicked ");
str =e.getActionCommand();
if(str.equals(" ")){
System.out.println(" This is a color button ");
JButton jbu =(JButton)e.getSource();// This means to record the event source obtained
Color color = jbu.getBackground();
g.setColor(color);// The brush gets the background color
}
else {
typestr =str;
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(" The button is clicked ");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1 = e.getX();
y1 =e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2 =e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(" Get into ");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(" sign out ");
}
}
The front is the whole program
// First Create a form
// Form needs Set size Ah, something like that ; In the middle ; When When the form closes, the program closes itself and ends ;
// You need to add buttons to the form ; Note that the button can only be added when the form is displayed ; Brushes and the like ;
// When the demand reaches the button to be added, you will think about where to add it , So you need to add Layout manager
// When you need to add multiple buttons, you find that their steps are similar ; So use functions instead of repetitive work
// Need to add text ; The button is created by itself ; Form required ; Need a listener
// You need to install a listener on the form ; By looking at the original code, it is found that the contents in brackets need an interface data type ; When interface data types are required Rewrite a class as data
// You need an array to match str Fill the occupied position
// When you need to add color buttons, you find ; The contents of the array have changed ; So create a Function to add a button with all colors
边栏推荐
- Go language self-study series | if else if statement in golang
- do{}while()的妙用
- Microservice - fuse hystrix
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- App移动端测试【5】文件的写入、读取
- 几种常见IO模型的原理
- 软件安装信息、系统服务在注册表中的位置
- [combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
- Download and install common programs using AUR
- [系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
猜你喜欢
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
App移动端测试【3】ADB命令
Visual upper system design and development (Halcon WinForm) -3 Image control
Jvm-06-execution engine
Second kill system 3 - list of items and item details
找映射关系
Microservice - declarative interface call openfeign
Subclass hides the function with the same name of the parent class
半监督学习
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
随机推荐
Qt常用语句备忘
《微服务设计》读书笔记(下)
App移动端测试【3】ADB命令
Go语言自学系列 | golang switch语句
How to use annotations such as @notnull to verify and handle global exceptions
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
CString的GetBuffer和ReleaseBuffer使用说明
How idea starts run dashboard
Detailed pointer advanced 2
深度学习之三维重建
About text selection in web pages and counting the length of selected text
求字符串函数和长度不受限制的字符串函数的详解
Break through 1million, sword finger 2million!
Secsha system 1- login function
详解指针进阶1
首发!!lancet饿了么官方文档
nifi从入门到实战(保姆级教程)——flow
潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
Tensorflow realizes verification code recognition (II)
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!