问题
I intended for this to paint a square on my JPanel, however, it does not show up. What am I doing wrong?
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);
}
}
Edit: the code
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();
}
}
回答1:
I looked at your code. Where do you ever add GUI to anything? Answer: you don't, and if you don't, nothing will be painted. Solution: add it to the gui, and read the tutorials as there is much to fix in your code.
Other suggestions:
- Get rid of all static variables except for the contants.
- Call your invokeLater in your main method, not in a JPanel's constructor
- Again, add your painting GUI to your actual gui so that it gets added to something that will display it.
- Don't call
getGraphics()on any component as that will get you a non-persisting Graphics object.
e.g.,
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();
}
});
}
}
来源:https://stackoverflow.com/questions/20693744/program-not-painting