【问题标题】:JFrame not displaying output?JFrame不显示输出?
【发布时间】:2012-10-05 01:38:39
【问题描述】:

我是 Java 新手,整个使用数组 + 使用类及其方法来填充和显示数组。我花了过去 2-3 个小时,研究这个并检查谷歌的各种答案,但似乎没有任何帮助,我仍然被困住。在使用数组时,我真的不知道如何使用 rectangle1 类中的方法。

主类:

public static void main(String[] args) {
    GameBoard frame = new GameBoard();
}

游戏板类

public class GameBoard extends JFrame {

    public GameBoard() {

        JFrame frame = new JFrame();
        frame.setBounds(0, 0, 195, 215);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Rectangle1 board[][] = new Rectangle1[3][3];
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                board[row][col] = new Rectangle1(0, 0, 195, 215);
                if ((row + col) % 2 == 0) {
                    Graphics g = null;    
                    board[row][col].paint(g);
                    g.setColor(Color.yellow);
                    g.fillRect(0, 0, 195, 215);
                } else {
                    Graphics h = null;
                    board[row][col].paint(h);
                    h.setColor(Color.red);
                    h.fillRect(0, 0, 195, 215);
                }
                frame.add(board[row][col]);
            }    
        }  

    }
}

Rectangle1 类。

public class Rectangle1 extends JComponent  {

    /** post:   getX() == x  and  getY() == y
     *          and  getWidth() == w  and getHeight() == h
     *          and  getBackground() == Color.black
     */
    public Rectangle1(int x, int y, int w, int h)  {
        super();
        setBounds(x, y, w, h);
        setBackground(Color.black);
    }

    /** post:   this method draws a filled Rectangle
     *          and  the upper left corner is (getX(), getY()) 
     *          and  the rectangle's dimensions are getWidth() and getHeight()
     *          and  the rectangle's color is getBackground()
     */
    public void paint(Graphics g)  {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth()-1, getHeight()-1);
        paintChildren(g);
   }

}

【问题讨论】:

    标签: java arrays class methods awt


    【解决方案1】:

    首先,您正在与布局管理器作斗争,在您的 Rectangle 类中调用类似 setBounds(x, y, w, h) 的内容将被父容器的布局管理器(本例中为框架)覆盖

    其次,你不负责画,这样做;

    board[row][col].paint(g);
    g.setColor(Color.yellow);
    g.fillRect(0, 0, 195, 215);
    

    ...错了。如果它没有以你的程序崩溃而告终,那么你很幸运。

    在我尝试找出解决方案时,您可能需要一些时间仔细阅读

    在我的咆哮中......

    这个; 公共无效油漆(图形g){ g.setColor(getBackground()); g.fillRect(0, 0, getWidth()-1, getHeight()-1); 油漆儿童(g); }

    不合适。你必须调用super.paint(),后台有很多事情要你覆盖这个方法而不调用它super

    事实上,您应该尽可能避免覆盖 paint 并改用用户 paintComponent

    另外,Swing 中的坐标是相对于组件的。也就是说,任何组件的左上角(在组件的上下文中)总是 0x0

    这是一个工作示例

    public class TestBoard {
    
        public static void main(String[] args) {
            new TestBoard();
        }
    
        public TestBoard() {
    
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
    
                    new GameBoard();
    
                }
            });
    
        }
    
        public class GameBoard extends JFrame {
    
            public GameBoard() {
    
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
    
                Rectangle1 board[][] = new Rectangle1[3][3];
                for (int row = 0; row < board.length; row++) {
    
                    gbc.gridx = 0;
    
                    for (int col = 0; col < board[row].length; col++) {
                        board[row][col] = new Rectangle1(195, 215);
                        if ((row + col) % 2 == 0) {
                            board[row][col].setBackground(Color.YELLOW);
                        } else {
                            board[row][col].setBackground(Color.RED);
                        }
                        frame.add(board[row][col], gbc);
                        gbc.gridx++;
                    }
    
                    gbc.gridy++;
    
                }
    
                frame.pack();
                frame.setVisible(true);
    
            }
        }
    
        public class Rectangle1 extends JPanel {
    
            public Rectangle1(int w, int h) {
                setPreferredSize(new Dimension(w, h));
            }
    
        }
    }
    

    由于您的需求性质,我不得不使用GridBagLayout,这不是最简单的布局管理器。

    JComponents本质上是透明的,需要填写,最好使用JPanel

    【讨论】:

      【解决方案2】:

      评论:

      1. GameBoard 扩展了JFrame,但随后您在GameBoard 构造函数中创建了一个JFrame 实例。你应该做一个或另一个,但不能同时做。

      2. Rectangle.paint() 应该是Rectangle.paintComponent()。现在您只需创建 Rectangle 对象并将它们添加到容器中,例如 JFrameJPanel。它们将被自动绘制。如果要扩展 JComponent,则不必显式调用 paint()paintComponent()

      【讨论】:

      • 对于第 1 点。只需使用框架的实例,除非添加新行为(非常罕见)。
      • @AndrewThompson 我倾向于扩展 JFrame 并在构造函数中添加组件。当然,这通常只是一个自定义 JPanel,然后在 its 构造函数中添加组件。
      【解决方案3】:

      您正在使用null 图形对象来绘制您的窗口。而不是使用

      Graphics g = null; Graphics h = null;
      

      使用

      Graphics g = getGraphics(); Graphics h = getGraphics();
      

      这很可能会给你一个NullPointerException,不是吗?

      此外,由于您的矩形继承自 JComponent,因此您不应自己调用 paint() 方法。而是在将它们全部添加到框架后调用repaint()

      【讨论】:

      • 我之前就知道了。我进行了这些更改,但仍然得到: tictactoe.Rectangle1.paint(Rectangle1.java:28) at tictactoe.GameBoard.(GameBoard.java:32) 在 tictactoe 的线程“main”java.lang.NullPointerException 异常。 TicTacToe.main(TicTacToe.java:22)
      • 不调用paint()方法,而是调用repaint()
      【解决方案4】:

      这是你的问题:

      Graphics g = null;
      board[row][col].paint(g);
      

      您使用 null Graphics 调用paint(),导致NullPointerException

      public void paint(Graphics g)  {
              g.setColor( getBackground() );
              g.fillRect(0, 0, getWidth()-1, getHeight()-1);
              paintChildren(g);
      }
      

      【讨论】:

      • 为了澄清,这是几个问题之一。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2017-04-25
      相关资源
      最近更新 更多