简体   繁体   English

Eclipse 上的 Java 不会显示 JPanel,即使我将它添加到 JFrame

[英]Java on Eclipse won't show the JPanel even when I add it to JFrame

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.我已将 JFrame 设置为可见,并在其中添加了 JPanel,但代码仅显示 window 中没有任何内容。

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);
     }
 }

Try changing the method in DrawingPanel from painting to paint , which will get called when run.尝试将DrawingPanel中的方法从painting更改为paint ,它将在运行时被调用。 paint is a method inherited from JPanel . paint是从JPanel继承的方法。

Edit: As mentioned by NomadMaker , use paintComponent() not paint() here.编辑:正如NomadMaker所提到的,在这里使用paintComponent()而不是paint() Read this for more information.阅读内容以获取更多信息。

Here's what I get after making your code runnable, fixing your JFrame method calls and fixing your drawing JPanel .这是我在使您的代码可运行、修复您的JFrame方法调用并修复您的绘图JPanel之后得到的结果。

图像图形用户界面

Swing applications should always start with a call to the SwingUtilities invokeLater method. Swing 应用程序应始终从调用SwingUtilities invokeLater方法开始。 This method ensures that the Swing components are created and executed on the Event Dispatch Thread .此方法确保在Event Dispatch Thread上创建和执行 Swing 组件。

You pack a JFrame .你打包一个JFrame You set the preferred size of your drawing JPanel .您设置绘图JPanel的首选大小。 This way, you know how big your drawing JPanel is, without worrying about the JFrame decorations.这样,您就知道您的绘图JPanel有多大,而不必担心JFrame装饰。

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);
        }
    }

}

You should override paintComponent like so:您应该像这样覆盖paintComponent:

...

@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D pen = (Graphics2D) g;
...
}

Also some suggestions:还有一些建议:

  • You can extending JComponent instead of JPanel (It should work both ways)您可以扩展JComponent而不是JPanel (它应该双向工作)
  • You can use setSize or setPreferredSize for your panel to fit it with your frame size.您可以为panel使用setSizesetPreferredSize以使其适合您的框架尺寸。
  • You can only use setVisisble(true);你只能使用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:可能不需要添加布局管理器,您也可以将setPreferredSize替换为setBounds ,如下所示:

panel.setBounds(0, 0, 350, 300);

frame.add(panel);

Where 0s are x and y coordinates respectively.其中 0 分别是 x 和 y 坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM