java gettext用法,Swing / Java:如何正确使用getText和setText字符串

I'm trying to make input nameField appear in a Label called label1 after a Button called button1 is clicked.

Right now it says: 'txt' and I understand why. But I don't know how I can use the string!

Can anyone explain me what I'm doing wrong and how to use this string properly?

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class thisismytest2 {

public static void main(String[] args) {

final JFrame frame = new JFrame();

JPanel panel = new JPanel();

JTextField nameField = new JTextField("...", 2);

JButton button1 = new JButton();

final JLabel label1 = new JLabel();

label1.setText("txt");

label1.setVisible(false);

String txt = nameField.getText();

frame.add(panel);

panel.add(button1);

panel.add(label1);

frame.setSize(200,200);

frame.setVisible(true);

panel.add(nameField);

frame.setSize(600,400);

nameField.setBounds(400, 40, 400, 30);

button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

label1.setVisible(true);

}

});

}

}

解决方案

You are setting the label text before the button is clicked to "txt". Instead when the button is clicked call setText() on the label and pass it the text from the text field.

Example:

label1.setText(nameField.getText());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值