2.01.2018

JAVA traffic

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

public class SignalLamp extends JFrame {
        SignalLamp() {
                setTitle("TRAFFIC");
                setSize(200, 500);
                setLayout(null);

                // JPanel
                JPanel trafficPanel = new JPanel();
                trafficPanel.setBorder(new LineBorder(Color.black, 3));
                trafficPanel.setLayout(null);
                trafficPanel.setLocation(34, 30);
                trafficPanel.setSize(120, 360);
                trafficPanel.setBackground(Color.black);

                this.add(trafficPanel);

                // JLabel - red signal
                ImageIcon redImg = new ImageIcon("img/red.png");
                JLabel red = new JLabel(redImg);
                red.setText("RED");
                red.setHorizontalAlignment(SwingConstants.CENTER);
                red.setBorder(new LineBorder(Color.black, 1));
                red.setLocation(0, 0);
                red.setSize(120, 120);

                trafficPanel.add(red);

                // JLabel - yellow signal
                ImageIcon yellowImg = new ImageIcon("img/yellow.png");
                JLabel yellow = new JLabel(yellowImg);
                yellow.setText("YELLOW");
                yellow.setHorizontalAlignment(SwingConstants.CENTER);
                yellow.setBorder(new LineBorder(Color.black, 1));
                yellow.setLocation(0, 400);
                yellow.setSize(120, 120);

                trafficPanel.add(yellow);

                // JLabel - green signal
                ImageIcon greenImg = new ImageIcon("img/green.png");
                JLabel green = new JLabel(greenImg);
                green.setText("GREEN");
                green.setHorizontalAlignment(SwingConstants.CENTER);
                green.setBorder(new LineBorder(Color.black, 1));
                green.setLocation(0, 240);
                green.setSize(120, 120);

                trafficPanel.add(green);


                // JPanel buttonPanel
                JPanel buttonPanel = new JPanel();

                buttonPanel.setLayout(null);
                buttonPanel.setLocation(34, 400);
                buttonPanel.setSize(120, 30);

                this.add(buttonPanel);

                // button
JButton button = new JButton("Change Signal");
                button.setLocation(0, 0);
                button.setSize(120, 30);

                buttonPanel.add(button);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(red.getIcon().toString());
                if (red.getIcon().toString().equals("img/red.png")){ //
                        red.setIcon( Img);
                        yellow.setIcon(yellowImg);
                }
}
});

                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setVisible(true);
        }

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                SignalLamp sl = new SignalLamp();
        }

}



댓글 1개:

  1. Spins for Men - Titanium Piercing Jewelry
    The 2016 ford fusion energi titanium classic spins are made with raft titanium the finest titanium trim walmart quality premium spins. The diamond oakley titanium glasses patterning on the diamond is a surefire titanium damascus knives sign that you are

    답글삭제