我花了几个小时搜索,我无法弄清楚如何解决这个问题.也许我只是完全关闭,但我不断收到错误"无法引用在另一个方法中定义的内部类中的非最终变量userInput".如果有人可以帮助我弄清楚为什么会发生这种情况或如何解决它,那将不胜感激.
我得到2个编译错误:不能在不同方法中定义的内部类中引用非最终变量userInput
和
不能在不同方法中定义的内部类中引用非final变量inputField
编辑:一些澄清,我想保持我的userInput变量不是最终的.
这是我的代码,也许有人可以看到我做错了什么,我省略了与此错误无关的所有代码:
//Import libraries
...
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
...
public class TextGame {
public static void main(String[] args) throws FileNotFoundException {
...
String userInput = "Input";
...
// Create the window
JFrame gameWindow = new JFrame("Game");
gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameWindow.setVisible(true);
// Centre the window
gameWindow.setLocationRelativeTo(null);
...
// Add input box to window
JTextField inputField = new JTextField();
inputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userInput = inputField.getText(); ****Here is where the error …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用包括片段标识符的请求模块发送GET请求。我有以下代码:
url = 'http://steamcommunity.com/market/search?appid=730#p20_quantity_desc'
page = requests.get(url, headers=headers)
Run Code Online (Sandbox Code Playgroud)
但是,我总是最终得到基本页面(http://steamcommunity.com/market/search?appid=730)而不是带有片段标识符的页面(似乎未发送#p20_quanitity_description)。
urllib2也不适用于以下代码:
req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0' })
page = urllib2.urlopen(req).read().decode('UTF-8', 'ignore')
Run Code Online (Sandbox Code Playgroud)
如何发送GET请求并在URL中包含#p20_quanitity_description?