2016-04-13 161 views
-1

我試圖顯示一個圖表,一旦文件被選中使用重繪方法,它不是提供給我任何錯誤,但有一些我做錯了,它不顯示圖,我是新的塗料方法,所以我認爲這就是爲什麼它不工作。JPanel Paint方法

代碼:

private final JPanel buttonPanel = new JPanel(); 
    private final JTextArea listArea = new JTextArea(); 
    //private final JTextArea graphArea = new JTextArea(); 

下面的JPanel中我試圖輸出圖表:

private final JPanel graphArea = new JPanel(); 
    private final JButton read = new PosJButton("Read", 0); 
    private final JButton breadth = new PosJButton("Breadth", 0); 
    private final JButton depth = new PosJButton("Depth", 0); 

我的類重繪面板:

private class graphArea extends JPanel { 

     public graphArea() { 
      graphArea.setPreferredSize(new Dimension(255,255)); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 

    } 
    } 

    //method creating a new gui 
    public static void makeAndShowGUI() { 
     graphGUI gGUI = new graphGUI(); 
     gGUI.showGUI(); 
    } 

     //setting items for gui 
yout); 
      listArea.setRows(1); 
); 
      contentPane.add(buttonPanel); 
      contentPane.add(graphArea);  




            newStation.addStation(name, posx, posy); 
            System.out.println("Station test: " + name + " " + posx + " " + posy); 
           } 
           else if(type.equals("Connection:")){ 
            String statA = sc.next(); 
            String statB = sc.next(); 
            double dist = sc.nextDouble(); 



            newStation.addConnection(statA, statB, dist); 
            System.out.println("Connection test: " + statA + " " + statB + " " + dist); 
           } 
          } 

這裏是哪裏我正試圖重繪jpanel graph區域:

graphArea.repaint(); 
         }catch(FileNotFoundException ex){ 
          JOptionPane.showMessageDialog(null, "invalid file format", "Error", JOptionPane.ERROR_MESSAGE); 
         } 


     } 
     } 
    }); 

回答

0

首先,所有類名都應以大寫字母開頭。

您的GraphArea(注意名稱)的大小是(0,0),所以沒有東西可以繪製。

您需要覆蓋getPreferredSize()方法以返回面板的大小。然後佈局管理器可以正常工作。

+0

我該怎麼做? – codingmachine

+0

@codingmachine,閱讀[自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html)上的Swing教程部分的示例。 – camickr