Java - Swing 기초

예제 1)

Main 클래스

public class Main {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyFrame();		
	}

}

JFrame 클래스를 상속받는 MyFrame 클래스.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class MyFrame extends JFrame {
	MyFrame() {
		setTitle("test");
		setSize(600,600);
		
		Container cp = getContentPane();
		
//		JButton btn = new JButton("버튼");
//		cp.add(btn);

		MyPanel p1 = new MyPanel();		
		JPanel p2 = new JPanel();		
		cp.setLayout(new BorderLayout()); // 레이아웃 종류들 중요한거만 기억해두기
		
		cp.add(p1, BorderLayout.CENTER);
		cp.add(p2, BorderLayout.SOUTH);
		
		JLabel lb1 = new JLabel("x1:");
		JLabel lb2 = new JLabel("x2:");
		JLabel lb3 = new JLabel("y1:");
		JLabel lb4 = new JLabel("y2:");
		JTextField t1 = new JTextField(3);
		JTextField t2 = new JTextField(3);
		JTextField t3 = new JTextField(3);
		JTextField t4 = new JTextField(3);
		
		p2.add(lb1);p2.add(t1);
		p2.add(lb2);p2.add(t2);
		p2.add(lb3);p2.add(t3);
		p2.add(lb4);p2.add(t4);
		
		JButton btn1 = new JButton("버튼");
		p2.add(btn1);
		
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s1 = t1.getText();
				String s2 = t2.getText();
				String s3 = t3.getText();
				String s4 = t4.getText();
				
				int i1 = Integer.parseInt(s1);
				int i2 = Integer.parseInt(s2);
				int i3 = Integer.parseInt(s3);
				int i4 = Integer.parseInt(s4);				
				p1.setData(i1,i2,i3,i4);
				p1.repaint();
				cp.revalidate();
			}
		});
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
}

JPanel 클래스를 상속받는 MyPanel 클래스

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MyPanel extends JPanel{
	private int x1, x2, y1, y2;
	
	public void setData(int x1, int y1, int x2, int y2) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}
	
	public int getX1() {
		return x1;
	}
	public void setX1(int x1) {
		this.x1 = x1;
	}
	public int getX2() {
		return x2;
	}
	public void setX2(int x2) {
		this.x2 = x2;
	}
	public int getY1() {
		return y1;
	}
	public void setY1(int y1) {
		this.y1 = y1;
	}
	public int getY2() {
		return y2;
	}
	public void setY2(int y2) {
		this.y2 = y2;
	}
	public void paint(Graphics g) {
		
		g.setColor(Color.RED);
		g.drawLine(x1,y1,x2,y2);
	}
}

예제2)

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class MyFrame extends JFrame {
	
	MyFrame() {
		setTitle("JFrame");
		setSize(500,500);	
		setLayout(new BorderLayout());
		
		Container c = getContentPane();
		JTextField jtf = new JTextField();
		JTextArea jta = new JTextArea();
		JButton btn = new JButton("Button");
		
		c.add(jtf,BorderLayout.NORTH);
		c.add(jta,BorderLayout.CENTER);
		c.add(btn,BorderLayout.SOUTH);
		
		btn.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {			
			jta.append(jtf.getText() + "\n");
			jtf.setText("");
			}
		});
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}

public class Main {
	public static void main(String[] args) {
		new MyFrame();
	}
	
}

버튼을 누르지 않고, TextField에서 엔터를 입력받으면, 텍스트를 Area로 옮기도록 함.

// ...	
	MyFrame() {
		// ...
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			jta.append(jtf.getText() + "\n");
			jtf.setText("");
			}
		});
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		jtf.addActionListener(new ActionListener() { // 추가.
			public void actionPerformed(ActionEvent e) {
			jta.append(jtf.getText() + "\n");
			jtf.setText("");
			}
		});
	}
}

// ...

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

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

class MyFrame extends JFrame {
	
	MyFrame() {
		setTitle("JFrame");
		setSize(500,500);	
		Container c = getContentPane();
		c.setLayout(null);
		
		JPanel pg1 = new JPanel();
		pg1.setLayout(null);
		JPanel pg2 = new JPanel();
		pg2.setLayout(null);
		c.add(pg1);
		c.add(pg2);
		
		pg1.setBounds(0,0,500,500);
		pg2.setBounds(0,0,500,500);
		
		JTextField jtf = new JTextField();
		JTextArea jta = new JTextArea();
		JButton btn1 = new JButton("Button");
		JButton btn2 = new JButton("Button2");
		JLabel lbl = new JLabel("Welcome");
		
		jtf.setBounds(5,5,470,20);
		jta.setBounds(5,28,470,400);
		btn1.setBounds(5,435,470,20);
		btn2.setBounds(5,435,470,20);
		lbl.setBounds(5,5,470,400);
		
		pg1.add(jtf);
		pg1.add(jta);
		pg1.add(btn1);
		
		pg2.add(btn2);
		pg2.add(lbl);
		
		pg1.setVisible(true);
		pg2.setVisible(false);
		
		// Event 처리
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pg1.setVisible(false);
				pg2.setVisible(true);
			}
		});
		
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pg2.setVisible(false);
				pg1.setVisible(true);
			}
		});
		
		jtf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jta.append(jtf.getText() + "\n");
				jtf.setText("");
			}
		});
		
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
}

public class Main {
	public static void main(String[] args) {
		new MyFrame();
	}
	
}

---

예제 3)
익명 클래스 활용

```java
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
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.JTextArea;
import javax.swing.JTextField;

class MyFrame extends JFrame {
	
	MyFrame() {
		setTitle("JFrame");
		setSize(500,500);	
		Container c = getContentPane();
		c.setLayout(null);
		
		JPanel pg1 = new JPanel();
		JPanel pg2 = new JPanel() {
			Image bgImage = new ImageIcon(Main.class.getResource("../images/wood-3302802_1280.jpg")).getImage();		
			public void paint(Graphics g) {
				g.drawImage(bgImage, 0, 0, null);
				g.setColor(Color.YELLOW);
				g.drawRect(100, 100, 100, 100);
				paintComponents(g);
			}
		};
		
		pg1.setLayout(null);
		pg2.setLayout(null);
		
		c.add(pg1);
		c.add(pg2);
		
		pg1.setBounds(0,0,500,500);
		pg2.setBounds(0,0,500,500);
		
		JTextField jtf = new JTextField();
		JTextArea jta = new JTextArea();
		JButton btn1 = new JButton("Button");
		JButton btn2 = new JButton("Button2");
		JLabel lbl = new JLabel("Welcome");
		
		jtf.setBounds(5,5,470,20);
		jta.setBounds(5,28,470,400);
		btn1.setBounds(5,435,470,20);
		btn2.setBounds(5,435,470,20);
		lbl.setBounds(5,5,470,400);
		
		pg1.add(jtf);
		pg1.add(jta);
		pg1.add(btn1);
		
		pg2.add(btn2);
		pg2.add(lbl);
		
		pg1.setVisible(true);
		pg2.setVisible(false);
		
		// Event 처리
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pg1.setVisible(false);
				pg2.setVisible(true);
			}
		});
		
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pg2.setVisible(false);
				pg1.setVisible(true);
			}
		});
		
		jtf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jta.append(jtf.getText() + "\n");
				jtf.setText("");
			}
		});
		
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
}

public class Main {
	public static void main(String[] args) {
		new MyFrame();
	}
	
}

차이점 비교 : 익명 클래스 내부 클래스 독립 클래스

태그:

업데이트:

댓글남기기