使用setText更新JLabel

时间:2013-08-15 07:57:00

标签: jlabel updating settext

enter coclass Gamehandler extends JFrame implements ActionListener {
//questions
JPanel qpanel = new JPanel();
//answers
JPanel aPanel = new JPanel();
JRadioButton[] responses;
ButtonGroup group = new ButtonGroup();
//bottom
JPanel botPanel = new JPanel();
JButton next = new JButton("next");
JButton finish = new JButton("Finish");
static String guess= " ";
readExcel re = new readExcel();
JLabel label = new JLabel();
public void actionPerformed(ActionEvent event) {
    re.readExcel1();
    Random rm = new Random();
    int radiobuttonsize = readExcel.ColumnTwo.size();
    radioButtons rbs = new radioButtons();
    scoreTest   sr = new scoreTest();
    responses = new JRadioButton[readExcel.ColumnTwo.size()];
    //adds my radio buttons 
    //adds action listeneners 
    //adds get actions
    for(int i=0;i<readExcel.ColumnTwo.size();i++){
        responses[i]=new JRadioButton(readExcel.ColumnTwo.get(i));
        group.add(responses[i]);
        aPanel.add(responses[i]);
        responses[i].addActionListener(rbs);
       responses[i].setActionCommand (String.valueOf(i));
   Object selection  =    responses[0].getAction();

    }

   int randomguessArray = rm.nextInt(radiobuttonsize);
    guess =  readExcel.ColumnOne.get(randomguessArray);
    label = new JLabel();
    label.setText(guess);
    qpanel.add(label);
    JFrame frame = new JFrame("Test");
    frame.setSize(400, 300);
    frame.setResizable(true);
    frame.setVisible(true);
    frame.add(aPanel);
    botPanel.add(next);
    botPanel.add(finish);
    frame.getContentPane().add(qpanel,BorderLayout.NORTH);
    frame.getContentPane().add(aPanel,BorderLayout.CENTER);
    frame.getContentPane().add(botPanel,BorderLayout.SOUTH);
    next.addActionListener(sr);

}

public void changeText(){
 label.setText("Work");
 System.out.print("hello");

}

我只想在我的JButton中添加一个actionlistener,然后能够在这里更改同一帧中的JLabel。这是我的计划的关键部分。这似乎是直截了当的,老实说,我无法弄明白。也许是因为它是凌晨3点53分,我刚刚工作了8个小时。我很乐意在我的代码上留下一双新鲜的眼睛。

1 个答案:

答案 0 :(得分:-1)

如果2014年的任何人仍然遇到同样的问题,方法revalidate()应该派上用场。