我正在尝试实时更改此 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(); 这是我的问题。