2016-04-13 5 views
-1

ファイルがrepaintメソッドを使用して選択されるとグラフを表示しようとしていますが、エラーは表示されませんが、グラフを表示していないということが間違っています。私はそれが動作しない理由thatsだと思うので、ペイントメソッドの新しい。JPanel Paintメソッド

コード:

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

はどこです以下は

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

はJPanelのは、私は、出力グラフにしようとしていますjpanel graphAreaを再描画しようとしています。

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


     } 
     } 
    }); 

答えて

0

まず、クラス名はすべて大文字で始める必要があります。

GraphArea(正しい名前に注意してください)のサイズは(0、0)なので、塗装するものはありません。

パネルのサイズを返すには、getPreferredSize()メソッドをオーバーライドする必要があります。その後、レイアウトマネージャは適切に動作します。

+0

どうすればいいですか? – codingmachine

+0

@codingmachine実例は[Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html)のSwingチュートリアルのセクションを参照してください。 – camickr

関連する問題