0
Java Master 형식의 게임 Mastermind를 다시 만들려고합니다. 내 문제는 actionListener에서 내 NewPanel 클래스에 액세스 할 수 없다는 생각이 듭니다. 어떤 도움도 좋을 것입니다. 메신저에 정말 붙어 있습니다.프로그래밍 문제 동작 리스너 -FillRect
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Mastermind extends JFrame {
public Mastermind(){
setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
add(p1, BorderLayout.WEST);
add(p2, BorderLayout.EAST);
/* PANEL 1 LAYOUT STUFF*/
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JButton red = new JButton("RED");
JButton blue = new JButton("BLUE");
JLabel HTP = new JLabel("How To Play");
JLabel htp =new JLabel("<html>Player 2 will have 10<br> "+
"attempts to correctly<br> guess Player 1's<br> "+
"Password.The<br> password will consist<br> "+
"of a 4 length sequence<br> "+
"of 4 different colors.<br> "+
"Colors may be used<br> "+
"more than once or<br> "
"not at all.</html>");
htp.getForeground();
JLabel results = new JLabel("Guesses:");
p1.setLayout(new BorderLayout());
p4.setLayout(new BorderLayout());
p5.setLayout(new GridLayout(1,2));
p1.add(p4, BorderLayout.NORTH);
p1.add(p5, BorderLayout.SOUTH);
p1.add(results, BorderLayout.CENTER);
p4.add(HTP, BorderLayout.NORTH);
p4.add(htp,BorderLayout.SOUTH);
p5.add(red);
p5.add(blue);
red.setPreferredSize(new Dimension(80,20));
/* END PANEL 1 LAYOUT STUFF*/
/* PANEL 2 LAYOUT STUFF*/
JPanel p3 = new JPanel();
JButton green = new JButton("GREEN");
JButton yellow = new JButton("YELLOW");
p2.setLayout(new BorderLayout());
p3.setLayout(new GridLayout(1,2));
p2.add(p3, BorderLayout.SOUTH);
p2.add(new NewPanel(), BorderLayout.CENTER);
p3.add(green);
p3.add(yellow);
/* END PANEL 2 LAYOUT STUFF*/
/* ACTION LISTENER EVENTS */
red.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fillRect(Color.red);
}
});
blue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
green.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
yellow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
/* END ACTION LISTENER EVENTS */
public static void main(String[] args) {
Mastermind frame = new Mastermind();
frame.setTitle("Mastermind");
frame.setSize(334, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class NewPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x,y;
for(y = 10; y<= 330; y+=35){
for(x =10; x<=120; x+=35){
g.drawRect(x, y, 25, 25);
}
}
}
}
그래서 모든 JPanels를 newPanel 클래스로 바꾸어야합니까? – Matt
이 숙제가 있습니까? – eabraham
다른 숙제로 아이디어를 얻었지만 숙제가 아닙니다. 인스턴트 메신저 gui에 새로운 그리고 그것은 재밌는 프로젝트처럼 들리 겠어 – Matt