java怎么写页面( 二 )


import javax.swing.event.*;
2.选择界面风格
Swing允许选择程序的图形界面风格常用的有java风格,windows风格等
下面的代码用于选择图形界面风格,这里选择的是跨平台的Java界面风格 。
try { UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName( )); }
catch (Exception e) { }
(3) 设置顶层容器
图形界面至少要有一个顶级Swing容器
顶级Swing容器为其它Swing组件在屏幕上的绘制和处理事件提供支持
常用的顶级容器:
JFrame(框架):表示主程序窗口
JDialog(对话框):每个JDialog对象表示一个对话框,对话框属于二级窗口
JApplet(小程序):在浏览器内显示一个小程序界面
一个框架包括边界、菜单栏、工具栏、状态栏,以及中间占主要部分的窗格
窗格也可以看作是一种面板,但它是框架的一个组成部分
组件不会直接放到框架上,而是放在若干个面板上,这些面板再放到窗格上
用框架对象的getContentPane()函数来获得窗格,再调用窗格的add()函数放置面板
public static void main(String[ ]args){JFrame frame=new JFrame("SwingApplication");
JPanel panel1=new JPanel();
frame.getContentPane().add(panel1,BorderLayout.CENTER);
。//添加其他组件
frame.pack();frame.setVisible(true);}
7.用java写界面注册怎么写package com.demo.client;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.*; public class RegistFrame extends JFrame implements ActionListener{ String serverIp = null; int port = 0; String strSex = ""; int PswProtectNum; String area = ""; JLabel labUserId; JLabel labChatname; JLabel labPsw; JLabel labRePsw; JLabel labPswQst; JLabel labPswAns; JLabel labAge; JLabel labArea; JLabel labSex; JTextField txtUserID; JTextField txtChatname; JPasswordField txtPsw; JPasswordField txtRePsw; JComboBox comPswQst; JTextField txtPswAns; JTextField txtAge; JComboBox comArea; JRadioButton rad1,rad2,rad3; JButton btnSubmit; JButton btnCancel; JPanel p0,p1,p3,p4,p5,p6,p7,p8,p9,p10; ItemListener listener1 = new ItemListener() { public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub JRadioButton button = (JRadioButton)e.getItemSelectable(); if (button == rad1)strSex = "男"; if (button == rad2)strSex = "女"; if (button == rad3)strSex = ""; } }; ItemListener listener2 = new ItemListener() { public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub JComboBox box = (JComboBox)e.getItemSelectable(); if(box == comPswQst){ PswProtectNum = comPswQst.getSelectedIndex(); } if(box == comArea){ area = comArea.getSelectedItem().toString(); } } }; public RegistFrame(String serverIp,int port) { this.serverIp = serverIp; this.port = port; p0 = new JPanel(); labUserId = new JLabel("*帐号:"); txtUserID = new JTextField(20); txtUserID.setEditable(true); p0.add(labUserId); p0.add(txtUserID); p1 = new JPanel(); labChatname = new JLabel("*昵称:"); txtChatname = new JTextField(20); txtChatname.setEditable(true); p1.add(labChatname); p1.add(txtChatname); p3 = new JPanel(); labPsw = new JLabel("*请输入密码:"); txtPsw = new JPasswordField(20); p3.add(labPsw); p3.add(txtPsw); p4 = new JPanel(); labRePsw = new JLabel("*请再次输入密码:"); txtRePsw = new JPasswordField(20); p4.add(labRePsw); p4.add(txtRePsw); p5 = new JPanel(); labPswQst = new JLabel("请设定密码保护问题:"); String [] strQst = { "","您母亲的姓名是?","您父亲的职业是?","您配偶的生日是?","您的学号(工号)是?","您母亲的生日是?","您高中班主任的姓名是?","您父亲的姓名是?","您的出生地是?","您小学班主任的姓名是?","您配偶的姓名是?" }; comPswQst = new JComboBox(strQst); comPswQst.addItemListener(listener2); p5.add(labPswQst); p5.add(comPswQst); p6 = new JPanel(); labPswAns = new JLabel("请设定密码保护答案:"); txtPswAns = new JTextField(20); p6.add(labPswAns); p6.add(txtPswAns); p7 = new JPanel(); labAge = new JLabel("您的性别是:"); rad1 = new JRadioButton("我是帅哥!"); rad1.addItemListener(listener1); rad2 = new JRadioButton("我是美女!"); rad2.addItemListener(listener1); rad3 = new JRadioButton("我不告诉你!"); rad3.addItemListener(listener1); ButtonGroup group = new ButtonGroup(); group.add(rad1); group.add(rad2); group.add(rad3); p7.add(labAge); p7.add(rad1); p7.add(rad2); p7.add(rad3); p8 = new JPanel(); labArea = new JLabel("请选择您的地区:"); String [] strArea = { "我是南方人","我是北方人","我是东北人","我是西北人","我是西南人" }; comArea = new JComboBox(strArea); comArea.addItemListener(listener2); p8.add(labArea); p8.add(comArea); p9 = new JPanel(); labAge = new JLabel("您的年龄是:"); txtAge = new JTextField(15); p9.add(labAge); p9.add(txtAge); p10 = new JPanel(); btnSubmit = new JButton("提交"); btnSubmit.addActionListener(this); btnCancel = new JButton("取消"); btnCancel.addActionListener(this); p10.add(btnSubmit); p10.add(btnCancel); getContentPane().setLayout(new GridLayout(10,1)); getContentPane().add(p0); getContentPane().add(p1); getContentPane().add(p3); getContentPane().add(p4); getContentPane().add(p5); getContentPane().add(p6); getContentPane().add(p7); getContentPane().add(p8); getContentPane().add(p9); getContentPane().add(p10); setTitle("注册新用户"); pack(); setResizable(false); setDefaultCloseOperation(RegistFrame.DISPOSE_ON_CLOSE); Toolkit tk=Toolkit.getDefaultToolkit(); setLocation((tk.getScreenSize().width-getSize().width)/2,(tk.getScreenSize().height-getSize().height)/2); setVisible(true); } public void。