62,621
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import javax.swing.*;
public class CrossLight extends JPanel{
private int width=getWidth();
private int height=getHeight();
private boolean Red,Yellow,Green;
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();
}
protected void paintComponent(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);
}
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);
}
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);
}
}
public Dimension getPreferredSize(){
return new Dimension(60,90);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ControlLight extends JFrame{
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);
add(jpbutton,BorderLayout.SOUTH);
add(light);
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);
}
}
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);
}
}