2

タイトルが示すように、配列から 100x100 セルの GridLayout にいくつかのラベルを作成しようとしているときに、多くの例外が発生します。完全に収まると信じていましたが、タイルを作成しようとすると (これは単にマップを表すパネル内の x、y およびタイプ変数を持つ JLabel の例外)、どこかで描画に問題があるようです。

これは適切なコードです (ペーストビンがルールに違反しているかどうかはわかりませんが、皆さんにそれを示すことで私の人生が楽になります)

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);
    }

}

新情報:

あの黒いブロックは何?

ここに画像の説明を入力

それが、マップがあるはずのパネルに表示されるものです。それが問題をより明確にするかどうかはわかりません。

読んでくれてありがとう!

4

1 に答える 1

0

クラスではメソッドをオーバーライドしTileないでください。これらは JComponent() によって定義されたメソッドです。getX()getY()

代わりに、setRow() および setColumn() と一緒に getRow() および getColumn() を使用してください。

于 2013-11-09T03:01:48.383 に答える