一道java编程
创建带有标签和文本框的窗体。当用户在文本框中输入其姓名后按回车,显示欢迎用户使用 java 编程的消息。例如用户输入姓名 flyhorse ,则显示消息“你好, flyhorse, 欢迎你使用 java 编程!”,并能正常关闭窗口。
创建带有标签和文本框的窗体。当用户在文本框中输入其姓名后按回车,显示欢迎用户使用 java 编程的消息。例如用户输入姓名 flyhorse ,则显示消息“你好, flyhorse, 欢迎你使用 java 编程!”,并能正常关闭窗口。
答案:import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener{
JLabel name,display;
JTextField content;
public Test(){
this.setLayout(new FlowLayout());
//控制窗口使它能够正常关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JLabel("用户名:");
display = new JLabel();
content = new JTextField("请输入用户名",12);
//添加监视器
content.addActionListener(this);
this.add(name);
this.add(content);
this.add(display);
this.setSize(300,100);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
}
//响应文本框的改变事件
public void actionPerformed(ActionEvent e) {
if(e.getSource()==content){
display.setText("你好,"+content.getText()+",欢迎你使用 java 编程!");
}
}
}
其他:import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestText extends Frame {
public void luanchFrame() {
TextField tt = new TextField();
this.setBounds(200, 200, 200, 100);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
setVisible(false);
}
});
TaActionListener tl = new TaActionListener();
this.add(tt);
tt.addActionListener(tl);
this.setVisible(true);
}
public class TaActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField)e.getSource();
tf.setText("你好:" + tf.getText() + ",欢迎你使用java编程");
}
}
public static void main(String[] args) {
new TestText().luanchFrame();
}
}
上一个:国产手机,长江A9手机使用什么平台。 是否可以使用java
下一个:JAVA方面split的问题!! String hour = new Date().toString().split(" ")[3].split(":")[0];