Java学习——用户电话输入显示
编写程序:在窗口中添加组件,产生如下图形化界面:当用户输入用户名和电话后,点击显示按钮后,按下图格式显示。
package cys;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class window8 extends JFrame implements ActionListener{
JTextField namtxt , photxt,tishi;
JTextArea btxt ;
JComboBox jcb;
JLabel name,phone;
JButton show,quit;
window8(){
Container con = getContentPane();
con.setLayout(new FlowLayout());
btxt=new JTextArea(10,12);
//下拉框
//String str1[] = {"fdsa", "fgs", "gfh", "345354","fff"};
//jcb = new JComboBox(str1);
//con.add(jcb);
JScrollPane scroll = new JScrollPane(btxt);
scroll.setBounds(0, 0, 500, 500);
//默认的设置是超过文本框才会显示滚动条,以下设置让滚动条一直显示
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
name=new JLabel("用户名",JLabel.CENTER);
phone=new JLabel("电话",JLabel.CENTER);
namtxt=new JTextField(10);
photxt=new JTextField(10);
tishi=new JTextField(15);
show=new JButton("显示");
quit=new JButton("退出");
//把滚动条添加到容器里面
con.add(scroll);
con.add(name);
con.add(namtxt);
con.add(phone);
con.add(photxt);
con.add(tishi);
con.add(show);
con.add(quit);
show.addActionListener(this);
quit.addActionListener(this);
this.setBounds(100,100,550,550);
this.setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==show) {
StringBuffer str1,str2;
str1 = new StringBuffer(namtxt.getText());
str2 = new StringBuffer(photxt.getText());
btxt.setText(btxt.getText()+"
"+"用户名:"+str1+"
电话:"+str2);
tishi.setText("你正在输入信息");
}
else if(e.getSource()==quit) {
System.exit(0);
}
}
}
public class PhoneNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
window8 win = new window8();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}