我想在选择完文件后更新label1的txt,但是如何在ActionListener中访问外部的label1阿??,小弱的问题麻烦各位大侠了。
JButton button = new JButton("打开文件");
      JLabel label1 = new JLabel("选择文件名:");
      button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          JFileChooser file = new JFileChooser();
          int result = file.showOpenDialog(new JPanel());
          if(result == file.APPROVE_OPTION){
            String fileName = file.getSelectedFile().getName();
//            file.getSelectedFile().getAbsolutePath()
            String dir = file.getCurrentDirectory().toString();
            
            JOptionPane.showConfirmDialog(null, dir + "\\" + fileName, "选择的文件",
                                          JOptionPane.YES_OPTION);
            }
        }
      });

解决方案 »

  1.   

    final JLabel label1 = new JLabel("选择文件名:");
    把label1设为final 就可以直接调用了
     final JLabel label1 = new JLabel("选择文件名:");
          button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
             label1.setText("===");
             
            }
          });
      

  2.   

    把lable放在构造函数外边声明也可以
    这样不用设为final
    JLabel label1 = new JLabel("选择文件名:");
    public TT(){
    this.setLayout(new FlowLayout());
    JButton button = new JButton("打开文件");
          button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
             label1.setText("===");
             
            }
          });
          this.add(button);
          this.add(label1);
    }