我目前正在使用一個簡單的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關閉時再一次。 最後嘗試通過點擊圖表與圖表互動。
LINK1:http://imgur.com/Z3MNx5k – ErichNova
LINK2:HTTP://imgur.com/z9Dy6jK – ErichNova
LINK3:HTTP:// imgur。 com/UeJM5EK – ErichNova