我打算在我的JPanel上画一个正方形,但是,它没有出现。我做错了什么?
class GUI extends JPanel {
private static Game game = new Game();
...
public GUI () {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setAttributes();
makeMenu();
}
});
}
...
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.drawRect(20, 20, 100, 100);
}
}
编辑:代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI extends JPanel {
private static Game game = new Game();
private static JPanel panel = new JPanel();
private static JFrame frame = new JFrame();
final private static int FRAME_HEIGHT = 500;
final private static int FRAME_WIDTH = 500;
//Board size 25x25px
final private static int PIXEL_SIZE = 20;
public GUI () {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setAttributes();
makeMenu();
}
});
}
public static void setAttributes() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("");
frame.setBackground(Color.black);
frame.setVisible(true);
}
private static void makeMenu() {
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.startGame();
}
});
panel.add(start);
frame.add(panel);
frame.pack();
}
public void setGameFrame() {
panel.removeAll();
frame.setTitle("Snake v0.1");
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.drawRect(20, 20, 100, 100);
}
public void paintGraphics() {
int[][] pixels = Game.getGraphics();
}
}
我看了你的代码。你在哪里将 GUI 添加到任何东西中?答:你没有,如果你不这样做,什么都不会被画出来。解决方案:将其添加到 gui 中,并阅读教程,因为您的代码中有很多需要修复的内容。
其他建议:
- 摆脱除常量之外的所有静态变量。
- 调用你的调用稍后在你的主方法中,而不是在 JPanel 的构造函数中
- 同样,将您的绘画 GUI 添加到您的实际 gui 中,以便将其添加到将显示它的内容中。
- 不要在任何组件上调用
getGraphics()
,因为这会让你得到一个非持久性的图形对象。
例如,
import javax.swing.*;
import java.awt.*;
class GUI extends JPanel {
private static final int PREF_W = 200;
private static final int PREF_H = PREF_W;
private static final int RECT_X = 20;
private static final int RECT_Y = RECT_X;
private static final int RECT_WIDTH = 100;
public GUI() {
setBackground(Color.darkGray);
}
// use @Override to be sure that your method is a true override
// Note that paintComponent should be protected, not public
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
// avoiding "magic" numbers here.
g.fillRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_WIDTH);
g.setColor(Color.red);
g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_WIDTH);
}
// so that the layout managers know how big I want this JPanel to be.
// this is a dumb method implementation. Yours will hopefully be smarter
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
GUI mainPanel = new GUI();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}