将 JComponent 添加到小程序的窗格



我正在尝试在按下按钮时将JComponent(例如标签)添加到小程序窗格中。我有以下一段代码:

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);

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

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

最新更新