0
Java GUI形式のゲームマスタを再作成しようとしています。私の問題は、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