JAVA图形不显示!

JAVA图形不显示!

我之前发过一篇帖子,但不太清楚!这是代码。这是我正在测试的示例代码,看看它是否适用于我的 ubuntu!

// using drawLine to connect the corners of s panel

import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{// draws an x from the corners of the panel
    public void paintComponent( Graphics g )
    {
    // calls paintComponent to ensure the panel displys correctly
    super.paintComponent( g );
    int width = getWidth();
    int height = getHeight();
    // draw line from the upper-left to lower-right corners
    g.drawLine( 0, 0, width, height );
    // draw line from then upper-right to the lower-left corners
    g.drawLine( width, 0, 0, height);
    System.out.println( "paintComponent Called");
    } // end method paintComponent
} // end class DrawPanel

And the driver code:

// application to display a DrawPanel
import javax.swing.JFrame;

public class DrawPanelTest
{
    public static void main(String[] args)
    {
    //create a panel that contains our drawing
    DrawPanel panel = new DrawPanel();
    // create a new frame to hold the panel
    JFrame application = new JFrame("Drawing Diagonals");
    // set the frame to exit when closed
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add(panel); // adds panel to the frame
    application.setSize(300, 300); // set the size 
    }
}

当我编译并运行它时,什么都没有显示!它应该弹出一个窗口并显示图形!

它在其他系统上运行良好,但在我的 ubuntu 上却不行!可能出了什么问题?

谢谢

答案1

添加application.setVisible(true);你的班级DrawPanelTest

public class DrawPanelTest {
    public static void main(String[] args) {
        // create a panel that contains our drawing
        DrawPanel panel = new DrawPanel();
        // create a new frame to hold the panel
        JFrame application = new JFrame("Drawing Diagonals");
        // set the frame to exit when closed
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel); // adds panel to the frame
        application.setSize(300, 300); // set the size
        application.setVisible(true);
    }
}

在此处输入图片描述

相关内容