這篇文章主要介紹了java通過JFrame做一個登錄系統(tǒng)的界面完整代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
成都創(chuàng)新互聯(lián)公司主營呼蘭網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),呼蘭h5重慶小程序開發(fā)搭建,呼蘭網(wǎng)站營銷推廣歡迎呼蘭等地區(qū)企業(yè)咨詢
在java的JFrame內(nèi)通過創(chuàng)建匿名對象的方式做登錄界面
package com.sxt;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame{
JTextField txtname=new JTextField();
JPasswordField txtpass=new JPasswordField();
JButton bl=new JButton("登錄");
JButton bg=new JButton("關(guān)閉");
//構(gòu)造無參構(gòu)造器把主要的方法放在構(gòu)造器里,然后在main方法里面調(diào)
public LoginFrame(){
setBounds(25,25,250,250);
Container c = getContentPane();
c.setLayout(new GridLayout(4,2,10,10));
c.add(new JLabel("用戶名"));
c.add(txtname);
c.add(new JLabel("密碼"));
c.add(txtpass);
c.add(bl);
c.add(bg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//注意:此處是匿名內(nèi)部類
bg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
);
//注意:此處是匿名內(nèi)部類
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
建議你用jsp技術(shù)
表示層用html或jsp(DreamWare開發(fā))
控制層用servlet
模型層用javabean
數(shù)據(jù)庫你可以隨便選,建議用mysql
不是很難,我用的數(shù)據(jù)庫時oracle
如果你需要例子
留下郵箱給我。
靜態(tài)網(wǎng)頁一般用Html ,css 等就完成了, 不需編譯直接運行。jsp可以做動態(tài)網(wǎng)頁
本人急忙做的:有什么不合要求的請再告訴我:
package?show;
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
public?class?Test?extends?JFrame?implements?ActionListener
{
GridBagLayout?g=new?GridBagLayout();
GridBagConstraints?c=new?GridBagConstraints();
Test(String?str)
{
super(str);
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(g);
//調(diào)用方法
addComponent();
submit.addActionListener(this);
setVisible(true);
setLocationRelativeTo(null);//設(shè)居中顯示;
}
//在這個方法中將會添加所有的組件;
//使用的網(wǎng)格包布局;希望樓主能看懂;
public?void?addComponent()
{
//個人信息登記
noteInformation=new?JLabel("個人信息登記:");
add(g,c,noteInformation,0,0,1,1);
//用戶名
userName=new?JLabel("用戶名:");
add(g,c,userName,0,1,1,1);
//用戶名輸入框
textUserName=new?JTextField(10);
add(g,c,textUserName,1,1,2,1);
//密碼:
password=new?JLabel("密碼:");
add(g,c,password,0,2,1,1);
//密碼輸入框
textUserPassword=new?JTextField(10);
add(g,c,textUserPassword,1,2,2,1);
//性別
sex=new?JLabel("性別:");
add(g,c,sex,0,3,1,1);
//男?女單選框
sexMan=new?JRadioButton("男");
add(g,c,sexMan,1,3,1,1);
sexGirl=new?JRadioButton("女");
add(g,c,sexGirl,2,3,1,1);
ButtonGroup?group=new?ButtonGroup();
group.add(sexMan);
group.add(sexGirl);
//出生日期
birthday=new?JLabel("出生日期:");
add(g,c,birthday,0,4,1,1);
//復(fù)選框及其內(nèi)容
String[]?YEARS=new?String[65];
for(int?i=1950,k=0;i=2014;i++,k++)
{
YEARS[k]=i+"年";
}
year=new?JComboBox(YEARS);
add(g,c,year,1,4,1,1);
//復(fù)選框及內(nèi)容
month=new?JComboBox(MONTH);
add(g,c,month,2,4,1,1);
//submit按鈕
submit=new?JButton("submit");
c.insets=new?Insets(7,0,4,0);
add(g,c,submit,1,5,1,1);
result=new?JTextArea(15,20);
add(g,c,result,0,6,3,4);
}
/* public?void?ActionPerformed(ActionEvent?e)
{
String?s=textUserName.getText();
String?t=textUserPassword.getText();
String?k=sexMan.getText();
String?v=sexGirl.getText();
String?a=(String)?year.getSelectedItem();
String?b=(String)month.getSelectedItem();
String?num="用戶名:"+s+"\n"+"密碼:?"+t+"性別:?"+(k==null?v:k)+"\n"+"出生日期:"+a+"?"+b;
result.append(num);
}*/
public?void?add(GridBagLayout?g,GridBagConstraints?c,JComponent?jc,int?x?,int?y,int?gw,int?gh)
{
c.gridx=x;
c.gridy=y;
c.anchor=GridBagConstraints.WEST;
c.gridwidth=gw;
c.gridheight=gh;
g.setConstraints(jc,c);
add(jc);
}
public?static?void?main(String?args[])
{
new?Test("個人信息登記表");
}
JLabel?noteInformation,userName,password;
JLabel?sex,birthday;
JTextField?textUserName,textUserPassword;
JRadioButton?sexMan,sexGirl;
JComboBox?year,month;
JButton?submit;
JTextArea?result;
final?String[]?MONTH={"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
@Override
public?void?actionPerformed(ActionEvent?arg0)
{
String?s=textUserName.getText();
String?t=textUserPassword.getText();
String?k=sexMan.getText();
String?v=sexGirl.getText();
String?a=(String)?year.getSelectedItem();
String?b=(String)month.getSelectedItem();
String?num="用戶名:"+s+"\n"+"密碼:?"+t+"\n?性別:?"+(k==null?v:k)+"\n"+"出生日期:"+a+"?"+b;
result.setText(num);
}
}
importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面紙:3元",Label.LEFT);label2=newLabel("鋼筆:5元",Label.LEFT);label3=newLabel("書:10元",Label.LEFT);label4=newLabel("襪子:8元",Label.LEFT);button1=newButton("加入購物車");button2=newButton("加入購物車");button3=newButton("加入購物車");button4=newButton("加入購物車");button5=newButton("查看購物車");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一個面紙、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一只鋼筆、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本書、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一雙襪子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"總價為:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("購物車");}}我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個。
我?guī)湍銓懥四切㎎AVA代碼了,效果包你滿意!呵呵……
至于網(wǎng)頁的代碼嘛,這里就不貼了。你要的話HI我吧^_^。
JAVA代碼如下:
import?java.awt.BorderLayout;
import?javax.swing.*;
import?java.awt.Dimension;
import?java.awt.Rectangle;
public?class?MyLuRu?extends?JFrame?{
private?static?final?long?serialVersionUID?=?1L;
private?JPanel?jContentPane?=?null;
private?JLabel?jLabel?=?null;
private?JLabel?jLabel1?=?null;
private?JLabel?jLabel2?=?null;
private?JLabel?jLabel3?=?null;
private?JTextField?jTextField?=?null;
private?JRadioButton?jRadioButton?=?null;
private?JRadioButton?jRadioButton1?=?null;
private?JComboBox?jComboBox?=?null;
private?JComboBox?jComboBox1?=?null;
private?JButton?jButton?=?null;
private?JButton?jButton1?=?null;
private?JTextArea?jTextArea?=?null;
private?ButtonGroup?mybg=new?ButtonGroup();
/**
*?This?is?the?default?constructor
*/
public?MyLuRu()?{
super();
initialize();
}
/**
*?This?method?initializes?this
*
*?@return?void
*/
private?void?initialize()?{
this.setSize(268,?407);
this.setContentPane(getJContentPane());
this.setTitle("錄入");
this.addWindowListener(new?java.awt.event.WindowAdapter()?{
public?void?windowClosing(java.awt.event.WindowEvent?e)?{
System.exit(0);
}
});
this.setVisible(true);
}
/**
*?This?method?initializes?jContentPane
*
*?@return?javax.swing.JPanel
*/
private?JPanel?getJContentPane()?{
if?(jContentPane?==?null)?{
jLabel3?=?new?JLabel();
jLabel3.setBounds(new?Rectangle(16,?150,?65,?18));
jLabel3.setText("文化程度:");
jLabel2?=?new?JLabel();
jLabel2.setBounds(new?Rectangle(16,?108,?39,?18));
jLabel2.setText("專業(yè):");
jLabel1?=?new?JLabel();
jLabel1.setBounds(new?Rectangle(16,?66,?39,?18));
jLabel1.setText("性別:");
jLabel?=?new?JLabel();
jLabel.setBounds(new?Rectangle(16,?24,?39,?18));
jLabel.setText("姓名:");
jContentPane?=?new?JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel,?null);
jContentPane.add(jLabel1,?null);
jContentPane.add(jLabel2,?null);
jContentPane.add(jLabel3,?null);
jContentPane.add(getJTextField(),?null);
jContentPane.add(getJRadioButton(),?null);
jContentPane.add(getJRadioButton1(),?null);
mybg.add(jRadioButton);
mybg.add(jRadioButton1);
jContentPane.add(getJComboBox(),?null);
jContentPane.add(getJComboBox1(),?null);
jContentPane.add(getJButton(),?null);
jContentPane.add(getJButton1(),?null);
jContentPane.add(getJTextArea(),?null);
}
return?jContentPane;
}
/**
*?This?method?initializes?jTextField
*
*?@return?javax.swing.JTextField
*/
private?JTextField?getJTextField()?{
if?(jTextField?==?null)?{
jTextField?=?new?JTextField();
jTextField.setBounds(new?Rectangle(61,?24,?180,?18));
}
return?jTextField;
}
/**
*?This?method?initializes?jRadioButton
*
*?@return?javax.swing.JRadioButton
*/
private?JRadioButton?getJRadioButton()?{
if?(jRadioButton?==?null)?{
jRadioButton?=?new?JRadioButton();
jRadioButton.setBounds(new?Rectangle(61,?62,?38,?26));
jRadioButton.setText("男");
}
return?jRadioButton;
}
/**
*?This?method?initializes?jRadioButton1
*
*?@return?javax.swing.JRadioButton
*/
private?JRadioButton?getJRadioButton1()?{
if?(jRadioButton1?==?null)?{
jRadioButton1?=?new?JRadioButton();
jRadioButton1.setBounds(new?Rectangle(117,?62,?38,?26));
jRadioButton1.setText("女");
}
return?jRadioButton1;
}
/**
*?This?method?initializes?jComboBox
*
*?@return?javax.swing.JComboBox
*/
private?JComboBox?getJComboBox()?{
if?(jComboBox?==?null)?{
String[]?strcb={"計算機","電子","工商"};
jComboBox?=?new?JComboBox(strcb);
jComboBox.setBounds(new?Rectangle(62,?108,?93,?18));
}
return?jComboBox;
}
/**
*?This?method?initializes?jComboBox1
*
*?@return?javax.swing.JComboBox
*/
private?JComboBox?getJComboBox1()?{
if?(jComboBox1?==?null)?{
String[]?strcb2={"大專","本科","碩士","博士"};
jComboBox1?=?new?JComboBox(strcb2);
jComboBox1.setBounds(new?Rectangle(92,?150,?125,?18));
}
return?jComboBox1;
}
/**
*?This?method?initializes?jButton
*
*?@return?javax.swing.JButton
*/
private?JButton?getJButton()?{
if?(jButton?==?null)?{
jButton?=?new?JButton();
jButton.setBounds(new?Rectangle(66,?181,?60,?28));
jButton.setText("提交");
jButton.addActionListener(new?java.awt.event.ActionListener()?{
public?void?actionPerformed(java.awt.event.ActionEvent?e)?{
String?strname=jTextField.getText();
String?strsex="男";
if(jRadioButton1.isSelected()){
strsex="女";
}
String?strzy=jComboBox.getSelectedItem().toString();
String?strwh=jComboBox1.getSelectedItem().toString();
jTextArea.setText("姓名:"+strname+"\r\n"+"性別:"+strsex+"\r\n"+"專業(yè):"+strzy+"\r\n"+"文化:"+strwh);
}
});
}
return?jButton;
}
/**
*?This?method?initializes?jButton1
*
*?@return?javax.swing.JButton
*/
private?JButton?getJButton1()?{
if?(jButton1?==?null)?{
jButton1?=?new?JButton();
jButton1.setBounds(new?Rectangle(158,?181,?60,?28));
jButton1.setText("取消");
jButton1.addActionListener(new?java.awt.event.ActionListener()?{
public?void?actionPerformed(java.awt.event.ActionEvent?e)?{
System.exit(0);
}
});
}
return?jButton1;
}
/**
*?This?method?initializes?jTextArea
*
*?@return?javax.swing.JTextArea
*/
private?JTextArea?getJTextArea()?{
if?(jTextArea?==?null)?{
jTextArea?=?new?JTextArea();
jTextArea.setBounds(new?Rectangle(16,?225,?229,?130));
}
return?jTextArea;
}
public?static?void?main(String?args[]){
new?MyLuRu();
}
}
效果如下圖: