简体   繁体   English

Java在JPanel上看不到图形

[英]Java Cannot See Graphics on JPanel

I am using Java Eclipse with Swing Designer and I am trying to display some graphics on a JPanel. 我正在将Java Eclipse与Swing Designer一起使用,并且试图在JPanel上显示一些图形。 I am a pretty amateurish programmer and new to graphics coding. 我是一个非常业余的程序员,并且是图形编码的新手。

Basically, There is a JFrame with 2 JPanels( Stage and Setttings). 基本上,有一个带有2个JPanels(阶段和设置)的JFrame。 There is a method paintTest inside the class Surface. 类Surface内部有一个paintTest方法。 This method should draw the string 'This is a test' on the Stage JPanel. 此方法应在Stage JPanel上绘制字符串“ This is a test”。 When I execute the code there are no errors. 当我执行代码时,没有错误。 Any help would be appreciated. 任何帮助,将不胜感激。

Please see the below code. 请参见下面的代码。

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame; 
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;



public class TestSimulator extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JFrame frmTestSimulation;
    private JPanel stagePanel = new JPanel();
    private JLabel stageLabel = new JLabel("Stage");
    private JPanel settingsPanel = new JPanel();
    private JLabel settingsLabel = new JLabel("Settings");


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestSimulator window = new TestSimulator();
                    window.frmTestSimulation.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestSimulator() {
        initialize();
    }


    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmTestSimulation = new JFrame();
        frmTestSimulation.setTitle("WAPP Simulation");
        frmTestSimulation.setBounds(100, 100, 727, 500);
        frmTestSimulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmTestSimulation.getContentPane().setLayout(null);

        //JPanel stagePanel = new JPanel();
        stagePanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
        stagePanel.setBounds(262, 11, 439, 439);
        frmTestSimulation.getContentPane().add(stagePanel);
        stagePanel.setLayout(null);
        stagePanel.add(new Surface());  //Show the string 'This is a test'

        //JLabel stageLabel = new JLabel("Stage");
        stageLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        stageLabel.setBounds(183, 11, 53, 34);
        stagePanel.add(stageLabel);

        //JPanel settingsPanel = new JPanel();
        settingsPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
        settingsPanel.setBounds(10, 11, 242, 439);

        frmTestSimulation.getContentPane().add(settingsPanel);
        settingsPanel.setLayout(null);

        //JLabel settingsLabel = new JLabel("Settings");
        settingsLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        settingsLabel.setBounds(78, 11, 71, 22);
        settingsPanel.add(settingsLabel);

    }

    class Surface extends JPanel{

        public void paintTest(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawString("This is a test", 50, 50);

        }

        @Override
        public void paintComponent(Graphics g) {

            super.paintComponent(g);
            paintTest(g);
        }



    }
}

Your stagePanel is lacking a layout, so only one component inside of it will be printed. 您的stagePanel缺少布局,因此将仅打印其中的一个组件。 Change 更改

stagePanel.setLayout(null);

to

stagePanel.setLayout(new GridLayout(1, 2));

And it should display something like 它应该显示类似

结果窗口的图像

Refer to this website for more information about layouts in Swing and to this answer to learn more about displaying custom stuff in JPanels. 请参阅此网站 ,以获取有关Swing中布局的更多信息,以及此答案,以了解有关在JPanels中显示自定义内容的更多信息。

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

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