JPanel可能重叠?

时间:2013-05-10 06:30:33

标签: java swing jpanel overlap

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

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

1 个答案:

答案 0 :(得分: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不是你的朋友......

enter image description here

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