实时更改JLabel的文本值

时间:2013-08-19 04:58:28

标签: java swing actionlistener jlabel settext

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

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();这是我的问题。

2 个答案:

答案 0 :(得分: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);
    }
});

答案 1 :(得分: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);
}