스윙 콤포넌트 추가 방법 1 : 기본 콤포넌트
- 추가사항
- 2019-05-09 : JTable 콤포넌트 추가 방법
- JFrame을 상속받아 MainUI 클래스 만들기
12345678910111213141516public class MainUI extends JFrame{public MainUI(){this.setTitle("201712345 홍길동");this.setSize(120, 200);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 콤포넌트 추가는 이곳에this.setVisible(true);}public static void main(String[] args) {new MainUI();}} - 콤포넌트 예제
- JButton 콤포넌트 1개 추가하기
1234567891011121314public class MainUI extends JFrame{public MainUI(){this.setTitle("201712345 홍길동");this.setSize(120, 200);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JButton btnAddr = new JButton("주소록");btnAddr.setBounds(10, 10, 100, 25);this.add(btnAddr);this.setVisible(true);}} - JButton에 ActionListener 추가하기 #1 : 버튼에 ActionListener 추가하기
1) btnAddr.addActionListener(new Ac 까지 입력하고 Ctrl+Spacebar를 클릭하여 추가
12345678910111213141516171819202122public class MainUI extends JFrame{public MainUI(){this.setTitle("201712345 홍길동");this.setSize(120, 200);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JButton btnAddr = new JButton("주소록");btnAddr.setBounds(10, 10, 100, 25);this.add(btnAddr);btnAddr.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub//new AddressUI();}});this.setVisible(true);}} - JButton에 ActionListener 추가하기 #2 : MainUI에 ActionListener 인터페이스 추가하기
1) JFrame 뒤에 implements ActionListener를 입력한다
2) class MainUI에 오류 발생으로 빨간 밑줄이 나온다.
3) MainUI위에 마우스를 올려놓은 후 Add unimplements method를 클릭한다.
4) public void actionPerformed(ActionEvent e){} 메소드가 추가된다.
12345678910111213141516171819202122232425public class MainUI extends JFrame implements ActionListener {public MainUI(){this.setTitle("201712345 홍길동");this.setSize(120, 200);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JButton btnAddr = new JButton("주소록");btnAddr.setBounds(10, 10, 100, 25);this.add(btnAddr);btnAddr.addActionListener(this);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubswitch(e.getActionCommand()){case "주소록"://new AddressUI();break;}}} - JFrame을 상속받아 AddressUI 클래스 만들기
12345678910public class AddressUI extends JFrame{public AddressUI(){this.setTitle("주소록");this.setSize(800, 600);this.setDefaultCloseOperation(HIDE_ON_CLOSE);this.setLayout(null);this.setVisible(true);}} - JLabel 추가
123JLabel lblName = new JLabel("이름");lblName.setBounds(10,10,100,25);this.add(lblName); - JTextField 추가
123JTextField txtName = new JTextField();txtName.setBounds(120, 10, 100, 25);this.add(txtName); - JRadioButton 추가
123456789101112JRadioButton rdoM = new JRadioButton("남자");rdoM.setBounds(120, 40, 100, 25);this.add(rdoM);JRadioButton rdoF = new JRadioButton("여자");rdoF.setBounds(230, 40, 100, 25);this.add(rdoF);rdoF.setSelected(true);ButtonGroup grpSex = new ButtonGroup();grpSex.add(rdoM);grpSex.add(rdoF); - JComboBox 추가 방법 1
1234String[] strHp = new String[]{"010","011","016","017","018","019"};JComboBox cmbHp = new JComboBox<String>(strHp);cmbHp.setBounds(120, 70, 100, 25);this.add(cmbHp); - JComboBox 추가 방법 2
12345678ArrayList<String> arDept = new ArrayList<String>();arDept.add("스마트미디어학과");arDept.add("게임학과");arDept.add("컴퓨터공학과");comDept = new JComboBox<String>(arDept.toArray(new String[arDept.size()]));comDept.setBounds(10, 10, 100, 25);this.add(comDept); - JComboBox에 Listener 추가와 item 변경
1234567891011121314151617181920212223242526272829303132333435String[] strOffice = new String[] { "연구동", "공학관","진리관" };comBuild = new JComboBox<String>(strOffice);comBuild.setBounds(10, 10, 80, 25);this.add(comBuild);comRoom = new JComboBox<String>();comRoom.setBounds(100, 10, 100, 25);this.add(comRoom);comBuild.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubArrayList<String> arRoom = new ArrayList<String>();if(comBuild.getSelectedItem().toString().contains("연구동")) {arRoom.add("연구동112");arRoom.add("연구동113");arRoom.add("연구동114");}if(comBuild.getSelectedItem().toString().contains("공학관")) {arRoom.add("공학관341");arRoom.add("공학관342");arRoom.add("공학관343");}if(comBuild.getSelectedItem().toString().contains("진리관")) {arRoom.add("진리관123");arRoom.add("진리관125");arRoom.add("진리관127");}comRoom.removeAllItems();for (int i = 0; i < arRoom.size(); i++) {comRoom.addItem(arRoom.get(i));}}}); - JTextArea 추가
123JTextArea txtMemo = new JTextArea();txtMemo.setBounds(10, 220, 600, 300);this.add(txtMemo); - JPanel 추가
12345JPanel panel = new JPanel();panel.setBounds(10, 10, 480, 530);panel.setBackground(Color.green);panel.setLayout(null);this.add(panel);