繁体   English   中英

单击时如何使图像替换JButton

[英]How to make an image replace a JButton when clicked

当前正在尝试制作TicTacToe游戏,我设置了边框和9个按钮(玩家可以为每个方块选择)。 单击时,向每个按钮添加功能时遇到麻烦。

package OX;

import javax.swing.*;
import java.awt.*;

public class OX{

public static void main(String[] args){
    JFrame frame = new JFrame("OX");
    frame.setSize(800,800);
    frame.setVisible(true);
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridBagLayout());
    frame.add(panel);
    GridBagConstraints c = new GridBagConstraints();
    JButton button1 = new JButton("1");
    button1.setPreferredSize(new Dimension(100,100));
    c.gridx = 1;
    c.gridy = 1;
    c.insets = new Insets(50,50,50,50);     
    panel.add(button1, c);
    JButton button2 = new JButton("2");
    button2.setPreferredSize(new Dimension(100,100));
    c.gridx = 2;
    c.gridy = 1;
    panel.add(button2, c);
    JButton button3 = new JButton("3");
    button3.setPreferredSize(new Dimension(100,100));
    c.gridx = 3;
    c.gridy = 1;
    panel.add(button3, c);
    JButton button4 = new JButton("4");
    button4.setPreferredSize(new Dimension(100,100));
    c.gridx = 1;
    c.gridy = 2;
    panel.add(button4, c);
    JButton button5 = new JButton("5");
    button5.setPreferredSize(new Dimension(100,100));
    c.gridx = 2;
    c.gridy = 2;
    panel.add(button5, c);
    JButton button6 = new JButton("6");
    button6.setPreferredSize(new Dimension(100,100));
    c.gridx = 3;
    c.gridy = 2;
    panel.add(button6, c);
    JButton button7 = new JButton("7");
    button7.setPreferredSize(new Dimension(100,100));
    c.gridx = 1;
    c.gridy = 3;
    panel.add(button7, c);
    JButton button8 = new JButton("8");
    button8.setPreferredSize(new Dimension(100,100));
    c.gridx = 2;
    c.gridy = 3;
    panel.add(button8, c);
    JButton button9 = new JButton("9");
    button9.setPreferredSize(new Dimension(100,100));
    c.gridx = 3;
    c.gridy = 3;
    panel.add(button9, c);      
  }
}

我尝试了很多不同的想法,但似乎做对了。

谢谢!

首先,您的代码不是模块化的。 我建议您使用适当的类对其进行模块化

Swing的重要接受是侦听器类。 编写按钮的功能; 您需要实现ActionListener并构建此类。所有游戏逻辑都将在此Listener类中实现。

有一个单独的方法可以从此侦听器验证游戏状态(赢/输)。 这应该使您的任务变得简单。

请参考-https: //docs.oracle.com/javase/8/docs/api/java/awt/event/ActionListener.html http://www.codejava.net/java-core/the-java-language/的java -8-λ-侦听器例如

希望这可以帮助。 干杯!

package OX;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class OX{

public static void main(String[] args){
    JFrame frame = new JFrame("OX");
    frame.setSize(800,800);
    frame.setVisible(true);
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridBagLayout());
    frame.add(panel);
    GridBagConstraints c = new GridBagConstraints();        
    JButton button1 = new JButton("1");
    button1.setPreferredSize(new Dimension(100,100));
    c.gridx = 1;
    c.gridy = 1;
    c.insets = new Insets(50,50,50,50);
    button1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test");
            }
        });
    panel.add(button1, c);      
    JButton button2 = new JButton("2");
    button2.setPreferredSize(new Dimension(100,100));
    c.gridx = 2;
    c.gridy = 1;
    button2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test2");
            }
        });
    panel.add(button2, c);      
    JButton button3 = new JButton("3");
    button3.setPreferredSize(new Dimension(100,100));
    c.gridx = 3;
    c.gridy = 1;
    button3.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test3");
            }
        });
    panel.add(button3, c);      
    JButton button4 = new JButton("4");
    button4.setPreferredSize(new Dimension(100,100));
    c.gridx = 1;
    c.gridy = 2;
    button4.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test4");
            }
        });
    panel.add(button4, c);      
    JButton button5 = new JButton("5");
    button5.setPreferredSize(new Dimension(100,100));
    c.gridx = 2;
    c.gridy = 2;
    button5.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test5");
            }
        });
    panel.add(button5, c);      
    JButton button6 = new JButton("6");
    button6.setPreferredSize(new Dimension(100,100));
    c.gridx = 3;
    c.gridy = 2;
    button6.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test6");
            }
        });
    panel.add(button6, c);      
    JButton button7 = new JButton("7");
    button7.setPreferredSize(new Dimension(100,100));
    c.gridx = 1;
    c.gridy = 3;
    button7.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test7");
            }
        });
    panel.add(button7, c);
    JButton button8 = new JButton("8");
    button8.setPreferredSize(new Dimension(100,100));
    c.gridx = 2;
    c.gridy = 3;
    panel.add(button8, c);
    button8.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test8");
            }
        });     
    JButton button9 = new JButton("9");
    button9.setPreferredSize(new Dimension(100,100));
    c.gridx = 3;
    c.gridy = 3;
    panel.add(button9, c);      
    button9.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("test9");
            }
        });

    }
}

@Vijayan Kani

我将ActionListener添加到每个按钮

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM