События для нескольких кнопок - Java
Формулировка задачи:
Пытаюсь потихоньку осваивать работу с событиями. Вот в чем казус: когда нажимаю кнопку "Change Text" изменяется и текст и цвет круга, хотя должен только текст. Подскажите где ошибка, или где я чего-то недопонимаю.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C {
JFrame frame;
JLabel label;
public static void main (String[] args) {
SimpleGui3C simpleGui3C = new SimpleGui3C();
simpleGui3C.go();
}
public void go () {
frame = new JFrame();
SimleGuiPanel simleGuiPanel = new SimleGuiPanel();
JButton buttonColor = new JButton("Color Change");
buttonColor.addActionListener(new ColorListener());
JButton buttonLabel = new JButton("Change Text");
buttonLabel.addActionListener(new LabelListener());
label = new JLabel("HelloWorld");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.getContentPane().add(BorderLayout.CENTER, simleGuiPanel);
frame.getContentPane().add(BorderLayout.SOUTH, buttonColor);
frame.getContentPane().add(BorderLayout.WEST, buttonLabel);
frame.getContentPane().add(BorderLayout.EAST, label);
}
class ColorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
frame.repaint();
}
}
class LabelListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Changed");
}
}
}import javax.swing.*;
import java.awt.*;
public class SimleGuiPanel extends JPanel {
public void paintComponent(Graphics graphics) {
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color color = new Color(red, green, blue);
graphics.setColor(color);
graphics.fillOval(70,70, 100,100);
}
}Решение задачи: «События для нескольких кнопок»
textual
Листинг программы
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C {
JFrame frame;
JLabel label;
SimleGuiPanel simleGuiPanel;
public static void main(String[] args) {
SimpleGui3C simpleGui3C = new SimpleGui3C();
simpleGui3C.go();
}
public void go() {
frame = new JFrame();
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
simleGuiPanel = new SimleGuiPanel(red, green, blue);
JButton buttonColor = new JButton("Color Change");
buttonColor.addActionListener(new ColorListener());
JButton buttonLabel = new JButton("Change Text");
buttonLabel.addActionListener(new LabelListener());
label = new JLabel("HelloWorld");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER, simleGuiPanel);
frame.getContentPane().add(BorderLayout.SOUTH, buttonColor);
frame.getContentPane().add(BorderLayout.WEST, buttonLabel);
frame.getContentPane().add(BorderLayout.EAST, label);
frame.setVisible(true);
}
class ColorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
simleGuiPanel.setR((int) (Math.random() * 255));
simleGuiPanel.setG((int) (Math.random() * 255));
simleGuiPanel.setB((int) (Math.random() * 255));
frame.repaint();
}
}
class LabelListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Changed");
}
}
}
import javax.swing.*;
import java.awt.*;
public class SimleGuiPanel extends JPanel {
int r, g, b;
SimleGuiPanel(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public void paintComponent(Graphics graphics) {
Color color = new Color(r, g, b);
graphics.setColor(color);
graphics.fillOval(70, 70, 100, 100);
}
public void setR(int r) {
this.r = r;
}
public void setG(int g) {
this.g = g;
}
public void setB(int b) {
this.b = b;
}
}