小编gam*_*ast的帖子

使用JTextField时,无法在内部类错误中引用非final变量

我花了几个小时搜索,我无法弄清楚如何解决这个问题.也许我只是完全关闭,但我不断收到错误"无法引用在另一个方法中定义的内部类中的非最终变量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)

java variables swing compiler-errors jtextfield

4
推荐指数
1
解决办法
3700
查看次数

在Python中发送带有片段标识符的GET请求

我正在尝试使用包括片段标识符的请求模块发送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?

html python url get

4
推荐指数
1
解决办法
887
查看次数

标签 统计

compiler-errors ×1

get ×1

html ×1

java ×1

jtextfield ×1

python ×1

swing ×1

url ×1

variables ×1