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

边栏推荐
- Introduction series of software reverse cracking (1) - common configurations and function windows of xdbg32/64
- 嵌入式开发:避免开源软件的7个理由
- 驱动与应用程序通信
- Microservice - fuse hystrix
- Semi supervised learning
- Reading notes of "micro service design" (Part 2)
- do{}while()的妙用
- [系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
- Qt常用语句备忘
- 秒殺系統3-商品列錶和商品詳情
猜你喜欢

How to use annotations such as @notnull to verify and handle global exceptions

嵌入式开发:避免开源软件的7个理由

整形和浮点型是如何在内存中的存储

从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍

半监督学习

“用Android复刻Apple产品UI”(3)—优雅的数据统计图表

ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装

Digital image processing -- popular Canny edge detection

Tensorflow realizes verification code recognition (I)

qt使用QZxing生成二维码
随机推荐
互斥对象与临界区的区别
首发!!lancet饿了么官方文档
Get the executable path through the process PID (queryfullprocessimagename)
利用MySQL中的乐观锁和悲观锁实现分布式锁
半监督学习
Visual upper system design and development (Halcon WinForm) -5 camera
Seckill system 3- product list and product details
Popular understanding of random forest
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
Secsha system 1- login function
Approval process design
Win10 enterprise 2016 long term service activation tutorial
一些事情的反思
[system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
Jvm-09 byte code introduction
Second kill system 3 - list of items and item details
Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
Go语言自学系列 | golang中的if else if语句
A Fei's expectation
Reading notes of "micro service design" (Part 2)