gpt4 book ai didi

java - JPanel 可能重叠?

转载 作者:行者123 更新时间:2023-12-01 14:36:07 25 4
gpt4 key购买 nike

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

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

最佳答案

有很多问题...

您看到的红色“方 block ”实际上是您的 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这里不是你的 friend ...

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

关于java - JPanel 可能重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com