자바에서 panel 출력이안되는데.어떻게해야하냐요 여기서,,ㅠㅠ
가지
안녕하세여 ㅋ
공부하다가 하노이탑이란걸 만들려고하는데
다른 패널이랑 버튼이 나오는데
actionperform 받앗는데 작동도안되고
패널도 출력이 안되네요ㅜㅜ
점만도와주세요
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class HanoiTower {
public static void main (String[] args){
new hanoi();
}
}
class hanoi extends JFrame {
//number of disks
int num = 3;
public hanoi(){
init();
}
private void init(){
JFrame mF = new JFrame();
mF.setSize(500,500);
Container contentPane = mF.getContentPane();
p3.setLayout(new GridLayout(1,13));
p3.setLayout(new FlowLayout(FlowLayout.LEFT));//south
for(int i=0;i13;i++) {
if(i==2) {
column = new JLabel(Column A,JLabel.CENTER);
column.setForeground(Color.red);
column.setFont(new Font(null, 0 ,12));
p3.add(column);
}else if(i==7) {
column = new JLabel(Column B,JLabel.CENTER);
column.setForeground(Color.yellow);
column.setFont(new Font(null, 0 ,12));
p3.add(column);
}else if(i==12) {
column = new JLabel(Column C,JLabel.CENTER);
column.setForeground(Color.green);
column.setFont(new Font(null, 0 ,12));
p3.add(column);
}else {
column = new JLabel(); // 여백주기
p3.add(column);
}
}
JButton move = new JButton();
move.setText(move);
move.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
moveActionPerformed(evt);
}});
p2.add(move);//p3 south
p.setLayout(new GridLayout(1,3));
contentPane.add(p,BorderLayout.CENTER);
contentPane.add(p2,BorderLayout.EAST);
contentPane.add(p3,BorderLayout.SOUTH);
mF.pack();
mF.setVisible(true);
mF.setBackground(Color.white);
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void moveActionPerformed(ActionEvent evt) {
if(evt.getSource() == move){
p_a = new JPanel();
p_a.setLayout(new GridLayout(3,0));
p_b = new JPanel();
p_b.setLayout(new GridLayout(3,0));
p_c = new JPanel();
p_c.setLayout(new GridLayout(3,0));
makeJL(num); // 레이블 생성
settingJL(num); // 레이블 등록 및 리페인팅
move.setEnabled(true); // Start버튼
hanoi(a ,b ,c ,num);
p.invalidate();
p.validate();
p.repaint();
}
}
/*
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==move){
p_a = new JPanel();
p_a.setLayout(new GridLayout(3,0));
p_b = new JPanel();
p_b.setLayout(new GridLayout(3,0));
p_c = new JPanel();
p_c.setLayout(new GridLayout(3,0));
makeJL(3); // 레이블 생성
settingJL(3); // 레이블 등록 및 리페인팅
move.setEnabled(true); // Start버튼
hanoi(a,b,c,3); // hanoiTower메서드 호출
}
*/
private void makeJL(int num) {
a = new JLabel[num];
b = new JLabel[num];
c = new JLabel[num];
for(int i = 1 ; i = num ; i++){
a[i-1]= new JLabel();
a[i-1].setFont(new Font(null ,0, 12));
a[i-1].setBackground(Color.green);
b[i-1]=new JLabel();
b[i-1].setFont(new Font(null ,0, 12));
b[i-1].setBackground(Color.yellow);
c[i-1]=new JLabel();
c[i-1].setFont(new Font(null ,0, 12));
c[i-1].setBackground(Color.red);
}
}
void hanoi(JLabel a[], JLabel b[], JLabel c[],int n) {
if(n==1) {
move(a,c);
}
else {
hanoi(a,c,b,n-1);
move(a,c);
hanoi(b,a,c,n-1);
}
}
private void move(JLabel[] go, JLabel[] where) {
JLabel temp;
int i,j,k=0; // 루프 제어변수
for (i=0;i3;i++) {
if(go[i].getText()!=) { // 출발기둥의 쟁반을 위에서부터 조사.. 공백이 아니면..
for (j=2;j=0;j--) {
if(where[j].getText()==) { // 목적기둥의 각 칸을 아래에서부터 조사.. 공백이면..
/* 스와핑 */
temp = go[i];
go[i] = where[j];
where[j] = temp;
k=1; // 2차루프 탈출을 위한 값 대입
break; // j 루프탈출
}
}
}
if(k==1)
break; // 만족할시 i루프탈출
}
settingJL(3); // 레이블 등록 및 리페인팅
}
private void settingJL(int num) {
for(int i=0;inum;i++) {
p_a.add(a[i]);
p_b.add(b[i]);
p_c.add(c[i]);
}
/* 센터패널에 각 패널 붙이기 */
p.add(p_a);
p.add(p_b);
p.add(p_c);
/* 센터패널 리페인팅 */
p.repaint();
}
private JPanel p = new JPanel();
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p_a = new JPanel();
private JPanel p_b = new JPanel();
private JPanel p_c = new JPanel();
JLabel a[] = new JLabel[3];
JLabel b[] = new JLabel[3];
JLabel c[] = new JLabel[3];
private JLabel column = new JLabel();
private JButton move = new JButton();
}