编写程序:在窗口中添加组件,产生如下图形化界面:当用户输入用户名和电话后,点击显示按钮后,按下图格式显示。
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 class MyWind extends JFrame implements ActionListener 6 { 7 TextField text1,text2,text3; 8 JButton btn1,btn2; 9 JTextArea ta1; 10 JPanel pnl; 11 JLabel lab1,lab2; 12 13 MyWind() 14 { 15 super("添加组件的窗口"); 16 text1=new TextField(8); 17 text2=new TextField(8); 18 text3=new TextField(16); 19 pnl = new JPanel(); 20 21 Container con=getContentPane(); 22 con.setLayout(new FlowLayout()); 23 ta1 = new JTextArea(10,25); 24 ta1.setLineWrap(true); 25 JScrollPane scr=new JScrollPane(ta1); 26 pnl.setLayout(new GridLayout(1,1)); 27 pnl.add(scr); 28 add(pnl); 29 30 lab1 = new JLabel("用户名"); 31 add(lab1); 32 add(text1); 33 lab2 = new JLabel("电话"); 34 add(lab2); 35 add(text2); 36 37 add(text3); 38 btn1 = new JButton("显示"); 39 btn1.addActionListener(this); 40 btn2 = new JButton("退出"); 41 add(btn1); 42 add(btn2); 43 44 setSize(300, 300); 45 setVisible(true); 46 validate(); 47 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 } 49 50 @Override 51 public void actionPerformed(ActionEvent e) 52 { 53 String a=text1.getText(); 54 String b=text2.getText(); 55 ta1.setText(a+'\n'+b+'\n'); 56 text3.setText("你按下了“显示按钮”"); 57 } 58 59 } 60 61 public class LX9_18 62 { 63 public static void main(String[] args) 64 { 65 new MyWind(); 66 67 } 68 }
后来才发现网上有这道题目的代码,晕死没早百度到, 这里值提供个网址好了,或者以后哦再加进来。
http://www.docin.com/p-201177425.html