“变量”无法解析输入?! - Java Swing

时间:2014-12-03 20:32:45

标签: java swing user-interface

我正在创建一个简单的文件浏览器GUI,但我收到了这些错误!它说BrwoseFile和sendfile无法解析为一个类型?但它适用于uni计算机。有什么想法吗?

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
BrowseFile cannot be resolved to a type
SendFile cannot be resolved to a type

这是我gui的代码:

public class NetZipGUI implements Runnable{

private JLabel path;
private JTextField file;
private JButton send;


public void run() {
    JFrame gui = new JFrame("File Explorer");
    gui.setSize(400, 300);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container pane = gui.getContentPane();
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

    JPanel TPanel = new JPanel();
    TPanel.setLayout(new BoxLayout(TPanel, BoxLayout.LINE_AXIS));

    path = new JLabel("File path");
    file = new JTextField(40);

    TPanel.add(path);
    TPanel.add(file);

    JButton browse = new JButton("Browse file");     
    TPanel.add(browse);
    browse.addActionListener(new BrowseFile());<---- Here

    pane.add(TPanel);

    JPanel BPanel = new JPanel();

    send = new JButton("Send file");
    send.setEnabled(false);
    BPanel.add(send);
    send.addActionListener(new SendFile());<---- Here

    pane.add(BPanel);

    gui.pack();
    gui.setVisible(true);

   class BrowseFile implements ActionListener{


    public void actionPerformed(ActionEvent e) {
        int value = 0; 

        JFileChooser fc = new JFileChooser();
        fc.showOpenDialog(fc);

        if(value == JFileChooser.APPROVE_OPTION){
            File sF = fc.getSelectedFile();
            file.setText(sF.getAbsolutePath());
            send.setEnabled(true);

        }

    }

}

class SendFile implements ActionListener{
    public void actionPerformed(ActionEvent e){

    }
}

}

public static void main(String[] args){
    SwingUtilities.invokeLater(new NetZipGUI());
  }
}

1 个答案:

答案 0 :(得分:0)

您的班级BrowseFilerun方法的中间开始。首先需要关闭run方法才能正确定义内部类BrowseFile。与SendFile相同。基本上,只需在}类定义前面移动倒数第三个BrowseFile