JPanel 전환
조회수 1860회
이렇게 jpanel을 전환하고 싶은데 결과창은 클릭 버튼을 눌러버리면 버튼이 눌러진상태로 패널이 안바뀝니다 ㅜㅜ 검색을 많이 했는데 잘안되네요.. 도와주실수있을까요
import javax.swing.; import java.awt.; import java.awt.event.*;
class MyActionListener implements ActionListener { JFrame frame; JButton button; JLabel label; public MyActionListener(JFrame f1,JButton b1,JLabel l1){ this.frame = f1; this.button = b1; this.label = l1; } public void actionPerformed(ActionEvent e) { executive ex = new executive(); JButton b = (JButton) e.getSource(); if (b.getText().equals("click")){ ex.newlabel(); } } }
class executive extends event{ public void click(){ MyActionListener ac = new MyActionListener(f1,b1,l1); b1.addActionListener(ac); //바로 actionpoerfomed로??? } public void newlabel(){ f1.getContentPane().removeAll(); f1.getContentPane().add(p3); revalidate(); repaint(); } }
class event extends JFrame{ static JFrame f1 = new JFrame("event"); static JButton b1 = new JButton("click"); static JLabel l1 = new JLabel("success"); static JPanel p1 = new JPanel(); static JPanel p2 = new JPanel(); static JPanel p3 = new JPanel(); public static void main(String [] args){ f1.setLocation(200,400); //창 위치 f1.setPreferredSize(new Dimension(600,400)); //창 크기 b1.setFont(new Font("Gothic", Font.ITALIC, 80)); //글자 폰트 p2.add(b1,BorderLayout.SOUTH); //아래에다 버튼 추가 p3.add(l1); p1.add(p2); f1.add(p1); f1.pack(); //프레임내에 서브컴포넌트들의 레이아웃과 Prefered Size에 맞도록 윈도우의 사이즈를 맞추는 작업 f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //자바 창을 닫으면 같이종료 f1.setVisible(true); MyActionListener my = new MyActionListener(f1,b1,l1); executive ex = new executive(); ex.click();
}
}
댓글 입력