ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 23일 차 Swing 입력하고 출력하기 조금더 예쁘게
    삼성SDS_멀티캠퍼스/Java 2015. 10. 12. 16:14
    반응형


    public class CCar {

    private String color;

    private int speed;

    private int mileage;

    //수식어는 public

    //반환유형자체가 없음

    //함수명이 클래스명과 동일

    public CCar(String c , int s, int m)

    {

    System.out.println("객체가 생성될땐 이 문장이 실행되야함");

    color = c;

    speed = s;

    mileage = m;

    }

    public String getColor() {

    return color;

    }

    public void setColor(String color) {

    this.color = color;

    }

    public int getSpeed() {

    return speed;

    }

    public void setSpeed(int speed) {

    this.speed = speed;

    }

    public int getMileage() {

    return mileage;

    }

    public void setMileage(int mileage) {

    this.mileage = mileage;

    }

    @Override

    public String toString() {

    return "Car [color=" + color + ", speed=" + speed + ", mileage="

    + mileage + "]";

    }

    }



    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;




    class CarFrame extends JFrame implements ActionListener{
    //사용할 컴포넌트 참조변수 등록
    private JLabel colorLabel, speedLabel, mileageLabel;
    private JTextField colorTf, speedTf, mileageTf;
    private JButton inputBtn, outputBtn;
    private JLabel resultLabel;
    private List<CCar> carList;
    public CarFrame(){
    carList = new ArrayList<CCar>();
    //Frame기본 세팅
    this.setSize(300,200 );
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //사용할 컴포넌트 객체화
    colorLabel = new JLabel("색상 : ");
    speedLabel = new JLabel("속도 : ");
    mileageLabel = new JLabel("주행거리 : ");
    colorTf = new JTextField(8);
    speedTf = new JTextField(4);
    mileageTf = new JTextField(4);
    inputBtn = new JButton("입력");
    outputBtn = new JButton("출력");
    resultLabel = new JLabel();
    //컴포넌트 속성값 지정
    inputBtn.addActionListener(this);
    outputBtn.addActionListener(this);
    //컴포넌트 박기
    JPanel panel = new JPanel();
    panel.add(colorLabel);
    panel.add(colorTf);
    panel.add(speedLabel);
    panel.add(speedTf);
    panel.add(mileageLabel);
    panel.add(mileageTf);
    panel.add(inputBtn);
    panel.add(outputBtn);
    panel.add(resultLabel);
    this.add(panel);
    this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if( e.getSource() == inputBtn)
    {
    String color = colorTf.getText();
    int speed = Integer.parseInt(speedTf.getText());
    int mileage = Integer.parseInt(mileageTf.getText());
    carList.add(new CCar(color, speed, mileage));
    }
    else if(e.getSource() == outputBtn)
    {
    String result = "<html>";
    for(CCar c : carList)
    {
    result += c.toString() +"<br>";
    }
    result += "</html>";
    resultLabel.setText(result);
    }
    }
    }


    public class CarTest {
    public static void main(String[] args) {
     new CarFrame();
    }
    }






    반응형
Designed by Tistory.