저는 Java에 익숙하지 않아 방금 GUI 테스트로이 멍청한 프로그램을 작성하기 시작했습니다. 그것이 해야하는 것은 12 개의 버튼을 가지고 있고, 모두 흰색으로 설정하고, 3 개의 무작위 버튼을 검정색으로 만들고, 모든 버튼을 다시 흰색으로 설정하고, 1 초를 기다린 다음 반복합니다. 문제는 반복 할 수없는 것입니다. 랜덤 버튼을 검정색으로 만드는 코드 섹션 주위에 for 나 for 루프를 넣으려고 할 때마다 단순히 실행되지 않습니다. 오류가 발생하지 않으며 프로세스 자체가 실행되지만 창이 표시되지 않습니다. 이 클래스의 코드입니다 (가져 오기 문을 제외한) :GUI에서 루프 문제 (java)
public class testingness extends JFrame {
JButton one, two, three, four, five, six, seven, eight, nine, ten, eleven,
twelve;
JPanel panel;
testingness(String title) {
super(title);
this.init();
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
void init() {
panel = new JPanel();
panel.setLayout(new GridLayout(3, 4));
one = new JButton();
one.setBackground(Color.white);
two = new JButton();
two.setBackground(Color.white);
three = new JButton();
three.setBackground(Color.white);
four = new JButton();
four.setBackground(Color.white);
five = new JButton();
five.setBackground(Color.white);
six = new JButton();
six.setBackground(Color.white);
seven = new JButton();
seven.setBackground(Color.white);
eight = new JButton();
eight.setBackground(Color.white);
nine = new JButton();
nine.setBackground(Color.white);
ten = new JButton();
ten.setBackground(Color.white);
eleven = new JButton();
eleven.setBackground(Color.white);
twelve = new JButton();
twelve.setBackground(Color.white);
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
panel.add(five);
panel.add(six);
panel.add(seven);
panel.add(eight);
panel.add(nine);
panel.add(ten);
panel.add(eleven);
panel.add(twelve);
this.add(panel);
while (true) {
randomness();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void randomness() {
for (int timesdone = 0; timesdone < 4; timesdone++) {
panel.update(panel.getGraphics());
Random r = new Random();
int rand = r.nextInt(12);
if (rand == 0) {
one.setBackground(Color.black);
} else if (rand == 1) {
two.setBackground(Color.black);
} else if (rand == 2) {
three.setBackground(Color.black);
} else if (rand == 3) {
four.setBackground(Color.black);
} else if (rand == 4) {
five.setBackground(Color.black);
} else if (rand == 5) {
six.setBackground(Color.black);
} else if (rand == 6) {
seven.setBackground(Color.black);
} else if (rand == 7) {
eight.setBackground(Color.black);
} else if (rand == 8) {
nine.setBackground(Color.black);
} else if (rand == 9) {
ten.setBackground(Color.black);
} else if (rand == 10) {
eleven.setBackground(Color.black);
} else if (rand == 11) {
twelve.setBackground(Color.black);
}
one.setBackground(Color.white);
two.setBackground(Color.white);
three.setBackground(Color.white);
four.setBackground(Color.white);
five.setBackground(Color.white);
six.setBackground(Color.white);
seven.setBackground(Color.white);
eight.setBackground(Color.white);
nine.setBackground(Color.white);
ten.setBackground(Color.white);
eleven.setBackground(Color.white);
twelve.setBackground(Color.white);
}
}
}
여기서 내가 뭘 잘못하고 있니?
일부 중단 점 및 모니터에 넣어 너의 행동 프로그램. 그것은 시도를 호출하는 것입니다, 귀하의 모든 setbackground 호출을 호출하는 아마도 어쩌면 그것은 단지 새로 고침 디스플레이. 먼저 논리를 확인해 보겠습니다. – mwjohnson
(http://stackoverflow.com/questions/18266087/change-jlabel-background-on-mouserelease-event/18293834#18293834) – MadProgrammer
당신은 update' 또는 '페인트'를 호출해서는 안 [예]에서 살펴 본다보십시오 '디렉토리,이 panel.update (panel.getGraphics());는 매우 나쁨 – MadProgrammer