问题
I'm having this problem where an object that I've drawn isn't appearing in the GUI. I know it's being processed because data is being pushed to a log file. However, the graphic isn't appearing.
Here's some of my code:
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setLayout(new BorderLayout());
window.setVisible(true);
}
There's a button and a few other widgets that I've placed here and there. The center pane (BorderLayout.CENTER) is where my DrawnObject is to be displayed.
// Called when button is pushed/clicked
public static void trigger()
{
DrawnObject shape = new DrawnObject();
window.setLayout(new BorderLayout());
window.getContentPane().add(shape, BorderLayout.CENTER);
window.pack;
}
public class DrawnObject extends JComponent()
{
@Override
public Dimension getMinimumSize()
{
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(500, 500);
}
@Override
public Dimension getMaximumSize()
{
return new Dimension(700, 700);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
}
}
I've tried casting the Graphics object as Graphics2D and using the appropriate draw methods, but that hasn't helped.
回答1:
Try changing the color...
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
The graphics context color is set to the components background color by default
public class PaintTest01 {
public static void main(String[] args) {
new PaintTest01();
}
public PaintTest01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DrawPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(30, 30);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
}
}
}
UPDATED
From your updated code in your question, it can't compile...
You create a JFrame named window in the constructor, which is a local variable...
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setLayout(new BorderLayout());
window.setVisible(true);
}
Then you try and add the DrawObject to the window...
public static void trigger()
{
DrawnObject shape = new DrawnObject();
window.setLayout(new BorderLayout());
window.getContentPane().add(shape, BorderLayout.CENTER);
window.pack;
}
But because window is undefined, you example can't compile.
The only way that this would compile is if you had a static variable at the class level called window, which in that case, it should be producing a NullPointerException, unless you've initialised that variable
public class MyDrawing {
public static JFrame window = new JFrame();
This would mean you have two frames, one you created in the constructor and one your create as a static level class field. This won't work, because they are different instances
回答2:
have to return PreferredSize from public class DrawnObject extends JComponent(), otherwise returns
Dimension(0, 0);Top-Level containershave got implementedBorderLayout, thenwindow.add(shape, BorderLayout.CENTER);is proper code line andJComponentshould be layed correctlyuse
pack()instead ofinvalidate(), this code line doesn't works, to invoke something for container layed byBorderLayoutorGridLayout(e.i. ???), nor for container based onJComponent,JComponenthavent implemented anyLayoutManagerinAPI, have to returnPreferredSizefor better help sooner post an SSCCE
回答3:
Try adding your DrawnObject to the windows content pane, and also don't forget to set a layout. Using a null layout is bad practice (your layout is set to null if you call on invalidate).
window.getContentPane().setLayout(new BorderLayout());
window.getContentPane().add(shape, BorderLayout.CENTER);
window.pack();
window.setVisible(true);
Also, try to skip the invalidate().
来源:https://stackoverflow.com/questions/12931326/graphics-not-drawn-appearing