跪求Java高手补写程序,我想(Graphics g)先画好所有再添加到各个button监听器中

2024-11-25 12:41:53
推荐回答(2个)
回答1:

楼主看看是不是你想要的效果!!!
我对其中代码作了点变动
希望采纳为满意答案,嘿嘿。
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();
}

}

回答2:

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();
}

}