问题
These are the files. I have set JFrame to be visible, and have added JPanel to it, but still, the code only shows the window without anything in it.
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Collections;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
frame.setVisible(true);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.setVisible(true);
}
-------------DRAWINGPANEL FILE-------------------
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel {
public void painting(Graphics pen) {
pen.drawRect(50, 50, 20, 20);
pen.drawRect(100, 50, 40, 20);
pen.drawOval(200,50,20,20);
pen.drawOval(250, 50, 40, 20);
pen.drawString("Square", 50, 90);
pen.drawString("Rectangle", 100, 90);
pen.drawString("Cirlce", 200, 90);
pen.drawString("Oval", 250, 90);
pen.fillRect(50, 100, 20, 20);
pen.fillRect(100, 100, 40, 20);
pen.fillOval(250, 100, 20, 20);
pen.fillOval(250, 100, 40, 20);
pen.drawLine(50, 150, 300, 150);
pen.drawArc(50, 150, 200, 100, 0, 180);
pen.fillArc(100, 175, 200, 75, 90, 45);
}
}
回答1:
Here's what I get after making your code runnable, fixing your JFrame method calls and fixing your drawing JPanel.
Swing applications should always start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
You pack a JFrame. You set the preferred size of your drawing JPanel. This way, you know how big your drawing JPanel is, without worrying about the JFrame decorations.
Here's the complete runnable code.
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawingPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawingPanelExample());
}
@Override
public void run() {
JFrame frame = new JFrame("My Empty Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(350, 300));
}
@Override
protected void paintComponent(Graphics pen) {
super.paintComponent(pen);
pen.drawRect(50, 50, 20, 20);
pen.drawRect(100, 50, 40, 20);
pen.drawOval(200, 50, 20, 20);
pen.drawOval(250, 50, 40, 20);
pen.drawString("Square", 50, 90);
pen.drawString("Rectangle", 100, 90);
pen.drawString("Cirlce", 200, 90);
pen.drawString("Oval", 250, 90);
pen.fillRect(50, 100, 20, 20);
pen.fillRect(100, 100, 40, 20);
pen.fillOval(250, 100, 20, 20);
pen.fillOval(250, 100, 40, 20);
pen.drawLine(50, 150, 300, 150);
pen.drawArc(50, 150, 200, 100, 0, 180);
pen.fillArc(100, 175, 200, 75, 90, 45);
}
}
}
回答2:
Try changing the method in DrawingPanel from painting to paint, which will get called when run. paint is a method inherited from JPanel.
Edit: As mentioned by NomadMaker, use paintComponent() not paint() here. Read this for more information.
回答3:
You should override paintComponent like so:
...
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D pen = (Graphics2D) g;
...
}
Also some suggestions:
- You can extending
JComponentinstead ofJPanel(It should work both ways) - You can use
setSizeorsetPreferredSizefor yourpanelto fit it with your frame size. - You can only use
setVisisble(true);only once after all of the configurations of your frame. - And add it to the center of the frame like so:
...
JFrame frame = new JFrame();
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
DrawingPanel panel = new DrawingPanel();
panel.setPreferredSize(new Dimensions(350, 300));
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
...
On a side note:
Adding a layout manager may not be necessary and you can also replace setPreferredSize with setBounds like so:
panel.setBounds(0, 0, 350, 300);
frame.add(panel);
Where 0s are x and y coordinates respectively.
来源:https://stackoverflow.com/questions/65484190/java-on-eclipse-wont-show-the-jpanel-even-when-i-add-it-to-jframe