java也不会显示它

k4aesqcs  于 2021-06-29  发布在  Java
关注(0)|答案(4)|浏览(351)

这些是文件。我已经将jframe设置为可见,并向其添加了jpanel,但是代码仍然只显示窗口,没有任何内容。

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

x7rlezfr1#


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

kr98yfug2#

---drawingpanel文件

mpgws1up

mpgws1up3#

尝试更改中的方法 DrawingPanelpaintingpaint ,运行时将调用它。 paint 是从继承的方法 JPanel .
编辑:正如nomadmaker提到的,使用 paintComponent() 不是 paint() 在这里。请阅读此文以了解更多信息。

zpf6vheq

zpf6vheq4#

下面是我在使你的代码可以运行,修复你的 JFrame 方法调用并修复图形 JPanel .

swing应用程序应该总是从调用
SwingUtilities invokeLater 方法。此方法确保在事件调度线程上创建和执行swing组件。
你带了一个 JFrame . 您可以设置图形的首选大小 JPanel . 这样,你就知道你的画有多大了 JPanel 是的,不用担心 JFrame 装饰。
下面是完整的可运行代码。

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

}

相关问题