Java 클래스 인 LAN gomoku/5에 대한 프로젝트를 시작했습니다. 게임 보드는 버튼 (JButton)으로 채워진 2 차원 배열로 표현됩니다. 이벤트 핸들러 (클래스 clickHandler)를 사용하면 클릭 한 버튼 (clickHandler 객체의 매개 변수)에 타원을 그립니다. 내 다음 코드는 작동하지 않았다. (변수 g의 null 값을 제거하는 방법을 모르겠다.) 조언을 해 주시면 감사하겠습니다. 정말 고마워.클릭하면 JButton에 타원을 그리기
class clickHandler implements ActionListener {
JButton button;
Dimension size;
Graphics g;
public clickHandler(JButton button) {
this.button = button;
this.size = this.button.getPreferredSize();
}
@Override
public void actionPerformed(ActionEvent ae) {
this.g.setColor(Color.BLUE);
this.g.fillOval(this.button.getHorizontalAlignment(), this.button.getVerticalAlignment(), this.size.width, this.size.height);
this.button.paint(this.g);
this.button.setEnabled(false);
}
}
(GUI를 생성하는 클래스에서 - 버튼의 전체 게임 보드를 - 경우 clickHandler의 인스턴스 - 나는 각 버튼의 새로운 액션 청취자 지정)이 방법 :
gButton.addActionListener(new clickHandler(gButton));
이 * ". 내가 충고의 조각을 감사하겠습니다"*는 구부릴 중지하고 사각형 버튼을 타원형에게 모양의 이미지를 넣어. 나는 학교 프로젝트가 타원형 모양의 버튼을 필요로한다는 것을 매우 의심한다. b) 프로젝트 제출에 적합한 시간에 작업을 수행 할 수 있습니다. - 죽이기 - 기쁨이 아니며, 당신이 그 일을 마지막으로 (또는 전혀)하지 말 것을 제안하십시오. –