跪求Java高手补写程序,我想(Graphics g)先画好所有再添加到各个button监听器中
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.awt.event.Acti...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.*;
import javax.swing.WindowConstants;
import java.io.*;
import java.util.*;
public class PainPen extends JFrame{
PainPen(){
super("画图");
JPanel p=new JPanel();
Container c=getContentPane();
c.add(p,BorderLayout.CENTER);
p.setBackground(Color.WHITE);
JButton drLine=new JButton("画直线");
JButton drPen=new JButton("画笔");
JButton drCircle=new JButton("画圆");
JButton drEraser=new JButton("橡皮");
JButton drRect=new JButton("画矩形");
JButton Clear=new JButton("清除");
p.add(drLine);
p.add(drPen);
p.add(drCircle);
p.add(drEraser);
p.add(drRect);
p.add(Clear);
drLine.setBounds(0,0,80, 60);
drPen.setBounds(90, 0, 80, 60);
drCircle.setBounds(180, 0, 80, 60);
drEraser.setBounds(270, 0, 80, 60);
drRect.setBounds(360, 0, 80, 60);
Clear.setBounds(450, 0, 80, 60);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drLine.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drPen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drCircle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drEraser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drRect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
Clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int x1,x2,y1,y2,width,height;
boolean First =true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new PainPen();
}
} 展开
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.*;
import javax.swing.WindowConstants;
import java.io.*;
import java.util.*;
public class PainPen extends JFrame{
PainPen(){
super("画图");
JPanel p=new JPanel();
Container c=getContentPane();
c.add(p,BorderLayout.CENTER);
p.setBackground(Color.WHITE);
JButton drLine=new JButton("画直线");
JButton drPen=new JButton("画笔");
JButton drCircle=new JButton("画圆");
JButton drEraser=new JButton("橡皮");
JButton drRect=new JButton("画矩形");
JButton Clear=new JButton("清除");
p.add(drLine);
p.add(drPen);
p.add(drCircle);
p.add(drEraser);
p.add(drRect);
p.add(Clear);
drLine.setBounds(0,0,80, 60);
drPen.setBounds(90, 0, 80, 60);
drCircle.setBounds(180, 0, 80, 60);
drEraser.setBounds(270, 0, 80, 60);
drRect.setBounds(360, 0, 80, 60);
Clear.setBounds(450, 0, 80, 60);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drLine.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drPen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drCircle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drEraser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
drRect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
Clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int x1,x2,y1,y2,width,height;
boolean First =true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new PainPen();
}
} 展开
展开全部
楼主看看是不是你想要的效果!!!
我对其中代码作了点变动
希望采纳为满意答案,嘿嘿。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* 作了一点小改动,主类本身实现ActionListener接口
*/
public class PainPen extends JFrame implements ActionListener {
JButton drLine;
JButton drPen;
JButton drCircle;
JButton drEraser;
JButton drRect;
JButton Clear;
Object obj;
JPanel p;
PainPen() {
super("画图");
p = new JPanel();
Container c = getContentPane();
c.add(p, BorderLayout.CENTER);
p.setBackground(Color.WHITE);
drLine = new JButton("画直线");
drPen = new JButton("画笔");
drCircle = new JButton("画圆");
drEraser = new JButton("橡皮");
drRect = new JButton("画矩形");
Clear = new JButton("清除");
p.add(drLine);
p.add(drPen);
p.add(drCircle);
p.add(drEraser);
p.add(drRect);
p.add(Clear);
drLine.setBounds(0, 0, 80, 60);
drPen.setBounds(90, 0, 80, 60);
drCircle.setBounds(180, 0, 80, 60);
drEraser.setBounds(270, 0, 80, 60);
drRect.setBounds(360, 0, 80, 60);
Clear.setBounds(450, 0, 80, 60);
// 为按钮添加事件监听
drLine.addActionListener(this);
drPen.addActionListener(this);
drCircle.addActionListener(this);
drEraser.addActionListener(this);
drRect.addActionListener(this);
Clear.addActionListener(this);
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
/*
* 下面的只写了三个按钮,就不出写下去啦,也不知道橡皮和画笔是咋样的,哈哈
*/
Graphics gg = this.getGraphics();
obj = e.getSource();
if (obj == drLine) {
gg.drawLine(0, 200, 500, 200);//随便画一条直线
} else if (obj == drRect) {
gg.drawRect(300, 300, 50, 30);//随便画一个矩形
} else if (obj == Clear) {
//清除所有画出来的图形
gg.clearRect(0, 200, 500, 200);
paint(gg); //这一句很重要,调用paint方法,要不点删除时会画一个大矩形出来的
}
// 这里可以继续写下去,就不写了
}
// public void paint(Graphics g){
// }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new PainPen();
}
}
我对其中代码作了点变动
希望采纳为满意答案,嘿嘿。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* 作了一点小改动,主类本身实现ActionListener接口
*/
public class PainPen extends JFrame implements ActionListener {
JButton drLine;
JButton drPen;
JButton drCircle;
JButton drEraser;
JButton drRect;
JButton Clear;
Object obj;
JPanel p;
PainPen() {
super("画图");
p = new JPanel();
Container c = getContentPane();
c.add(p, BorderLayout.CENTER);
p.setBackground(Color.WHITE);
drLine = new JButton("画直线");
drPen = new JButton("画笔");
drCircle = new JButton("画圆");
drEraser = new JButton("橡皮");
drRect = new JButton("画矩形");
Clear = new JButton("清除");
p.add(drLine);
p.add(drPen);
p.add(drCircle);
p.add(drEraser);
p.add(drRect);
p.add(Clear);
drLine.setBounds(0, 0, 80, 60);
drPen.setBounds(90, 0, 80, 60);
drCircle.setBounds(180, 0, 80, 60);
drEraser.setBounds(270, 0, 80, 60);
drRect.setBounds(360, 0, 80, 60);
Clear.setBounds(450, 0, 80, 60);
// 为按钮添加事件监听
drLine.addActionListener(this);
drPen.addActionListener(this);
drCircle.addActionListener(this);
drEraser.addActionListener(this);
drRect.addActionListener(this);
Clear.addActionListener(this);
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
/*
* 下面的只写了三个按钮,就不出写下去啦,也不知道橡皮和画笔是咋样的,哈哈
*/
Graphics gg = this.getGraphics();
obj = e.getSource();
if (obj == drLine) {
gg.drawLine(0, 200, 500, 200);//随便画一条直线
} else if (obj == drRect) {
gg.drawRect(300, 300, 50, 30);//随便画一个矩形
} else if (obj == Clear) {
//清除所有画出来的图形
gg.clearRect(0, 200, 500, 200);
paint(gg); //这一句很重要,调用paint方法,要不点删除时会画一个大矩形出来的
}
// 这里可以继续写下去,就不写了
}
// public void paint(Graphics g){
// }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new PainPen();
}
}
追问
我想要那种点击按钮就可以按我我自己的意思随便画图的程序,可以帮我在改改吗?麻烦了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ShowButton implements ActionListener{
private JTextField textField = null;
private JButton button = null;
private JFrame frame = null;
public ShowButton(){
frame = new JFrame("显示按钮文字");
button = new JButton("3");
textField = new JTextField("",20);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.add(textField);
button.addActionListener(this);
frame.add(button);
frame.setSize(400, 100);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField.setText(button.getActionCommand());
System.out.println(button.getActionCommand()+"===");
}
public static void main(String[] args) {
new ShowButton();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ShowButton implements ActionListener{
private JTextField textField = null;
private JButton button = null;
private JFrame frame = null;
public ShowButton(){
frame = new JFrame("显示按钮文字");
button = new JButton("3");
textField = new JTextField("",20);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.add(textField);
button.addActionListener(this);
frame.add(button);
frame.setSize(400, 100);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField.setText(button.getActionCommand());
System.out.println(button.getActionCommand()+"===");
}
public static void main(String[] args) {
new ShowButton();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询