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

边栏推荐
- Halcon and WinForm study section 1
- App mobile terminal test [3] ADB command
- Tensorflow realizes verification code recognition (III)
- Subclass hides the function with the same name of the parent class
- Visual upper system design and development (Halcon WinForm) -6 Nodes and grids
- Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
- Popular understanding of gradient descent
- The wonderful use of do{}while()
- Popular understanding of random forest
- Unity function - unity offline document download and use
猜你喜欢

C语言刷题~Leetcode与牛客网简单题

nifi从入门到实战(保姆级教程)——flow

Distributed task scheduling XXL job

《微服务设计》读书笔记(下)

请做好3年内随时失业的准备?

“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画

Baidu AI Cloud helps Shizuishan upgrade the smart health care model of "Internet + elderly care services"

Visual upper system design and development (Halcon WinForm) -1 Process node design

Popular understanding of decision tree ID3

String functions that you need to know
随机推荐
Persisting in output requires continuous learning
CString getbuffer and releasebuffer instructions
Please be prepared to lose your job at any time within 3 years?
整形和浮点型是如何在内存中的存储
[system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
Problems of CString in multithreading
How are integer and floating-point types stored in memory
2022年Q2加密市场投融资报告:GameFi成为投资关键词
Visual upper system design and development (Halcon WinForm) -4 Communication management
Microservices Seata distributed transactions
自定义注解
详解指针进阶2
Get the executable path through the process PID (queryfullprocessimagename)
Large CSV split and merge
Intelij idea efficient skills (III)
do{}while()的妙用
nifi从入门到实战(保姆级教程)——flow
求字符串函数和长度不受限制的字符串函数的详解
深度学习之三维重建
Calibre LVL