簡體   English   中英

調用繪制后,JPanel不顯示在JFrame中

[英]JPanel not displaying in JFrame after paint is called

我目前正在使用一個簡單的GUI界面與Lego Mindstorm NXT進行交互。 我當前的問題與界面上的繪畫問題有關。 當我的MainUI加載時,它將調用一個名為GirdPanel()的方法來設置我的GridPanel。 MainUI擴展了JFrame,然后將該面板添加到JFrame調用中。 這是與此問題相關的MainUI的完整代碼。

  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並控制該程序的圖形顯示。

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的評論

有人在我的JFrame或JPanel構造中看到任何可見的錯誤嗎? 您有什么建議可以幫助我弄清楚發生了什么嗎?

旁注:第一次加載框架時,將調用Paint功能3次。 JComboBox打開時再一次。 JComboBox關閉時再一次。 最后,當試圖通過單擊不起作用時,會出現更多次。

為什么使用此行Graphics pg = panel.getGraphics(); 並使用pg對象在面板上繪制點? 為什么不創建另一個擴展JPanel並覆蓋其paintComponent方法以繪制所有必需點的類,然后將該覆蓋的Jpanel類的對象添加到WGraph面板?

例如,考慮下面的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM