将JComponent添加到applet的窗格

时间:2012-07-04 07:47:44

标签: java swing applet jcomponent

我在按下按钮时尝试将JComponent(例如标签)添加到applet窗格。我有以下代码:

public class TestApplet extends JApplet
{
    @Override
    public void init()
    {
        setLayout(new FlowLayout());
        JButton bt = new JButton("hit it");
        bt.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                // getContentPane().setFont(null);
                getContentPane().add(new JLabel("to the right"));
            }
        });
        add(bt);
    }
}

这不会使标签可见,除非我取消注释getContentPane().setFont(null);

请告知我应该如何正确显示标签。提前谢谢。

1 个答案:

答案 0 :(得分:2)

getContentPane().add(new JLabel("to the right"));
this.revalidate();