Java學習——用戶電話輸入顯示
阿新 • • 發佈:2018-12-18
編寫程序 用戶名 policy wing npe == public main container
編寫程序:在窗口中添加組件,產生如下圖形化界面:當用戶輸入用戶名和電話後,點擊顯示按鈕後,按下圖格式顯示。
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()+"\n"+"用戶名:"+str1+"\n電話:"+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); } }
Java學習——用戶電話輸入顯示