程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame?

开发过程中遇到为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame的解决方法建议,希望对你解决为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame有所启发或帮助;

所以我正在尝试用 JAVA 创建一个国际象棋游戏(自学)。
我使用 JFrame 创建框架,使用 JPanel 将其添加到框架中,使用 JLabel 定义棋盘的图块。
我想通过单击它来更改图块的颜色,我正在使用这样的东西: 这是鼠标点击的方法。

@OverrIDe
public voID mouseClicked(MouseEvent e) {
    this.x = e.getX();
    this.y = e.getY();
    System.out.println("X: " + this.x + "; Y:" + this.y);
    GUI GUIOBject = new GUI();
    GUIOBject.clickOnTile(this.x,this.y);
}

这里调用的函数GUIObject.clickOnTile(int x,int y);是这样的:

public voID clickOnTile(int x,int y){
    this.labels[x/60 + y/60*8].setBackground(color.blue);
}

问题是这样做时每次点击某处都会出现一个新窗口。

为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame

因此,当我单击一个图块时,它会为正确的图块着色,但会打开一个新图块。 知道为什么会这样吗,我如何才能仅在该框架中进行更改?

这是完整的源代码:
主程序

public static voID main(String[] args){

    GUI graphic_user_interface = new GUI();

}

GUI.java:
public JFrame frame;
public JPanel panel;
public JLabel[] labels;
public String[] @R_404_4612@s = {"a","b","c","d","e","f","g","h"};

public GUI(){
    this.createFrame();
    this.createPanel();
    this.createLabels();
    this.addPanel();
    this.addLabels();
    this.setFrameVisible();
}

public voID clickOnTile(int x,int y){
    this.labels[x/60 + y/60*8].setBackground(color.blue);
}


@OverrIDe
public voID createFrame() {
    this.frame = new JFrame();
    this.frame.setSize(500,500);
    frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
}

@OverrIDe
public voID createPanel() {
    this.panel = new JPanel();
    this.panel.setLayout(new GrIDLayout(8,8));
    JPanel panel = new JPanel();
}

private voID createLabels(){
    this.labels = new JLabel[64];
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j <8; j++) {
            this.labels[i*8 + j] = new JLabel(this.@R_404_4612@s[j] + (i+1));
            this.labels[i*8 + j].setBounds(i*10,i*10,50,50);
            this.labels[i*8 + j].setborder(borderFactory.createEtchedborder(1));
            this.labels[i*8 + j].setopaque(true);

            if(i%2 == 0){
                if(j%2 == 0){
                    this.labels[i*8 + j].setBackground(color.black);
                }else{
                    this.labels[i*8 + j].setBackground(color.white);
                }
            }else{
                if(j%2 == 1){
                    this.labels[i*8 + j].setBackground(color.black);
                }else{
                    this.labels[i*8 + j].setBackground(color.white);
                }
            }

        }

    }
}

private voID addPanel(){
    this.frame.add(this.panel);
}

private voID addLabels(){
    for (int i = 0; i < 64; i++) {
        this.panel.add(this.labels[i]);
    }
}

private voID setFrameVisible(){
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    this.frame.setLocation(dim.wIDth/2-this.frame.getSize().wIDth/2,dim.height/2-this.frame.getSize().height/2);
    this.frame.getContentPane().addMouseListener(new Mouse());
    this.frame.setVisible(true);
}

鼠标.java:
公共静态 int x; public static int y;

@OverrIDe
public voID mouseClicked(MouseEvent e) {
    this.x = e.getX();
    this.y = e.getY();
    System.out.println("X: " + this.x + "; Y:" + this.y);
    GUI GUIOBject = new GUI();
    GUIOBject.clickOnTile(this.x,this.y);
}

@OverrIDe
public voID mousepressed(MouseEvent e) {
}

@OverrIDe
public voID mouseReleased(MouseEvent e) {
}

@OverrIDe
public voID mouseEntered(MouseEvent e) {
}

@OverrIDe
public voID mouseExited(MouseEvent e) {
}

IGUI(界面):

public voID createFrame();
public voID createPanel();

解决方法

发现问题:

在 Mouse 类中,我创建了一个新对象,然后调用该函数为 JLabel 着色。
我将标签变量设为静态,然后在 Mouse 类中更改它,效果很好。

大佬总结

以上是大佬教程为你收集整理的为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame全部内容,希望文章能够帮你解决为什么更改 JLabel 的 bg 颜色会打开另一个 JFrame所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: