JPanel doesnt display the drawing
I have problem displaying drawing on JPanel. I created three class which linked to each other as the fo开发者_StackOverflow社区llowing. I was wondering why this code, doesn't display my drawing.
c.add(pDraw);
pDraw.add(draw);
1) MAIN
public class mainPage {
public static void main(String[]args){
JFrame appFrame = new Frame();
appFrame.setVisible(true);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2) JFRAME
public class Frame extends JFrame implements ActionListener{
private drawingBoard draw;
public Frame (){
draw = new drawingBoard(); //generate pattern
GridBagLayout m = new GridBagLayout();
Container c = (Container)getContentPane();
c.setLayout (m);
GridBagConstraints con;
.......
JPanel pDraw = new JPanel();
pDraw.setPreferredSize(new Dimension(500,500));
.....
c.add(pDraw);
pDraw.add(draw); // Call other class for drawing
.....
setResizable(false);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
3) JPANEL
public class drawingBoard extends JPanel {
.....
public void paint(Graphics g) {
......
}
}
Here is the answer, I forgot to set the size of the drawingBoard JPanel :-)
public class drawingBoard extends JPanel {
public drawingBoard(){
setPreferredSize(new Dimension (500,500));
}
}
Its a one line change to your code.
All you have to do is read the Swing tutorial on "Custom Painting" to figure out what you are doing wrong.
This is the third question in a row that could have been solved in minutes if you bothered to read the tutorial.
And you still haven't learned how to post a SSCCE, so I'm not about to spoon feed the answer to you.
I agree with all the points Ben Torell made in his post - plus some extra troubleshooting advice here.
Try this -
public class DrawingBoardTest extends JFrame {
public DrawingBoardTest() {
getContentPane().add(new drawingBoard(), BorderLayout.CENTER);
}
public static void main(String[] args ) {
JFrame f = new DrawingBoardTest();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
If the drawingPanel shows up, then it's a problem with your GridBagLayout, or having a preferred size set on drawingPanel that isn't large enough to display the drawing, or adding drawingBoard to pDraw (which, as far as I can see from the code, isn't really needed...).
The default layout for a JFrame's content pane is BorderLayout, which will give all space to the center component, which is where I'm placing drawingBoard in the code I posted.
The default layout of a JPanel is FlowLayout, which will only give the component it's preferred size. I see you set the preferred size on pDraw, but not on drawingBoard, in the original code - drawingBoard may have a preferred size set that is too small to display the drawing.
If the drawingPanel doesn't show up - then it's a problem in your drawingPanel paint() method.
While it's a little tough to debug the code without seeing the GridBagConstraints or the paint() method, I will offer to you that it's generally considered better to override the paintComponent() method rather than the paint() method. In drawingBoard, try this instead of overriding paint():
public void paintComponent(Graphics g) {
super.paintComponent(g); //optional
...
}
That may make a difference. For more information, check out this article from Java.
Also, as a freebie, you probably don't need to perform setVisible() and setDefaultCloseOperation() in both the main() method and Frame's constructor.
精彩评论