我需要在此前言,因爲我們不允許在課堂上使用IDE。我顯然錯過了一些重要的東西。我正在編寫一個程序,該程序應允許用戶單擊單選按鈕,然後將「燈光」更改爲該顏色。我以爲我正確地連接了聽衆,但顯然我忘了一些事情。有人可以幫我弄這個嗎?我錯過了什麼ActionListener?
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Lab4Frame extends JFrame {
//public boolean red, yellow, green;
Lab4Frame(){
this.setLayout(new BorderLayout());
setTitle("Lab 4 - Application #1");
Lab4Panel p = new Lab4Panel();
Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();
add(p, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
}
public static void main(String[] args){
Lab4Frame frame = new Lab4Frame();
frame.setTitle("Lab4 Application # 1");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
}
}
class Lab4RadioButtonPanel extends JPanel {
Lab4Panel canvas = new Lab4Panel();
//Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
public Lab4RadioButtonPanel() {
boolean red, green, yellow;
this.setLayout(new FlowLayout());
JRadioButton jrbRed = new JRadioButton("Red", true);
JRadioButton jrbYellow = new JRadioButton("Yellow");
JRadioButton jrbGreen = new JRadioButton("Green");
this.setBorder(BorderFactory.createLineBorder(Color.black));
ButtonGroup group = new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
this.add(jrbRed);
this.add(jrbYellow);
this.add(jrbGreen);
jrbRed.addActionListener(new RedListener(canvas));
jrbYellow.addActionListener(new YellowListener(canvas));
jrbGreen.addActionListener(new GreenListener(canvas));
}
}
class Lab4Panel extends JPanel{
public Lab4Panel(){}
boolean red, green, yellow;
int radius = 5;
int x = -1;
int y = -1;
public void setRed(){
red = true;
repaint();
}
public void setYellow(){
yellow = true;
repaint();
}
public void setGreen(){
green = true;
repaint();
}
protected void paintComponent(Graphics g){
if (x<0 || y<0) {
x = getWidth()/2 - radius;
y = getHeight()/2 - radius;
}
super.paintComponent(g);
g.drawRect(x - 10,y - 90, 40, 120);
g.drawRect(x - 5,y - 90, 40, 120);
g.setColor(Color.RED);
g.drawOval(x,y - 80, 4 * radius, 4 * radius);
g.setColor(Color.YELLOW);
g.drawOval(x,y - 40, 4 * radius, 4 * radius);
g.setColor(Color.GREEN);
g.drawOval(x,y, 4 * radius, 4 * radius);
if(red){
g.setColor(Color.RED);
g.fillOval(x,y - 80, 4 * radius, 4 * radius);
repaint();
}
else if (yellow){
g.setColor(Color.YELLOW);
g.fillOval(x,y - 40, 4 * radius, 4 * radius);
repaint();
}
if(green){
g.setColor(Color.GREEN);
g.fillOval(x,y, 4 * radius, 4 * radius);
repaint();
}
}
}
class RedListener implements ActionListener{
//private Lab4RadioButtonPanel canvas;
private Lab4Panel canvas;
RedListener(Lab4Panel canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.setRed();
}
}
class YellowListener implements ActionListener{
private Lab4Panel canvas;
YellowListener(Lab4Panel canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.setYellow();
}
}
class GreenListener implements ActionListener{
//private Lab4RadioButtonPanel canvas;
private Lab4Panel canvas;
GreenListener(Lab4Panel canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.setGreen();
}
}
爲什麼在paintComponent方法中重複調用repaint()? – 2012-02-14 02:50:36
我想要它覆蓋原來的輪廓顏色 – Robert 2012-02-14 02:51:08
我希望它繪製紅色的黃色和綠色的圓圈,然後基於用戶點擊我想要它填充圓圈 – Robert 2012-02-14 02:52:04