2개의 Panel 이 서로 변환되는 소스
출처 : 자바 스윙(swing) - JPanel ( 2개의 패널 전환 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 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.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @SuppressWarnings("serial") class JPanel01 extends JPanel { // 1번째 패널 private JButton jButton1; private JScrollPane jScrollPane1; private JTextArea jTextArea1; private JPanelTest win; public JPanel01(JPanelTest win) { this.win = win; setLayout(null); jButton1 = new JButton("버튼"); jButton1.setSize(70, 20); jButton1.setLocation(10, 10); add(jButton1); jTextArea1 = new JTextArea(); jScrollPane1 = new JScrollPane(jTextArea1); jScrollPane1.setSize(200, 150); jScrollPane1.setLocation(10, 40); add(jScrollPane1); jButton1.addActionListener(new MyActionListener()); } class MyActionListener implements ActionListener { // 버튼 키 눌리면 패널 2번 호출 @Override public void actionPerformed(ActionEvent e) { win.change("panel02"); } } } @SuppressWarnings("serial") class JPanel02 extends JPanel { // 2번째 패널 private JTextField textField; private JPasswordField passwordField; private JPanelTest win; public JPanel02(JPanelTest win) { setLayout(null); this.win = win; JLabel lblLbl = new JLabel("아이디:"); lblLbl.setBounds(31, 40, 67, 15); add(lblLbl); textField = new JTextField(); textField.setBounds(123, 40, 116, 21); add(textField); textField.setColumns(10); JLabel lblLbl_1 = new JLabel("암호:"); lblLbl_1.setBounds(31, 84, 67, 15); add(lblLbl_1); passwordField = new JPasswordField(); passwordField.setBounds(123, 84, 116, 21); add(passwordField); JButton btn = new JButton("버튼"); btn.setSize(70, 20); btn.setLocation(10, 10); add(btn); btn.addActionListener(new MyActionListener()); } class MyActionListener implements ActionListener { // 버튼 키 눌리면 패널 1번 호출 @Override public void actionPerformed(ActionEvent e) { win.change("panel01"); } } } @SuppressWarnings("serial") class JPanelTest extends JFrame { public JPanel01 jpanel01 = null; public JPanel02 jpanel02 = null; public void change(String panelName) { // 패널 1번과 2번 변경 후 재설정 if (panelName.equals("panel01")) { getContentPane().removeAll(); getContentPane().add(jpanel01); revalidate(); repaint(); } else { getContentPane().removeAll(); getContentPane().add(jpanel02); revalidate(); repaint(); } } } public class PanelChangeExample { public static void main(String[] args) { JPanelTest win = new JPanelTest(); win.setTitle("frame test"); win.jpanel01 = new JPanel01(win); win.jpanel02 = new JPanel02(win); win.add(win.jpanel01); win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); win.setSize(300, 300); win.setVisible(true); } } | cs |
'JAVA' 카테고리의 다른 글
[JAVA][SWING] 메뉴(JMenuBar, JMenu, JMenuItem, MenuActionListener) (0) | 2018.09.11 |
---|---|
[JAVA][SWING] Drag-and-drop 파일선택시 드레그 앤 드롭 구현 (1) | 2018.09.10 |
[JAVA][SWING]화면 중앙에 창 띄우기 (0) | 2018.09.10 |
[JAVA][SWING]JTable Row Header Example (0) | 2018.09.05 |
[JAVA][SWING]Frame In Frame (0) | 2018.07.26 |