2013-01-24 90 views
0

我目前正在使用一個簡單的GUI界面來與Lego Mindstorm NXT進行交互。 我目前的問題是在我的界面上出現塗料問題。 當我的MainUI加載它時,調用一個名爲GirdPanel()的方法來設置我的GridPanel。 MainUI擴展了JFrame,然後將此面板添加到它的JFrame調用中。 以下是MainUI的完整代碼,對於此問題很重要。JPanel在調用paint之後不會顯示在JFrame中

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函數被調用三次。當JComboBox打開時再一次。當JComboBox關閉時再一次。 最後嘗試通過點擊圖表與圖表互動。

+0

LINK1:http://imgur.com/Z3MNx5k – ErichNova

+0

LINK2:HTTP://imgur.com/z9Dy6jK – ErichNova

+0

LINK3:HTTP:// imgur。 com/UeJM5EK – ErichNova

回答

2

爲什麼使用這條線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); 
     } 
    } 
} 
+0

Graphics pg = panel.getGraphics() ;恰恰是我的問題撒謊的地方。我不需要這個,應該只使用超級通話中的p。 – ErichNova