Swing组件通信

时间:2015-02-21 17:52:59

标签: java swing

我正在构建一个算法模拟工具,我正在努力获得实例化的大型机类,并将所有子组件添加到JFrame中以获取伪代码所在位置的文件系统位置(对于伪代码面板。)

我已经使每个算法都有一个字符串,详细说明了每个算法的相关文本文件信息。 我有一个方法,然后在传入算法时获取字符串并将其存储到字符串变量中。 然后将此字符串变量传递给面板。 不幸的是,这是抛出一个空指针,我一直在尝试调试它并且无法走远。

  public class SortAnimator extends JFrame
{  
   private static final int VALUES_LENGTH = 30;
   private static final int FRAME_WIDTH = 1200;
   private static final int FRAME_HEIGHT = 700;
   private PsuedocodePanel pseudoPanel;
   private Menu menu;
   private InformationPanel infoPanel;
   private String algoName;
   public String algoLocation;


   public SortAnimator(Sorter s) throws IOException
   {  
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      menu=new Menu();
      pseudoPanel=new PsuedocodePanel();
      ArrayComponent panel = new ArrayComponent();
      infoPanel= new InformationPanel();

      add(menu,BorderLayout.NORTH);
      add(panel, BorderLayout.CENTER);
      add(pseudoPanel,BorderLayout.WEST);
      add(infoPanel,BorderLayout.SOUTH);

      setSize(FRAME_WIDTH, FRAME_HEIGHT);
      setVisible(true);

      int[] values = new int[VALUES_LENGTH];
      for (int i = 0; i < values.length; i++)
         values[i] = (int) (Math.random() * panel.getHeight());

      s.setValues(values);
      s.setPanel(panel);
      Thread t = new Thread(s);
      t.start();
      algoName=s.getAlgorithmName();
      algoLocation =s.getAlgorithmLocation();
       System.out.println(algoLocation);
      pseudoPanel.passFileLocation(algoLocation);
   }


}




    public class PsuedocodePanel extends JPanel{

        private JTextArea txtArea;
        private String textFile;
        private String fileLocation;


        public PsuedocodePanel() throws FileNotFoundException, IOException{

       setLayout(new BorderLayout());

       txtArea=new JTextArea();
       txtArea.setEditable(false);



         add(txtArea,BorderLayout.CENTER);
         FileReader fr = new FileReader(this.fileLocation);
         BufferedReader reader=new BufferedReader(fr);
         txtArea.read(reader,null);

         Dimension dim=getPreferredSize();//returns object
         System.out.println(getPreferredSize());
         dim.width=300;
         dim.height=75;
         setPreferredSize(dim);

           Border innerBorder=BorderFactory.createTitledBorder("Algorithm Psuedocode");
           Border outerBorder=BorderFactory.createEmptyBorder(5,5,5,5);

           setBorder(BorderFactory.createCompoundBorder(outerBorder,innerBorder));


        }

       public void passFileLocation(String algoLocation) {
            this.fileLocation= algoLocation;
        }

1 个答案:

答案 0 :(得分:0)

您的PsuedocodePanel班级使用FileReader在其构造函数中创建this.fileLocation,但代码中的fileLocation将为空 - 请参阅下面的简化代码片段:

public class PsuedocodePanel extends JPanel{

    private String fileLocation;  // not initialised

    // constructor does not accept a fileLocation...
    public PsuedocodePanel() throws FileNotFoundException, IOException{

    // ... so this.fileLocation is null here:
    FileReader fr = new FileReader(this.fileLocation);