gpt4 book ai didi

Java 绘画不工作

转载 作者:行者123 更新时间:2023-12-01 11:23:00 25 4
gpt4 key购买 nike

所以我有这个代码:

package tictactoe;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TicTacToe extends JFrame {

public static void main(String[] args) {
JFrame masterFrame = new JFrame("TicTacToe");
JPanel drawingPanel = new JPanel();
GameBoard theGame = new GameBoard();

masterFrame.add(drawingPanel);
masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
masterFrame.setSize(504, 504);
masterFrame.setResizable(false);
masterFrame.setLocationRelativeTo(null);
masterFrame.setVisible(true);
} //End of Main method


@Override
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//Drawing the GridLines
g2.drawLine(168, 0, 168, 500);
g2.drawLine(336, 0, 336, 500);
g2.drawLine(0, 168, 500, 168);
g2.drawLine(0, 336, 500, 336);
} //End of Paint Method
} //End of TicTacToe Class

我希望它以 TicTacToe 的方式在我的 JFrame 上画 4 条线,但 JFrame 仍然是空白的。为什么是这样?我的代码有什么问题,我应该如何修复它?

最佳答案

您是否确实创建了 TicTacToe 的实例,而不是 JFrame ,你的DrawingPane可能会通过它的 paint 绘画/覆盖您的框架正在绘画的内容方法。

// Create an instance of TicTacToe instead of JFrame...
//JFrame masterFrame = new JFrame("TicTacToe");
TicTacToe masterFrame = new TicTacToe();

可以在不通知或要求其父容器进行绘制的情况下绘制子组件,这意味着子组件实际上会覆盖框架先前绘制的内容,这可能会产生一些非常奇怪的结果,因为不同的部分会发生变化已更新

一个JFrame包含许多子组件,包括 JRootPanecontentPane ,您可以在其上放置自己的组件。

RootPane

针对您的情况,更好的解决方案可能是使用您的 DrawingPane并自定义它的 paintComponent渲染网格

参见Painting in AWT and SwingPerforming Custom Painting了解更多详情

例如...

TicTacToe

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TicTacToe {

public static void main(String[] args) {
new TicTacToe();
} //End of Main method

public TicTacToe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Tic Tac Toe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TicTacToePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TicTacToePane extends JPanel {

public TicTacToePane() {
}

@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
//Drawing the GridLines
g2d.drawLine(168, 0, 168, 500);
g2d.drawLine(336, 0, 336, 500);
g2d.drawLine(0, 168, 500, 168);
g2d.drawLine(0, 336, 500, 336);
g2d.dispose();
}

}

} //End of TicTacToe Class

作为一般规则,建议不要直接从 JFrame 扩展,除了您遇到的自定义绘制问题之外,您没有向类添加任何功能,并且您将自己锁定在单个用例中(很难再次重用该类)

关于Java 绘画不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31039667/

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