当前位置:网站首页>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

边栏推荐
- 通过进程PID获取可执行文件路径(QueryFullProcessImageName)
- The difference between mutually exclusive objects and critical areas
- Custom annotation
- “用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
- Jvm-06-execution engine
- 需要知道的字符串函数
- Popular understanding of linear regression (I)
- Intelij idea efficient skills (III)
- How to use annotations such as @notnull to verify and handle global exceptions
- Unity功能——Unity离线文档下载及使用
猜你喜欢

How idea starts run dashboard

C language brush questions ~leetcode and simple questions of niuke.com

Visual upper system design and development (Halcon WinForm) -4 Communication management

WinDbg analysis dump file

Microservice - fuse hystrix

Microservice sentinel flow control degradation

Jvm-06-execution engine

CString getbuffer and releasebuffer instructions

深度学习之三维重建

Jvm-08-garbage collector
随机推荐
CString在多线程中的问题
Go language self-study series | if else if statement in golang
Backtracking method to solve batch job scheduling problem
CString中使用百分号
App mobile terminal test [3] ADB command
Go language self-study series | golang switch statement
Jvm-04-runtime data area heap, method area
Seckill system 3- product list and product details
Popular understanding of gradient descent
Large CSV split and merge
About text selection in web pages and counting the length of selected text
Popular understanding of decision tree ID3
Calibre LVL
Problems of CString in multithreading
秒杀系统2-Redis解决分布式Session问题
Redis high availability and persistence
C语言刷题~Leetcode与牛客网简单题
The wonderful use of do{}while()
整形和浮点型是如何在内存中的存储
Subclass hides the function with the same name of the parent class