这是我的代码,帮我看一下改怎样改
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class PushCounterPanel extends JPanel
{
private JButton push;
public PushCounterPanel ()
{
push = new JButton ("Push Me!");
push.addActionListener (new ButtonListener()); add (push);
setPreferredSize (new Dimension(300, 40));
setBackground (Color.cyan);
} private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
public void paintComponent (Graphics page)
{
super.paintComponent (page);
page.setColor (Color.black);
page.drawOval (60, 60, 15, 7);
}
}
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class PushCounterPanel extends JPanel
{
private JButton push;
public PushCounterPanel ()
{
push = new JButton ("Push Me!");
push.addActionListener (new ButtonListener()); add (push);
setPreferredSize (new Dimension(300, 40));
setBackground (Color.cyan);
} private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
public void paintComponent (Graphics page)
{
super.paintComponent (page);
page.setColor (Color.black);
page.drawOval (60, 60, 15, 7);
}
}
}}
解决方案 »
- Java基础技术分享
- jsp中如何比较时间(解决给全分,急急急急急急急!!!)?
- 遍历list元素,用iterator() ,再while(hasNext()),next()有什么好处吗
- 字符串匹配问题!为什么"siteid1_"能匹配到,而"siteid6_"匹配不到!
- 我这Socket程序怎么老是有异常,救命
- 用户列表应该如何显示?
- 接口中的变量为何必须为static final
- 求URLConnection关于请求属性的详细说明
- java配置问题,没辙了
- *.jar文件的作用、如何生成jar文件、如何运行jar文件,最好有实例????
- 麻烦帮忙解决一下文件路径的问题
- socket监听问题
在函数actionPerformed里不可用再定义函数paintComponent;
还有就是我觉得这里不可以调用paintComponent这个函数;因为这个函数只用你画了这个画板就会直接调用这个函数
那你点击按钮的时候就不可以画图了;
所以我把你的程序改了改:
一下代码没用重写paintComponent这个函数;
而是直接在点击事件的函数画图
Graphics page = getGraphics();//获得一个Graphics实例,并在其上画图
page.setColor(Color.black);
page.drawOval(60, 60, 15, 15);import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class PushCounterPanel extends JPanel { private JButton push; public PushCounterPanel() {
push = new JButton("Push Me!");
push.addActionListener(new ButtonListener());
add(push);
setPreferredSize(new Dimension(300, 40));
setBackground(Color.cyan);
} private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) {
Graphics page = getGraphics();
page.setColor(Color.black);
page.drawOval(60, 60, 15, 15);
}
} public static void main(String[] args) {
PushCounterPanel sai = new PushCounterPanel();
JFrame f = new JFrame();
f.getContentPane().add(sai);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setVisible(true);
}
}