[Java] swing登陆界面 →→→→→进入此内容的聊天室

来自 , 2020-01-31, 写在 Java, 查看 158 次.
URL http://www.code666.cn/view/ebd6d2f5
  1. package test;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6.  
  7. import java.awt.event.ActionEvent;
  8.  
  9. import java.awt.event.ActionListener;
  10.  
  11. public class Test extends JFrame implements ActionListener
  12.  
  13. {
  14.  
  15. /**
  16.  *
  17.  */
  18. private static final long serialVersionUID = 1L;
  19.  
  20.  
  21.  
  22. Test(String str)
  23.  
  24. {
  25.  
  26. super(str);
  27.  
  28. setSize(300,500);
  29.  
  30. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.  
  32. setLayout(g);
  33.  
  34. //调用方法
  35.  
  36. addComponent();
  37.  
  38. submit.addActionListener(this);
  39.  
  40. setVisible(true);
  41.  
  42. setLocationRelativeTo(null);//设居中显示;
  43.  
  44. }
  45.  
  46. //在这个方法中将会添加所有的组件;
  47.  
  48. //使用的网格包布局;希望楼主能看懂;
  49.  
  50. public void addComponent()
  51.  
  52. {
  53.  
  54. //个人信息登记
  55.  
  56. noteInformation=new JLabel("个人信息登记:");
  57.  
  58. add(g,c,noteInformation,0,0,1,1);
  59.  
  60. //用户名
  61.  
  62. userName=new JLabel("用户名:");
  63.  
  64. add(g,c,userName,0,1,1,1);
  65.  
  66. //用户名输入框
  67.  
  68. textUserName=new JTextField(10);
  69.  
  70. add(g,c,textUserName,1,1,2,1);
  71.  
  72. //密码:
  73.  
  74. password=new JLabel("密码:");
  75.  
  76. add(g,c,password,0,2,1,1);
  77.  
  78. //密码输入框
  79.  
  80. textUserPassword=new JTextField(10);
  81.  
  82. add(g,c,textUserPassword,1,2,2,1);
  83.  
  84. //性别
  85.  
  86. sex=new JLabel("性别:");
  87.  
  88. add(g,c,sex,0,3,1,1);
  89.  
  90. //男 女单选框
  91.  
  92. sexMan=new JRadioButton("男");
  93.  
  94. add(g,c,sexMan,1,3,1,1);
  95.  
  96. sexGirl=new JRadioButton("女");
  97.  
  98. add(g,c,sexGirl,2,3,1,1);
  99.  
  100.  
  101. group.add(sexMan);
  102.  
  103. group.add(sexGirl);
  104.  
  105. //出生日期
  106.  
  107. birthday=new JLabel("出生日期:");
  108.  
  109. add(g,c,birthday,0,4,1,1);
  110.  
  111. //复选框及其内容
  112.  
  113. String[] YEARS=new String[65];
  114.  
  115. for(int i=1950,k=0;i<=2014;i++,k++)
  116.  
  117. {
  118.  
  119. YEARS[k]=i+"年";
  120.  
  121. }
  122.  
  123. year=new JComboBox(YEARS);
  124.  
  125. add(g,c,year,1,4,1,1);
  126.  
  127. //复选框及内容
  128.  
  129. month=new JComboBox(MONTH);
  130.  
  131. add(g,c,month,2,4,1,1);
  132.  
  133. //submit按钮
  134.  
  135. submit=new JButton("submit");
  136.  
  137. c.insets=new Insets(7,0,4,0);
  138.  
  139. add(g,c,submit,1,5,1,1);
  140.  
  141. result=new JTextArea(15,20);
  142.  
  143. add(g,c,result,0,6,3,4);
  144.  
  145. }
  146.  
  147. /* public void ActionPerformed(ActionEvent e)
  148.  
  149. {
  150.  
  151. String s=textUserName.getText();
  152.  
  153. String t=textUserPassword.getText();
  154.  
  155. String k=sexMan.getText();
  156.  
  157. String v=sexGirl.getText();
  158.  
  159. String a=(String) year.getSelectedItem();
  160.  
  161. String b=(String)month.getSelectedItem();
  162.  
  163. String num="用户名:"+s+"\n"+"密码: "+t+"性别: "+(k==null?v:k)+"\n"+"出生日期:"+a+" "+b;
  164.  
  165. result.append(num);
  166.  
  167. }*/
  168.  
  169. public void add(GridBagLayout g,GridBagConstraints c,JComponent jc,int x ,int y,int gw,int gh)
  170.  
  171. {
  172.  
  173. c.gridx=x;
  174.  
  175. c.gridy=y;
  176.  
  177. c.anchor=GridBagConstraints.WEST;
  178.  
  179. c.gridwidth=gw;
  180.  
  181. c.gridheight=gh;
  182.  
  183. g.setConstraints(jc,c);
  184.  
  185. add(jc);
  186.  
  187. }
  188.  
  189. public static void main(String args[])
  190.  
  191. {
  192.  
  193. new Test("个人信息登记表");
  194.  
  195. }
  196.  
  197. JLabel noteInformation,userName,password;
  198.  
  199. JLabel sex,birthday;
  200.  
  201. JTextField textUserName,textUserPassword;
  202.  
  203. JRadioButton sexMan,sexGirl;
  204.  
  205. JComboBox year,month;
  206.  
  207. JButton submit;
  208.  
  209. JTextArea result;
  210.  
  211. final String[] MONTH={"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
  212.  
  213. @Override
  214.  
  215. public void actionPerformed(ActionEvent arg0)
  216.  
  217. {
  218.  
  219. String s=textUserName.getText();
  220.  
  221. String t=textUserPassword.getText();
  222.  
  223. String k=sexMan.getText();
  224.  
  225. String v=sexGirl.getText();
  226.  
  227. String a=(String) year.getSelectedItem();
  228.  
  229. String b=(String)month.getSelectedItem();
  230.  
  231. String num="用户名:"+s+"\n"+"密码: "+t+"\n 性别: "+(k==null?v:k)+"\n"+"出生日期:"+a+" "+b;
  232.  
  233. result.setText(num);
  234.  
  235. }
  236.  
  237. }

回复 "swing登陆界面"

这儿你可以回复上面这条便签

captcha