2.01.2018

JAVA Traffic2

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 trafficPanelnew 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");
                final JLabel red new JLabel(redImg); //final을 붙이면 값이 고정됨
                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");
                final JLabel yellow = new JLabel(yellowImg);
                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.setHorizontalAlignment(SwingConstants.CENTER);
                green.setBorder(new LineBorder(Color.black, 1));
                green.setLocation(0, 240);
                green.setSize(120, 120);

                trafficPanel.add(green);

                //JLabel - no signal
                ImageIcon nosignalImg new ImageIcon("img/nosignal.png");
                JLabel nosignalnew JLabel(nosignalImg);
                nosignal.setHorizontalAlignment(SwingConstants.CENTER);
                nosignal.setBorder(new LineBorder(Color.black, 1));
                nosignal.setSize(120, 120);

                trafficPanel.add(nosignal);

                // 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(nosignalImg);
                                        yellow.setIcon(yellowImg);
                                } else if (red.getIcon().toString().equals("img/red.png")){ //
                                        yellow.setIcon(nosignalImg);
                                        green.setIcon(greenImg);
else if (red.getIcon().toString().equals("img/red.png")){ //
                                        red.setIcon(nosignalImg);
                                        yellow.setIcon(yellowImg);
                                }
                        }
  });

                //button.addActionListener(new MyActionListener(red))
                buttonPanel.add(button);

                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setVisible(true);
        }

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

}

댓글 없음:

댓글 쓰기