繁体   English   中英

在Java中用数组创建多个标签时出现异常(Swing)

[英]Getting an exception as I create several labels off an array in Java (Swing)

就像标题所说的那样,当我尝试在一个数组中为100x100单元的GridLayout创建多个标签时,我遇到了很多异常,我相信它应该很合适,但是当我尝试创建Tiles时( JLabel的一个例外,它在表示地图的面板内有x,y和类型变量),似乎有些东西在将它们绘制到某处时遇到了麻烦,试图使用debug却没有获得任何有关其发生原因的有用信息。

这是相关的代码(不知道pastebin是否违反规则,但这会让我的生活更轻松地向大家展示)

GUI类:

package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUIJuego extends JFrame{

    private JButton botonConstruirEscuela = new JButton("Escuela");
    private JButton botonConstruirComisaria = new JButton("Comisaria");
    private JButton botonConstruirCuartel = new JButton("Cuartel de Bomberos");

    private JButton botonConstruirArbol = new JButton("Arbol");

    private JButton botonConstruirCasa = new JButton("Casa");
    private JButton botonConstruirComercio = new JButton("Comercio");
    private JButton botonConstruirIndustria = new JButton("Industria");

    private Tile[][] mapaTiles = new Tile[100][100];

    private JLabel arcaLabel = new JLabel("Arca");
    private JLabel puntosBellezaLabel = new JLabel("Puntos de Belleza");
    private JLabel habitantesLabel = new JLabel("Habitantes");

    public GUIJuego(){

        JPanel panelConstruccion = new JPanel(new GridLayout(7,1));
        JPanel panelDatosCiudad = new JPanel(new GridLayout(1,3));
        JPanel panelMapa = new JPanel(new GridLayout(100,100));

        add(panelConstruccion, BorderLayout.WEST);
        panelConstruccion.add(botonConstruirEscuela);
        panelConstruccion.add(botonConstruirComisaria);
        panelConstruccion.add(botonConstruirCuartel);
        panelConstruccion.add(botonConstruirArbol);
        panelConstruccion.add(botonConstruirCasa);
        panelConstruccion.add(botonConstruirComercio);
        panelConstruccion.add(botonConstruirIndustria);

        add(panelDatosCiudad, BorderLayout.NORTH);
        panelDatosCiudad.add(arcaLabel);
        panelDatosCiudad.add(puntosBellezaLabel);
        panelDatosCiudad.add(habitantesLabel);

        add(panelMapa, BorderLayout.CENTER);
        cargarTiles(panelMapa);

    }

    private void cargarTiles(JPanel panel){

        for(int i = 0; i < 100; i++){
            for(int j = 0; j < 100; j++){
                mapaTiles[i][j] = new Tile(i, j, 0);
                asignarTile(mapaTiles[i][j], panel);
            }
        }

    }

    private void asignarTile(Tile tile, JPanel panel){

        if(tile.getTipo() == 0){
            tile.setText("Pasto");
            panel.add(tile);
        }
    }
}

这是一个隔离的GUIJuego类,显示了我认为引起问题的内容:

public class GUIJuego extends JFrame{

    private Tile[][] mapaTiles = new Tile[100][100];

    public GUIJuego(){

        JPanel panelMapa = new JPanel(new GridLayout(100,100));
        add(panelMapa, BorderLayout.CENTER);
        cargarTiles(panelMapa);
    }

    private void cargarTiles(JPanel panel){

        for(int i = 0; i < 100; i++){
            for(int j = 0; j < 100; j++){
                mapaTiles[i][j] = new Tile(i, j, 0);
                asignarTile(mapaTiles[i][j], panel);
            }
        }

    }

    private void asignarTile(Tile tile, JPanel panel){

        if(tile.getTipo() == 0){
            tile.setText("Pasto");
            panel.add(tile);
        }
    }

}

我认为问题出在GUI类之内,我的应用程序从第50行开始消亡。这是给我的错误的转储:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
        at java.util.TimSort.mergeLo(Unknown Source)
        at java.util.TimSort.mergeAt(Unknown Source)
        at java.util.TimSort.mergeCollapse(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.Arrays.sort(Unknown Source)
        at java.util.Collections.sort(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
        at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
        at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.SequencedEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
        at java.util.TimSort.mergeLo(Unknown Source)
        at java.util.TimSort.mergeAt(Unknown Source)
        at java.util.TimSort.mergeCollapse(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.Arrays.sort(Unknown Source)
        at java.util.Collections.sort(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
        at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
        at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
        at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.SequencedEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

编辑:平铺类

import javax.swing.JLabel;

public class Tile extends JLabel{

    private int x, y, tipo;

    public int getX() {
            return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getTipo(){
        return tipo;
    }

    public void setTipo(int tipo){
        if(tipo >= 0 || tipo <= 6)
            this.tipo = tipo;
    }

    public Tile(int x, int y, int tipo) {
        this.setX(x);
        this.setY(y);
            this.setTipo(tipo);
    }

}

新的信息:

那黑块是什么?

在此处输入图片说明

这就是我应该在地图应该位于的面板中看到的内容,我不知道这是否使我的问题更加清楚。

谢谢阅读!

Tile类中,不要重写getX()getY()方法。 这些是由JComponent()定义的方法。

相反,可以将getRow()和getColumn()以及setRow()和setColumn()一起使用。

暂无
暂无

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

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