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();

}

}

  • 아...질문 처음인데 코드 정리 어떻게 하는거지... 박준영 2019.2.6 06:35

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)