[JAVA] Swing

원종서·2021년 10월 19일
1

swing

목록 보기
1/4

Component 클래스의 일반적으로 사용되는 메소드

Component 클래스의 메소드는 다음과 같이 Java 스윙에서 널리 사용됩니다.

There are two ways to create a frame:

  1. Frame class 객체 생성
  2. Frame class 상속
public  static  void main(String[] args){

        JFrame f = new JFrame(); //  JFrame 객체를 만든다;
        
        JButton b= new JButton("click"); //  JButton객체 만든다;
        
        b.setBounds(130,100,100,40);// x축, y축 , 너비	, 높이

        f.add(b); // 프레임에 버튼 추가하기

        f.setSize(400, 500); // 너비	, 높이
        f.setLayout(null); // 레이아웃 관리자를 사용치 않겠다.
        f.setVisible(true); // 프레임 시각화 설정

    }

Java JButton

JButton 클래스는 플랫폼 독립적 구현이 있는 레이블이 지정된 버튼을 만드는 데 사용됩니다. 버튼을 누르면 응용 프로그램에서 일부 작업이 수행됩니다. AbstractButton 클래스를 상속합니다

JButton class 선언

public class JButton extends AbstractButton implements Accessible  

   JFrame f=new JFrame("Button Example");
        JButton b=new JButton("Click Here");
     
        b.setBounds(50,100,95,30);
        f.add(b);
        f.setSize(400,400);
        f.setLayout(null);
        f.setVisible(true);

JAVA JButtonActionListener 예시

 public  static  void main(String[] args){

        JFrame f=new JFrame("Button Example");
        
        final JTextField tf = new JTextField();
        tf.setBounds(50,50,150,20);
        
        JButton b=new JButton("Click Here");
        b.setBounds(50,100,95,30);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tf.setText("Welcome to JavatPoint");
            }
        });
        f.add(tf);
        f.add(b);
        
        f.setSize(400,400);
        f.setLayout(null);
        f.setVisible(true);

    }

JButton b= new JButton(new ImageIcon(${IMAGE_PATH}));

JLabel

  • JLabel은 컨테이너에 텍스트를 표시하기 위한 컨테이너이다.
  • read only 단일 라인을 표시한다.
  • 어플리케이션을 통해 텍스트 변경은 가능하지만, 유저가 직접적으로 편집할 수 없다.
  • 는 JComponent를 상속받는다.

JLabel 선언

public class JLabel extends JComponent implements SwingConstants, Accessible  

 public  static  void main(String[] args){

       JFrame f = new JFrame("Label Example");
       JLabel l1, l2;
       l1 = new JLabel("First Label");
       l1.setBounds(50,50,100,30);
        l2 = new JLabel("Second Label");
        l2.setBounds(50,100,100,30);
        f.add(l1); f.add(l2);
        f.setSize(300,300);
        f.setLayout(null);
        f.setVisible(true);
    }

JLabel Example with ActionListenr


public class LabelExample extends Frame implements ActionListener {

    JTextField tf; JLabel l ; JButton b;
    LabelExample(){
        tf = new JTextField();
        tf.setBounds(50,50,150,20);
        l = new JLabel();
        l.setBounds(50,100,250,20);
        b= new JButton("Find IP");
        b.setBounds(50,150,95,30);
        b.addActionListener(this);
        add(b); add(tf); add(l);
        setSize(400,400);
        setLayout(null);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

            String host = tf.getText();
            try {
                String ip=java.net.InetAddress.getByName(host).getHostAddress();
                l.setText("Ip of " + host + " is "+ ip);
            } catch (UnknownHostException unknownHostException) {
                unknownHostException.printStackTrace();
                System.out.println(unknownHostException);
            }

    }
    public static  void main(String[] args){
        new LabelExample();
    }
}

JTextField

  • 편집 불가능한 JLabel과 달리 편집 가능한 단일 텍스르 라인
  • JTextComponent 를 상속받은

JTextField 상속

public class JTextField extends JTextComponent implements SwingConstants

 public static  void main(String[] args){
        JFrame f= new JFrame("TextField Example");
        JTextField t1,t2;
        t1=new JTextField("Welcome to Javatpoint.");
        t1.setBounds(50,100, 200,30);
        t2=new JTextField("AWT Tutorial");
        t2.setBounds(50,150, 200,30);
        f.add(t1); f.add(t2);
        f.setSize(400,400);
        f.setLayout(null);
        f.setVisible(true);

    }

JTextField Example with ActionListener


public class TextFieldExample implements ActionListener {
    JTextField tf1, tf2, tf3;
    JButton b1, b2;
    TextFieldExample(){
        JFrame f= new JFrame();
        tf1 = new JTextField();
        tf1.setBounds(50,50,150,20);
        tf2 = new JTextField();
        tf2.setBounds(50,100,150,20);

        tf3 = new JTextField();
        tf3.setBounds(50,150,150,20);

        tf3.setEnabled(false); // tf3 임의 변경 불가능

        b1 = new JButton("+");
        b1.setBounds(50,200,50,50);
        b2 = new JButton("-");
        b2.setBounds(120,200,50,50);
        b1.addActionListener(this);
        b2.addActionListener(this);
        f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
        f.setSize(300,300);
        f.setLayout(null);
        f.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String s1 = tf1.getText();
        String s2 = tf2.getText();
        String minus = b2.getText();
        int x = Integer.parseInt(s1);
        int y = Integer.parseInt(s2);

        int result = 0;

        if(e.getSource()== b1){
            result = x + y;
        }
        else if(e.getActionCommand().equals(minus)){
            result = x-y;
        }
        String stringResult = String.valueOf(result);
        tf3.setText(stringResult);
    }
    public  static  void main(String[] args){
        new TextFieldExample();
    }
}

0개의 댓글