我正在创建一个简单的文件浏览器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());
}
}
答案 0 :(得分:0)
您的班级BrowseFile
从run
方法的中间开始。首先需要关闭run
方法才能正确定义内部类BrowseFile
。与SendFile
相同。基本上,只需在}
类定义前面移动倒数第三个BrowseFile
。