1

我正在尝试实时更改此 JLabel (searchedstock) 的文本。这是我的代码:

public void gamegui() {

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 2;
    JLabel welcome = new JLabel("Welcome " + playername + "!");
    welcome.setFont(new Font("Arial", 1, 30));
    gamepanel.add(welcome, c);
    c.gridx = 0;
    c.gridy = 1;
    gamepanel.add(new JPanel(), c);
    c.gridwidth = 1;
    c.gridy = 2;
    gamepanel.add(new JLabel("Symbol Search:"), c);
    c.gridx = 1;
    final JTextField symbolsearch = new JTextField(10);
    gamepanel.add(symbolsearch, c);
    c.gridx = 0;
    c.gridy = 3;
    gamepanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 2;
    JButton search = new JButton("Search");
    search.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e){

            stockvalue = Double.toString(getstocks(symbolsearch.getText()));
        }
    });
    gamepanel.add(search, c);
    c.gridx = 0;
    c.gridy = 5;
    gamepanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 2;
    JLabel searchedstock = new JLabel(stockvalue);
    gamepanel.add(searchedstock, c);
    add(gamepanel);
}

我如何通过按下该按钮实时更改值,因为它的侦听器在内部类中?它不能直接访问我的 JLabel,所以我不能使用 setText(); 这是我的问题。

4

2 回答 2

3

制作 JLabel final(并在按钮之前声明)或实例成员。然后可以从内部类中访问它。

例如

final JLabel searchedstock = new JLabel(stockvalue);

search.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e){

        String stockvalue = Double.toString(getstocks(symbolsearch.getText()));
        searchedstock.setText(stockvalue);
    }
});
于 2013-08-19T05:00:49.307 回答
3

你至少有两个选择...

选项1

制作JLabel final,但你必须在内部听众之前这样做......

public void gamegui() {
    /*...*/
    final JLabel searchedstock = new JLabel(stockvalue);
    JButton search = new JButton("Search");
    search.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e){

            stockvalue = Double.toString(getstocks(symbolsearch.getText()));
            searchedstock.setText(NumberFormat.getNumberInstance().format(stockValue));
        }
    });
    gamepanel.add(search, c);
    c.gridx = 0;
    c.gridy = 5;
    gamepanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 2;
    gamepanel.add(searchedstock, c);
    add(gamepanel);
}

选项 2

使标签成为类实例字段...

private JLabel searchedstock;

public void gamegui() {
    /*...*/
    final JLabel searchedstock = new JLabel(stockvalue);
    JButton search = new JButton("Search");
    search.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e){

            stockvalue = Double.toString(getstocks(symbolsearch.getText()));
            searchedstock.setText(NumberFormat.getNumberInstance().format(stockValue));
        }
    });
    gamepanel.add(search, c);
    c.gridx = 0;
    c.gridy = 5;
    gamepanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 2;
    searchedstock = new JLabel(stockvalue);
    gamepanel.add(searchedstock, c);
    add(gamepanel);
}
于 2013-08-19T05:02:06.313 回答