package defaultpackage;import java.awt.*;
import javax.swing.*;public class CrossLight extends JPanel {
/**
 * 
 */
private static final long serialVersionUID = 3545860909727139358L; private int width = 200; private int height = 200; private boolean Red, Yellow, Green;

public CrossLight(){
this.setSize(200, 200);
this.setLocation(100, 100);
setVisible(true);
} private boolean isRed() {
return Red;
} protected void setRed(boolean Red) {
this.Red = Red;
repaint();
} protected boolean isYellow() {
return Yellow;
} protected void setYellow(boolean Yellow) {
this.Yellow = Yellow;
repaint();
} protected boolean isGreen() {
return Green;
} protected void setGreen(boolean Green) {
this.Green = Green;
repaint();
} public void paint(Graphics g) {
super.paintComponent(g);
int width_Rect = width / 3;
// int height_Rect=height/2;
int x_Rect = width / 2;
int y_Rect = height / 2;
int x_Red = x_Rect;
int y_Red = y_Rect;
int x_Yellow = x_Rect;
int y_Yellow = y_Red + width_Rect;
int x_Green = x_Rect;
int y_Green = y_Yellow + width_Rect;
g.setColor(Color.BLACK);
g.drawRect(x_Rect, y_Rect, width_Rect, 3 * width_Rect);
if (Red) {
g.setColor(Color.RED);
g.fillOval(x_Red, y_Red, width_Rect, width_Rect);
repaint();
} else {
g.drawOval(x_Red, y_Red, width_Rect, width_Rect);
}
if (Yellow) {
g.setColor(Color.YELLOW);
g.fillOval(x_Yellow, y_Yellow, width_Rect, width_Rect);
} else {
g.drawRect(x_Yellow, y_Yellow, width_Rect, width_Rect);
repaint();
}
if (Green) {
g.setColor(Color.GREEN);
g.fillOval(x_Green, y_Green, width_Rect, width_Rect);
} else {
g.drawOval(x_Green, y_Green, width_Rect, width_Rect);
repaint();
}
} public Dimension getPreferredSize() {
return new Dimension(60, 90);
}

public static void main(String[] args) {
CrossLight cl = new CrossLight();
cl.setRed(true);
JFrame jf = new JFrame();
jf.setLocation(300, 300);
jf.setSize(300, 300);
jf.add(cl);
jf.setVisible(true);
}
}
package defaultpackage;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class ControlLight extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 7839937459494006808L; private JRadioButton jrbRed = new JRadioButton("Red"); private JRadioButton jrbYellow = new JRadioButton("Yellow"); private JRadioButton jrbGreen = new JRadioButton("Green"); private CrossLight light = new CrossLight(); public ControlLight() {
JPanel jpbutton = new JPanel(new FlowLayout(FlowLayout.CENTER, 2, 0));
jpbutton.add(jrbRed);
jpbutton.add(jrbYellow);
jpbutton.add(jrbGreen);
ButtonGroup group = new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
setSize(500, 500);
setLayout(new BorderLayout());
add(jpbutton, BorderLayout.NORTH);
// light.setRed(true);
add(light, BorderLayout.CENTER);
jrbRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
light.setRed(true);
}
});
jrbYellow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
light.setYellow(true);
}
});
jrbGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
light.setGreen(true);
}
});
} public static void main(String[] args) {
ControlLight frame = new ControlLight();
frame.setTitle("ControlLight");
// frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}我改了一下,现在可以显示了。

解决方案 »

  1.   

        int width_Rect=width/3;
        //int height_Rect=height/2;
        int x_Rect=width/2;
        int y_Rect=height/2;改成下面这样:
         int width_Rect=getWidth()/3;
        //int height_Rect=height/2;
        int x_Rect=getWidth()/2;
        int y_Rect=getHeight()/2;
      

  2.   

    做swing的越来越多了。
    最近java 6 update 18 对swing优化了很多。大家可以去看看。
      

  3.   

    swing 忘光了 sorry  给点分让我升星星吧!
      

  4.   

    huangwj20042008同志,给我解析一下为什么我向下面这样写
     int width_Rect=width/3; 
        //int height_Rect=height/2; 
        int x_Rect=width/2; 
        int y_Rect=height/2; 
    所获取的值是空啊?麻烦了!!
      

  5.   


    width,height是在初始化的时候赋的值,而构建panel的时候,getWidth(),getHeight()返回的是0。