调用画图后,JPanel不显示在JFrame中 [英] JPanel not displaying in JFrame after paint is called

查看:161
本文介绍了调用画图后,JPanel不显示在JFrame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用一个简单的GUI界面来与Lego Mindstorm NXT进行交互. 我当前的问题与界面上的绘画问题有关. 当我的MainUI加载时,它会调用一个名为GirdPanel()的方法来设置我的GridPanel. MainUI扩展了JFrame,然后将该面板添加到JFrame调用中. 这是与此问题相关的MainUI的完整代码.

I am currently imlementing a simple GUI interface to interact with a Lego Mindstorm NXT. My current issue resides with a paint issue on my interface. When my MainUI loads it calls a method called GirdPanel() which sets up my GridPanel. The MainUI, which extends JFrame, then adds this panel to it's JFrame calling. Here is the full code for MainUI that matters for this problem.

  public MainUI(){
    setSize(700, 600);

    PathPanel pathPanel = new PathPanel(controller);
    add(pathPanel, BorderLayout.WEST);
    CurrentPath.getInstance().addPathDataListener(pathPanel);
    CurrentPath.getInstance().addPointSelectionListener(pathPanel);

    gridPanel();
    add(gridPanel, BorderLayout.CENTER);


    robotControlBar();
    add(robotControls, BorderLayout.NORTH);

    setJMenuBar(menuPanel());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public void gridPanel(){
    gridPanel = new JPanel(new BorderLayout());
    WGraph graph = new WGraph();

    gridPanel.add(graph, BorderLayout.CENTER);

}

WGraph是我的类,它扩展了JPanel并控制该程序的图形显示.

The WGraph is my class that extends JPanel and controls the graphing display for this program.

public class WGraph extends JPanel{

public WGraph(){
    //WGraph Panel property setup
    setLayout(new BorderLayout());
    //Variable creation
    points = new ArrayList<Dot>();
    //Label to display coordinates of selected point
    pointDisplay = new JLabel("Selected point at: None Chosen");

    //testPoints(); //Comment Out when test dots not needed.

    //Create Graph Panel
    panel = new JPanel();
    panel.setBackground(PANEL_COLOR);

    //Mouse Listeners for Panel
    MouseEventHandler mouseListener = new MouseEventHandler();
    panel.addMouseListener(mouseListener);
    panel.addMouseMotionListener(mouseListener);


    //Adding components to the WGraph panel
    add(pointDisplay, BorderLayout.NORTH);
    add(panel, BorderLayout.CENTER);

    repaint();
}
public void paintComponent(Graphics g){
    // invokes default painting for JFrame; must have this!
    super.paintComponent(g); 

    // paint on the canvas rather than the JFrame
    Graphics pg = panel.getGraphics(); 
    System.out.println("*"); //Print out to see when repaint has been called. for testing only
    int width = panel.getWidth();
    int height = panel.getHeight();
    pg.setColor(GRID_COLOR);

    for (int i = 50; i < width; i+=50) {
        pg.drawLine(i, 0, i, height);
    }

    for (int i = 50; i < width; i+=50) {
        pg.drawLine(0, i, width, i);
    }

    Dot previousPoint = null;

    for (int i = 0; i < points.size(); i++) {
        Dot currentPoint = points.get(i);
        currentPoint.draw(pg);

        if (previousPoint != null) {
            pg.setColor(Dot.DESELECTED_COLOR);
            pg.drawLine(new Float(previousPoint.getCenter().x).intValue(), 
                    new Float(previousPoint.getCenter().y).intValue(), 
                    new Float(currentPoint.getCenter().x).intValue(), 
                    new Float(currentPoint.getCenter().y).intValue());
        }

        previousPoint = currentPoint;
    }
}

所以,毕竟我可以描述我的问题. 问题在于图形面板也不会在预期时显示. 我正在尝试确定原因. 当前,当程序加载时,它看起来像这样. LINK1 很简单,它根本不显示图,但是当我下拉JComboBox时,它就会出现. LINK2 当JComboBox选中一个项目并关闭时,它也会重新显示. LINK3 但是,当您尝试与之交互时,它会再次消失. LINK4在评论中

So after all that I can describe my problem. The problem is with the graphing panel it will not show up when expected too. I am trying to determine why. Currently when the program loads it appears like this. LINK1 It simple doesn't show the graph at all but when I drop down the JComboBox it appears. LINK2 It also reamins when the JComboBox has an item selected and closes. LINK3 However it disappears again when you try to interact with it. LINK4 in comments

有人在我的JFrame或JPanel构造中看到任何可见的错误吗? 您有什么建议可以帮助我弄清楚发生了什么事吗?

Does anybody see any visible error in my JFrame or JPanel construction? Do you have any suggestions to help me figure out what is going on?

旁注: 当框架第一次加载时,Paint函数会被调用3次. JComboBox打开时再次显示. JComboBox关闭时再次显示. 最后,当试图通过单击不起作用时,会出现更多次.

Side note: The Paint function is called three times when the frame first loads. Once more when JComboBox opens. Once more when JComboBox closes. And finally more times when trying to ineract with the graph by clicking on it.

推荐答案

为什么要使用这条线Graphics pg = panel.getGraphics();并使用pg对象在面板上绘制点?为什么不创建另一个扩展JPanel并覆盖其paintComponent方法以绘制所有所需点的类,然后将该覆盖的Jpanel类的对象添加到WGraph面板上呢?

Why are you using this line Graphics pg = panel.getGraphics(); and using the pg object for drawing points on panel? Why not create another class that extends JPanel and override its paintComponent method to draw all the required points , and then add the object of that overriding Jpanel class to WGraph panel ?.

例如,考虑下面的代码:

For example consider the code give below:

import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class MyFrame extends JFrame implements ActionListener
{
    private JComboBox jcbShape;
    private WGraph jpGraph;
    public MyFrame()
    {
        super("GridFrame");
    }
    public void prepareGUI()
    {
        Object[] items= {"Line","Rectangle","Circle"};
        jcbShape = new JComboBox(items);
        jpGraph = new WGraph();
        Container container = getContentPane();
        container.add(jpGraph);
        container.add(jcbShape,BorderLayout.NORTH);
        jcbShape.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,400);
    }
    @Override
    public void actionPerformed(ActionEvent evt)
    {
        String sShape = (String)jcbShape.getSelectedItem();
        jpGraph.setShape(sShape);
    }
    public static void main(String[] st)
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                MyFrame myFrame = new MyFrame();
                myFrame.prepareGUI();
                myFrame.setVisible(true);
            }
        });
    }
}
class WGraph extends JPanel
{
    private String sShape = "Line";
    public void setShape(String shape)
    {
        sShape = shape;
        repaint();
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if ("Line".equalsIgnoreCase(sShape))
        {
            g.drawLine(10, 20, 100, 200);
        }
        else if ("Circle".equalsIgnoreCase(sShape))
        {
            g.drawOval(50, 100 , 200, 200);
        }
        else if ("Rectangle".equalsIgnoreCase(sShape))
        {
            g.drawRect(10, 20, 150, 200);
        }
    }
}

这篇关于调用画图后,JPanel不显示在JFrame中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆