0

所以我的代码有这个问题。每当我加载游戏时,屏幕中央都会出现一个红色方块,我还没有对其进行编程。我试图找到错误几个小时,但我就是看不到它。我认为这与面板或其他东西有关。第二件事是当我按下按钮绘制网格时,只出现了一条小线。它被编程为比实际大得多,而且它也不在正确的位置。以下是我的所有代码,非常感谢任何帮助!

package com.theDevCorner;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class Game extends JPanel implements ActionListener {

   public static JButton grid = new JButton("Show Grid");

   public static JPanel drawArea = new JPanel();
   public static JMenuBar menu = new JMenuBar();
   public static JPanel notDrawn = new JPanel();

   public static boolean gridPressed = false;

   public Game() {

      grid.addActionListener(this);

   }

   public static void main(String args[]) {
      Game game = new Game();

      JFrame frame = new JFrame();
      frame.setVisible(true);
      frame.setSize(new Dimension(
            Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit
                  .getDefaultToolkit().getScreenSize().height));
      frame.setTitle("Game");
      frame.setAlwaysOnTop(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);

      menu.setSize(new Dimension(1600, 20));
      menu.setLocation(0, 0);

      notDrawn.setBackground(new Color(255, 0, 50));
      notDrawn.setSize(100, 900);
      notDrawn.add(grid);
      notDrawn.setLayout(null);

      grid.setSize(new Dimension(100, 25));
      grid.setLocation(0, 25);

      drawArea.setSize(new Dimension((Toolkit.getDefaultToolkit()
            .getScreenSize().width), Toolkit.getDefaultToolkit()
            .getScreenSize().height));
      drawArea.setLocation(100, 0);
      drawArea.setBackground(Color.black);
      drawArea.add(menu);
      drawArea.add(game);

      frame.add(drawArea);
      frame.add(notDrawn);

   }

   public void paint(Graphics g) {

      Game game = new Game();

      if (gridPressed) {

         Game.drawGrid(0, 0, g);

      }

      g.dispose();
      repaint();

   }

   public static void drawGrid(int x, int y, Graphics g) {

      g.setColor(Color.white);
      g.drawLine(x, y, 50, 300);

   }

   @Override
   public void actionPerformed(ActionEvent e) {

      if (e.getSource() == grid && gridPressed == true) {
         gridPressed = false;
         System.out.println("Unpressed");
      }

      if (e.getSource() == grid) {
         gridPressed = true;

         System.out.println("Pressed");
      }
   }
}
4

1 回答 1

1

有很多问题...

您看到的红色“正方形”实际上是您的grid按钮。它是红色的原因是因为你的paint方法。

Graphics是共享资源,即屏幕上绘制的每个组件共享相同的Graphics上下文。因为你选择dispose了上下文并且因为你没有尊重油漆链,所以你基本上把它搞砸了。

永远不要处理Graphics您没有创建的上下文。它将防止任何东西再次被涂在上面。总是打电话super.paintXxx。油漆链很复杂,做了很多非常重要的工作。如果您要忽略它,请准备好重新实现它。

null布局变化很少是正确的选择,尤其是在布局组件时。您需要将您的组件与自定义绘画分开,否则组件将出现在自定义绘画上方。

frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height))不是最大化窗口的方法。这没有考虑任务栏之类的可能性。而是使用Frame#setExtendedState

正如安德烈亚斯已经评论的那样,你actionPerformed的逻辑是错误的。您应该使用if-statement或简单地翻转boolean逻辑...

用简单的例子更新

ps-static这里不是你的朋友...

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Game extends JPanel implements ActionListener {

    private GridPane gridPane;

    public Game() {
        setLayout(new BorderLayout());

        SideBarPane sideBar = new SideBarPane();
        sideBar.addActionListener(this);
        add(sideBar, BorderLayout.WEST);

        gridPane = new GridPane();
        add(gridPane);
    }

    public static void main(String args[]) {
        Game game = new Game();

        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setTitle("Game");
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());
        frame.add(game);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("grid")) {
            gridPane.setGridOn(!gridPane.isGridOn());
        }
    }

    public class GridPane extends JPanel {

        private boolean gridOn = false;

        public GridPane() {
            setBackground(Color.BLACK);
        }

        public boolean isGridOn() {
            return gridOn;
        }

        public void setGridOn(boolean value) {
            if (value != gridOn) {
                this.gridOn = value;
                repaint();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (gridOn) {
                g.setColor(Color.white);
                g.drawLine(0, 0, 50, 300);
            }
        }
    }

    public class SideBarPane extends JPanel {

        public JButton grid;

        public SideBarPane() {
            setBackground(new Color(255, 0, 50));
            setLayout(new GridBagLayout());
            grid = new JButton("Show Grid");
            grid.setActionCommand("grid");
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weighty = 1;
            add(grid, gbc);
        }

        public void addActionListener(ActionListener listener) {
            grid.addActionListener(listener);
        }
    }
}
于 2013-05-10T06:45:52.260 回答