1

基本上我有一个带有卡片的程序(我正在使用 CardLayout),当用户输入句子或他们输入的任何内容时,我希望在他们点击名为 create 的按钮时将其添加到下一页上的标签中。我不确定如何保存输入的字段并将其作为变量放入标签中。有任何想法吗?如有必要,我可以提供我的代码。

createButton2.addActionListener(new ActionListener() {   //Back button listener, switches back to ADMIN fixtures panel
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(container, "6");
                String theText = descriptionField.getText();
                fixtureDescLabel.setText( theText );
                fixtureDescLabel.setBounds(250, 150, 200, 40);
                add(fixtureDescLabel);
            }
        });
4

1 回答 1

2

这很简单。

从 textArea 中获取文本:

String theText = myTextArea.getText();

贴上标签:

myLabel.setText( theText );

在按钮侦听器中:

myButton.addActionListener( new ActionListener() {
    @override public actionPerformed( ActionEvent event )
    {
        String theText = myTextArea.getText();
        myLabel.setText( theText );
    }
} );

编辑

查看您的编辑,您的问题是您将一个组件添加到您的框架而没有重新验证(JFrame#revalidate())它。

于 2014-02-17T20:52:26.537 回答