Java 改进JFrame和JPanel之间的通信
我正在开发一个小游戏,需要在主Java 改进JFrame和JPanel之间的通信,java,swing,user-interface,jframe,jpanel,Java,Swing,User Interface,Jframe,Jpanel,我正在开发一个小游戏,需要在主JFrame和名为gamepan的JPanel之间进行通信 游戏要求用户通过点击包含字母的按钮找到隐藏的单词 我的JFrame包含gamepan、scorepan、菜单和其他内容 mygamepan包含my按钮ArrayListener、隐藏单词和其他内容 问题是,当用户找到隐藏的单词时,我需要告诉我的JFrame他这样做了,这以后可以重新绘制gamepan,生成一个新单词 我所做的是:我在我的gamepan中添加了一个boolean,告诉我的单词是否找到,并在我的
JFrame
和名为gamepan
的JPanel
之间进行通信
游戏要求用户通过点击包含字母的按钮找到隐藏的单词
我的JFrame
包含gamepan
、scorepan
、菜单
和其他内容
mygamepan
包含my按钮ArrayListener
、隐藏单词和其他内容
问题是,当用户找到隐藏的单词时,我需要告诉我的JFrame
他这样做了,这以后可以重新绘制gamepan
,生成一个新单词
我所做的是:我在我的gamepan
中添加了一个boolean
,告诉我的单词是否找到,并在我的gamepan
中添加了一个MouseListener
,因此每次我移动鼠标时,JFrame
都会测试单词是否找到
然后,在我的gamepan
中添加了setter,如果找到隐藏的单词,我将在JFrame
中再次使用setter初始化
我发现添加一个MouseListener
不是很有效,因为我必须移动鼠标来进行更改,而且每次鼠标移动都会进行无用的处理
我想知道是否有比添加鼠标侦听器更好的解决方案??我还想知道我是否在gamepan
中添加了setter来设置新词的变量
这是我的密码
gamepan代码
public class GamePanel extends JPanel{
private JPanel leftPan = new JPanel();
private JPanel rightPan = new JPanel();
private String[] letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",};
private JButton Button[] = new JButton[26];
private JLabel label1;
private JLabel label2;
private JLabel label3;
private ActionListener buttonListener;
private JOptionPane jop = new JOptionPane();
private Word randWord = new Word(); // mot aléatoire
private TreatedWord tWord = new TreatedWord(randWord.getRandWord());// mot aléatoire traité ( etoiles et tout ça )
private char clickedButton;// lettre tappée
private boolean motTrouvé = false; // pour informer la classe Fenetre si le mot a été retrouvé ou pas on utilise ce boolean avec son getter et setter ( a savoir que cette classe est informée si le mot est trouvé ou pas via l'objet tWord)
private final List<CustomListener> customListener = new LinkedList<>(); //On crée une liste de CustomListener pour en ajouter autant qu'on veut(Via addCustomListener)
public GamePanel(){
this.setBackground(Color.white);
initGamePan();
initListeners();
this.setLayout(new BorderLayout());
this.add(leftPan,BorderLayout.WEST);
this.add(rightPan,BorderLayout.EAST);
}
public void initGamePan(){
rightPan.add(new JLabel(new ImageIcon("131869.jpg")));
label1 = new JLabel("Nombre de mots trouvés : 0");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setFont(new Font("arial",Font.BOLD,20));
label1.setPreferredSize(new Dimension(300,50));
label2 = new JLabel("Score Actuel : 0 point");
label2.setHorizontalAlignment(JLabel.CENTER);
label2.setFont(new Font("arial",Font.BOLD,20));
label2.setPreferredSize(new Dimension(300,50));
label3 = new JLabel(tWord.getStars());
label3.setHorizontalAlignment(JLabel.CENTER);
label3.setFont(new Font("arial",Font.BOLD,30));
label3.setForeground(Color.blue);
label3.setPreferredSize(new Dimension(450,50));
leftPan.add(label1);
leftPan.add(label2);
leftPan.add(label3);
for(int i=0;i<letters.length;i++){
Button[i]= new JButton(letters[i]);
leftPan.add(Button[i]);
}
leftPan.setPreferredSize(new Dimension(460,650));
leftPan.setBackground(Color.WHITE);
rightPan.setPreferredSize(new Dimension(420,650));
rightPan.setBackground(Color.WHITE);
}
public void initListeners(){
buttonListener= new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
clickedButton = ((JButton)(arg0.getSource())).getText().charAt(0); // on prend le bouton cliqué, on le convertis en string puis en char
label3.setText(tWord.treatedWord(clickedButton));// on donne a la methode tretedWord de l'objet tWord le char clickedbutton pour faire le traitement sur le mot mystère
((JButton)(arg0.getSource())).setEnabled(false);
if(tWord.isFound()==true){
jop.showMessageDialog(null, "Bravo t'a trouvé le mot !", "U don't Say B|", JOptionPane.INFORMATION_MESSAGE);
motTrouvé = true;
}
}
};
for(int i=0;i<letters.length;i++){
Button[i].addActionListener(buttonListener);
}
}
public void setNewWord(){
this.randWord = new Word();
this.tWord = new TreatedWord(randWord.getRandWord());
this.label3.setText(tWord.getStars());
}
public void resetButtons(){
for(JButton B : this.Button){
B.setEnabled(true);
}
}
public void addCustomListener(final CustomListener listener) {
this.customListener.add(listener);
}
public void notifyWordFound(/* any data you could use */) {
for(final CustomListener listener : this.customListener) {
listener.wordFound(/* any data you could use */);
}
}
}您可以使用一些在找到单词时可能发生的自定义事件:
public interface MyEventListener {
public abstract void wordFound(/* any data you could use */);
}
然后使您的GamePan
能够发出此类事件:
public class GamePan extends JPanel {
private final List<MyEventListener> myEventListeners = new LinkedList<>();
// Here, keep everything you already have
public void addMyEventListener(final MyEventListener listener) {
this.myEventListeners.add(listener);
}
private void notifyWordFound(/* any data you could use */) {
for(final MyEventListener listener : this.myEventListeners) {
listener.wordFound(/* any data you could use */)
}
}
}
在这里,我们只需调用ScorePanel#wordFound
方法即可更新ScorePanel
不要忘记,您可以通过所有这些方法传输您感兴趣的任何数据。例如,如果您想传输用户刚刚找到的单词,可以按如下方式声明MyEventListener#wordFound
方法:
public interface MyEventListener {
public abstract void wordFound(final String word);
}
编辑:添加了一些与
ScorePanel
实例的交互,以回答您在评论中的其他问题:)您可以使用一些在找到单词时可能发生的自定义事件:
public interface MyEventListener {
public abstract void wordFound(/* any data you could use */);
}
然后使您的GamePan
能够发出此类事件:
public class GamePan extends JPanel {
private final List<MyEventListener> myEventListeners = new LinkedList<>();
// Here, keep everything you already have
public void addMyEventListener(final MyEventListener listener) {
this.myEventListeners.add(listener);
}
private void notifyWordFound(/* any data you could use */) {
for(final MyEventListener listener : this.myEventListeners) {
listener.wordFound(/* any data you could use */)
}
}
}
在这里,我们只需调用ScorePanel#wordFound
方法即可更新ScorePanel
不要忘记,您可以通过所有这些方法传输您感兴趣的任何数据。例如,如果您想传输用户刚刚找到的单词,可以按如下方式声明MyEventListener#wordFound
方法:
public interface MyEventListener {
public abstract void wordFound(final String word);
}
编辑:添加了一些与
ScorePanel
实例的交互,以回答注释中的其他问题:)一般来说,是类名。此语法用于非静态的嵌套类内部。它用于访问其外部类的实例。让我举个例子:
class OuterClass {
private final String text = "I'm the outer class!";
class NestedClass {
private final String text = "I'm the nested class!";
public final String getText() {
return this.text; // "I'm the outer class!"
}
public final String getOuterClassText() {
return OuterClass.this.text; // "I'm the nested class!"
}
}
}
现在,在您的例子中,您拥有的嵌套类实际上是一个匿名类,您是通过实例化MyEventListener
接口创建的,代码如下:
gamePanel.addMyEventListener(new MyEventListener() {
@Override
public void wordFound(/* any data you could use */) {
// Here, access your GamePanel's instance attributes and methods using GamePanel.this
}
});
让我向您解释一下上面的代码到底做了什么:
new MyEventListener() {
}
此符号将创建一个实现MyEventListener的匿名类。您可以通过在括号{}
之间放置任何需要的内容来修改该类定义。由于实现了某个接口,因此必须覆盖该接口中声明的每个抽象方法,如下所示:
new MyEventListener() {
@Override
public void wordFound(/* any data you could use */) {
}
}
我希望它很清楚,并且对您有所帮助:)通常是类名。这种语法在非静态的嵌套类中使用。它用于访问其外部类的实例。让我举个例子:
class OuterClass {
private final String text = "I'm the outer class!";
class NestedClass {
private final String text = "I'm the nested class!";
public final String getText() {
return this.text; // "I'm the outer class!"
}
public final String getOuterClassText() {
return OuterClass.this.text; // "I'm the nested class!"
}
}
}
现在,在您的例子中,您拥有的嵌套类实际上是一个匿名类,您是通过实例化MyEventListener
接口创建的,代码如下:
gamePanel.addMyEventListener(new MyEventListener() {
@Override
public void wordFound(/* any data you could use */) {
// Here, access your GamePanel's instance attributes and methods using GamePanel.this
}
});
让我向您解释一下上面的代码到底做了什么:
new MyEventListener() {
}
此符号将创建一个实现MyEventListener的匿名类。您可以通过在括号{}
之间放置任何需要的内容来修改该类定义。由于实现了某个接口,因此必须覆盖该接口中声明的每个抽象方法,如下所示:
new MyEventListener() {
@Override
public void wordFound(/* any data you could use */) {
}
}
我希望它很清楚,并且对您有所帮助:)Thanx,我明白,但我应该在哪里称呼gamepan.notifyWordFound???我是说我把它放在我的JFrame里的什么地方??我编辑了我的帖子以添加我的代码,使其变得简单。您需要在以下情况下通知已找到某个单词(即使用notifyWordFound
)。。。找到一个词:)基本上,我想你应该在test的地方使用它,如果(tWord.isFound()==true)
(我相信在你的文件GamePanel.java的第90行左右。就这样称呼它:GamePanel.this.notifyWordFound();
。顺便说一句,GamePan\notifyWordFound
应该是私有的,在if(tWord.isFound()==true)
中不需要==true
,除非您认为更清楚,如果tWord.isFound
是true
,那么它是true
,并且输入的条件已验证,则无需明确地将其与true
进行比较:)最后一件事,当您的问题得到解决并开始工作时,不要忘记接受答案,这样将来的用户就会知道已经找到了解决方案:)谢谢,它很有效。还有一件事,为什么我要把GamePanel.this.notifyWordFound()
而不是this.notifyWordFound()
(顺便说一句,它不起作用)放到Thanx,我明白了,但是我应该把gamepan.notifyWordFound放在哪里呢???我是说我把它放在我的JFrame里的什么地方??我编辑了我的文章,添加了代码,使之变得简单