自己编写的Java小程序(敬请指教!)
编写
画图板程序,首先要创建一个画线或画图形的环境,即画图板,所以要创建一个窗体,但要记住在窗体上并不能直接画,要给窗体加一个画布(即Graphics),
监听器是画图板中重点,我是用先创建一个
事件处理类,再给对象注册该类监听器,例如: ShapJbuttonlistener m2=new ShapJbuttonlistener();
btn_01.addActionListener(m2);
这样思绪比较清楚,还有就是在画图板中对象和监听器的传递。
源代码:
登陆页面:
package wubin.draw1007;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public
class Login extends JFrame{
/**
* 入口函数
*/
public static void main(String[] args){
Login lg=new Login();
lg.iniGUI();
}
//方法
public void iniGUI(){
this.setTitle("登陆页面");
this.setBounds(400,400,300,200);
this.set
DefaultClose
Operation(3);
this.setLayout(new FlowLayout());
/**
* 创建组件
*/
JLabel la_01=new JLabel("用户名");
JTextField jtx_02=new JTextField(20);
JLabel la_03=new JLabel("密 码");
JPasswordField jp_04=new JPasswordField(20);
JButton btn_05=new JButton("登陆");
btn_05.setActionCommand("true");
JButton btn_06=new JButton("清空");
btn_06.setActionCommand("clear");
//添加组件
this.add(la_01);
this.add(jtx_02);
this.add(la_03);
this.add(jp_04);
this.add(btn_05);
this.add(btn_06);
//给按钮添加监听器
Loginlistener m=new Loginlistener(jtx_02,jp_04,this);
btn_05.addActionListener(m);
btn_06.addActionListener(m);
this.setVisible(true);
}
登陆监听器:
package wubin.draw1007;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Loginlistener implements ActionListener {
private JTextField jtx;
private JPasswordField jp;
private JFrame jf;
public Loginlistener(JTextField jtx,JPasswordField jp,JFrame jf){
this.jf=jf;
this.jtx=jtx;
this.jp=jp;
}
@Override
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if(s.equals("true")){
if(jtx.getText().equals("wubin")&&new String(jp.getPassword()).equals("123")){
Drawframe frame=new Drawframe();
frame.draw();
jf.setVisible(false);
System.out.println("denglu");
}
}else if(s.equals("clear")){
jtx.setText("");
jp.setText("");
System.out.println("quxiao");
}
// TODO Auto-generated method stub
}
}
画图板:
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Drawframe extends JFrame {
//属性
//方法
public void draw(){
this.setTitle("画图板");
this.setBounds(200,200,500,400);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
this.setVisible(true);
//创建组件
JButton btn_01=new JButton("直线");
btn_01.setActionCommand("line");
JButton btn_02=new JButton("矩形");
btn_02.setActionCommand("rect");
this.add(btn_01);
this.add(btn_02);
//添加画布
Graphics g=this.getGraphics();
//给按钮添加监听器
ShapJbuttonlistener m2=new ShapJbuttonlistener();
btn_01.addActionListener(m2);
btn_02.addActionListener(m2);
Drawlistener m1=new Drawlistener(g,m2);
this.addMouseListener(m1);
}
}
画图板按钮监听器:
package wubin.draw1007;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShapJbuttonlistener implements ActionListener {
public String
command;
@Override
public void actionPerformed(ActionEvent e) {
command=e.getActionCommand();
// TODO Auto-generated method stub
}
}
[color=red][/color]
画线的实现;
package wubin.draw1007;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
public class Drawlistener implements MouseListener {
private int x1,x2,y1,y2;
private Graphics g;
private ShapJbuttonlistener m2;
public Drawlistener(Graphics g,ShapJbuttonlistener m2){
this.m2=m2;
this.g=g;
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
String type=m2.command;
if(type.equals("line")){
g.drawLine(x1, y1, x2, y2);
}else if(type.equals("rect")){
g.drawRect(Math.min(x1, x2),Math.min(y1, y2) ,Math.abs(x1-x2) ,Math.abs(y1-y2));
}
// TODO Auto-generated method stub
}
}