为了账号安全,请及时绑定邮箱和手机立即绑定

JFrame frame;的位置为什么放在此处?

JFrame frame;的位置为什么放在此处?

慕粉3355806 2016-06-04 21:48:52
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class SimpleGui3C implements ActionListener { JFrame frame;  //这里要放在这里 public static void main(String[] args) { SimpleGui3C gui = new SimpleGui3C(); gui.go(); } public void go(){ frame = new JFrame();//JFrame frame = new JFrame();  写成这样子,不要上面的JFrame frame;怎么就报错了 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Change color"); button.addActionListener(this); MyDrawPanel drawPanel = new MyDrawPanel(); frame.getContentPane().add(BorderLayout.SOUTH, button); frame.getContentPane().add(BorderLayout.CENTER, drawPanel); frame.setSize(300,300); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { frame.repaint(); }}import java.awt.*;import javax.swing.JPanel;public class MyDrawPanel extends JPanel { public void paintComponent(Graphics g){ g.fillRect(0, 0, this.getWidth(), this.getHeight()); int red = (int)(Math.random() * 255); int green = (int)(Math.random() * 255); int blue = (int)(Math.random() * 255); Color randomColor = new Color(red, green, blue); g.setColor(randomColor); g.fillOval(70, 70, 100, 100); }}
查看完整描述

1 回答

?
王大厉

TA贡献58条经验 获得超11个赞

 actionPerformed(ActionEvent event) 因为你这个方法用到了 frame

查看完整回答
反对 回复 2016-06-04
  • 1 回答
  • 0 关注
  • 1687 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信