2016-04-13 131 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