Phil Kooistra

Greenhorn
+ Follow
since Sep 15, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Phil Kooistra

ok I messed up on the copy paste from one blog to this one the code is


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Lab35C {
JFrame frame;
JButton green;
JButton red;
Dot dot;

public static void main(String[] args) {
Lab35C me = new Lab35C();
me.go();
}

public void go() {
frame = new JFrame("Watch the dot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
green = new JButton("Green dot");

green.addActionListener(this);
frame.getContentPane().add(green, BorderLayout.NORTH);

red = new JButton("Red dot");

red.addActionListener(this);
frame.getContentPane().add(red, BorderLayout.SOUTH);

dot = new Dot();
frame.getContentPane().add(dot);
frame.setVisible(true);
while (true) {
dot.repaint();
try {
Thread.sleep(50);
}
catch (Exception err) {}
}
}

class Dot extends JPanel {
Color color = Color.blue;

public void paintComponent(Graphics g) {
int height = this.getHeight();
int width = this.getWidth();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(color);
int randomX = (int) (Math.random() * width);
int randomY = (int) (Math.random() * height);
g.fillOval(randomX, randomY, 30, 30);
}

public void changeColor(Color nColor) {
color = nColor;
}
}

class GreenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.green);
}
}

class RedListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.red);
}
}
}



The compile errors I'm getting are


Lab35C.java:22: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Lab35C)
green.addActionListener(this);
^
Lab35C.java:27: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Lab35C)
red.addActionListener(this);
^
2 errors



Thanks for the help, you guys are on this quick
13 years ago
I can't figure out why this will not work, Every other example I look at show the listeners where they are in my code but it won't even compile. The program is suppose to make 2 buttons one saying red, one green and when they are pressed turn the dot that color respectively



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab35C {
JFrame frame;
JButton green;
JButton red;
Dot dot;
public static void main(String[] args) {
Lab35C me = new Lab35C();
me.go();
}
public void go() {
frame = new JFrame("Watch the dot");
frame.setDefaultCloseOperation(JFrame.EX…
frame.setSize(300, 300);
green = new JButton("Green dot");

green.addActionListener(this);
frame.getContentPane().add(green, BorderLayout.NORTH);

red = new JButton("Red dot");

red.addActionListener(this);
frame.getContentPane().add(red, BorderLayout.SOUTH);

dot = new Dot();
frame.getContentPane().add(dot);
frame.setVisible(true);
while (true) {
dot.repaint();
try {
Thread.sleep(50);
}
catch (Exception err) {}
}
}
class Dot extends JPanel {
Color color = Color.blue;
public void paintComponent(Graphics g) {
int height = this.getHeight();
int width = this.getWidth();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(color);
int randomX = (int) (Math.random() * width);
int randomY = (int) (Math.random() * height);
g.fillOval(randomX, randomY, 30, 30);
}
public void changeColor(Color nColor) {
color = nColor;
}
}
class GreenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.green);
}
}
class RedListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.red);
}
}
}


javascript:emoticon('');

Thanks a ton ahead of time
13 years ago