繁体   English   中英

当我尝试运行程序时,GUI无法加载,我无法弄清楚为什么

[英]When i try to run my program, the GUI won't load and I can't figure out why

当不使用数组或数组列表时,这部分代码可以正常工作。

import javax.swing.*;
public class GUI
{
    public static void main(String[] args) {
        JFrame frame = new JFrame("01");
        frame.getContentPane().add(new Panel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }
}

当不使用数组或数组列表时,这部分代码可以正常工作

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

public class Panel extends JPanel
{
    private Shapes shapes;

    public Panel () {
        setFocusable(true);
        requestFocusInWindow();
        setPreferredSize(new Dimension(500,500));
    }

    public void paintComponent(Graphics gc) {
        super.paintComponent(gc);
        shapes.draw(gc);
    }
}

在这个类中,如果我不使用数组或数组列表,它将运行良好,但不能使其与它们一起使用。

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

public class Shapes
{

    ArrayList <int[]> blocks = new ArrayList <int[]>();

    int[] arr;
    int w,x,y,z;

    public void draw(Graphics gc) {
        gc.setColor(Color.black);
        blocks();
        for(int i=0; i<blocks.size()-1; i++){
            w=blocks.get(i)[0];
            x=blocks.get(i)[1];
            y=blocks.get(i)[2];
            z=blocks.get(i)[3];
            gc.fillRect(w, x, y, z);
        }
    }

    public void blocks() {
        popBlocks(100,500,300,30);
        popBlocks(300,400,150,30);
        popBlocks(500,300,150,30);
        popBlocks(700,200,150,30);
        popBlocks(900,100,150,30);
    }

    private void popBlocks(int a, int b, int c, int d) {
        arr[0] = a;
        arr[1] = b;
        arr[2] = c;
        arr[3] = d;
        blocks.add(arr);
    }
}

Panel#paintComponent NullPointerException ,因为未初始化shapes

public class Panel extends JPanel {

    private Shapes shapes;

    public Panel() {
        shapes = new Shapes();

Shapes#popBlocks NullPointerException ,因为未初始化arr

public class Shapes
{
    //...    
    int[] arr = new int[4];
    //...

但是等等,那只能画一个形状吗?! 这一切都是用一些新值更新arr实例并将其添加到blocks List

private void popBlocks(int a, int b, int c, int d) {
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
    arr[3] = d;
    blocks.add(arr);
}

这意味着您有5个块,其值分别为900,100,150,30

而不是使用实例字段,您应该使arr成为方法级别的字段,例如...

public class Shapes
{
    //...    
    //int[] arr;
    //...

    private void popBlocks(int a, int b, int c, int d) {
        int[] arr = new int[4];
        arr[0] = a;
        arr[1] = b;
        arr[2] = c;
        arr[3] = d;
        blocks.add(arr);
    }
}

同样, draw方法中的for-loop是错误的,例如,它应该从0-size - 1而不是0-size - 2循环。

public void draw(Graphics gc) {
    //...
    for(int i=0; i < blocks.size(); i++){
        //...
for(int i=0; i<blocks.size()-1; i++){

for循环上的条件应该是错误的

for(int i=0; i<=blocks.size()-1; i++) . 
or
for(int i=0; i<blocks.size(); i++) 

您可能想尝试一下。

暂无
暂无

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

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