简体   繁体   English

JFrame上什么都没有显示

[英]Nothing is getting displayed on JFrame

This what I am doing but is nothing is getting displayed on Jframe window. 我正在做的事情,但是什么都没有显示在Jframe窗口中。 I have not extended class JFrame to do my, is it necessary to do so for displaying objects on window. 我没有扩展类JFrame来执行我的操作,是否需要这样做才能在窗口上显示对象。

public class testGraphics {
    static JFrame workingFrame = null;

    public static void main(String args[])
    {
        JFrame workingManager = new JFrame("Hello");
        workingManager.setSize(500, 500);
        workingManager.setVisible(true);
        Graphics g = workingManager.getGraphics();
        JPanel jp = (JPanel) workingManager.getContentPane();
        workingManager.paintComponents(g);
        g.fillOval(0, 0, 30, 30);
        g.drawOval(0, 50, 30, 30);
        g.setColor(Color.CYAN);
    }
}

Do not ever call getGraphics() or explicitly call paintXxx() to do custom painting. 永远不要调用getGraphics()或显式调用paintXxx()做的风俗画。 The correct way to do custom painting is to override the paintComponent method of the panel to paint on. 进行自定义绘制的正确方法是重写要在其上绘制的面板的paintComponent方法。 The overriden method will be implicitly called for you. 该覆盖方法会被隐式调用 Then add that panel to the frame. 然后将该面板添加到框架中。 Also you should override the getPreferredSize() of the panel, so it has a preferred size, so you can just pack the frame 另外,您应该覆盖面板的getPreferredSize() ,使其具有首选的大小,因此您可以打包框架

class PaintPanel extends JPanel {
    @Override
    protected paintComponent(Grapchics g) {
        super.paintComponent(g);
        g.drawString(....);
    }
    @Override
    public Dimension getPreferredSize() {'
        return new Dimension(300, 300);
    }
}

Then add it to the frame (or if you want to set it as the content pane of the frame, do that instead) 然后将其添加到框架中(或者,如果要将其设置为框架的内容窗格,则改为执行此操作)

PaintPanel panel = new PaintPaint();
frame.add(panel);
...
frame.pack();

See more at Performing Custom Painting 执行自定义绘画中查看更多信息

I made several changes to your code to get it to work properly. 我对您的代码进行了几处更改,以使其正常工作。

  1. I changed your main method to call the SwingUtilities invokeLater method to make sure that the Swing components were defined and used on the Event Dispatch thread . 我将您的main方法更改为调用SwingUtilities invokeLater方法,以确保在事件分发线程上定义并使用了Swing组件。

  2. I created a drawing JPanel. 我创建了一个图形JPanel。 I set the color first, then drew the ovals. 我先设置颜色,然后画椭圆形。

  3. I added a JFrame default close operation. 我添加了一个JFrame默认关闭操作。 You must specify a default close operation, or else your Java application will continue running after you close the JFrame. 您必须指定默认的关闭操作,否则您的Java应用程序将在关闭JFrame之后继续运行。

  4. I moved the size to the drawing panel. 我将尺寸移动到绘图面板。 The frame size will be calculated when you call the JFrame pack method. 当您调用JFrame pack方法时,将计算帧大小。

And here's the modified code: 这是修改后的代码:

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGraphics implements Runnable{

    private JFrame workingManager;

    private JPanel drawingPanel;

    @Override
    public void run() {
        workingManager = new JFrame("Hello");
        workingManager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawingPanel = new DrawingPanel();

        workingManager.add(drawingPanel, BorderLayout.CENTER);
        workingManager.pack();
        workingManager.setLocationByPlatform(true);
        workingManager.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestGraphics());
    }

    public class DrawingPanel extends JPanel {

        private static final long   serialVersionUID    = 
                -3701718376300985046L;

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(500, 500));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.CYAN);
            g.fillOval(0, 0, 30, 30);
            g.drawOval(0, 50, 30, 30);
        }

    }

}

The setSize() and setVisible() must be at the bottom of the method: setSize()setVisible()必须在方法的底部:

 JFrame workingManager = new JFrame("Hello");
    Graphics g = workingManager.getGraphics();
    JPanel jp = (JPanel) workingManager.getContentPane();
    workingManager.paintComponents(g);
    g.fillOval(0, 0, 30, 30);
    g.drawOval(0, 50, 30, 30);
    g.setColor(Color.CYAN);

    workingManager.setSize(500, 500);
    workingManager.setVisible(true);

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

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