當前位置:首頁 » 編程語言 » java界面實例

java界面實例

發布時間: 2024-05-18 05:16:07

1. 用java語言設計一個界面,

  1. 首先:採用什麼技術實現

java語言可以使用awt 和swing等技術實現圖形界面

推薦使用Swing,因為Swing比AWT更專業,更漂亮,組件更豐富,功能更強大。


2. 其次:分析採用什麼布局

邊界布局BorderLayout,配合表格布局GridLayout,既簡單又美觀


3. 最後:分析需求中需要用的組件

學生姓名 學號 顯示信息 需要用到文本框JTextField

單選按鈕 需要用到組件JRadioButton

復選框 需要用到組件JCheckBox

組合框 需要用到組件JComboBox


圖片效果

//導入所需要的包
importjava.awt.event.*;
importjavax.swing.border.*;
importjavax.swing.*;
importjava.awt.*;
{//寫一個類繼承自JFrame窗體
//定義組件
=1L;
privateJPanelcontentPane;
privateJTextFieldtfName,tfNum,allInfo;
privateJRadioButtonrb1,rb2;
privateJCheckBoxcb1,cb2,cb3;
privateJComboBox<String>t1,t2,t3;
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
ClassFrameframe=newClassFrame();//創建一個窗口實例
frame.setVisible(true);//讓該窗口實例可見
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*窗口屬性的設置,內部組件的初始化
*/
publicClassFrame(){
setTitle("選課ing...");//標題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉是退出JVM
setSize(450,339);//設置窗體大小
setLocationRelativeTo(null);//窗體居中
contentPane=newJPanel();//內容面板
contentPane.setBorder(newEmptyBorder(5,5,5,5));
contentPane.setLayout(newBorderLayout(0,0));//設置布局
setContentPane(contentPane);
JPanelpanel=newJPanel(newGridLayout(5,1,5,10));//5行1列的表格布局
panel.setBorder(newTitledBorder(null,"",TitledBorder.LEADING,TitledBorder.TOP,null,null));
contentPane.add(panel,BorderLayout.CENTER);//給panel添加邊框
JPanelpanel_1=newJPanel();
panel.add(panel_1);
JLabellabel=newJLabel("姓名");
panel_1.add(label);
tfName=newJTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabellabel_2=newJLabel("學號");
panel_1.add(label_2);
tfNum=newJTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1=newJRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//設置單選按鈕中,默認選擇的按鈕
rb2=newJRadioButton("女");
panel_1.add(rb2);
ButtonGroupbts=newButtonGroup();//單選按鈕需要加入同一個ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanelpanel_2=newJPanel();
panel.add(panel_2);
cb1=newJCheckBox("高等數學");
panel_2.add(cb1);
t1=newJComboBox<String>();
t1.setModel(newDefaultComboBoxModel<String>(newString[]{"林老師","趙老師","孫老師"}));
panel_2.add(t1);
JPanelpanel_3=newJPanel();
panel.add(panel_3);
cb2=newJCheckBox("世界經濟");
panel_3.add(cb2);
t2=newJComboBox<String>();
t2.setModel(newDefaultComboBoxModel<String>(newString[]{"張老師","劉老師"}));
panel_3.add(t2);
JPanelpanel_4=newJPanel();
panel.add(panel_4);
cb3=newJCheckBox("音樂賞析");
panel_4.add(cb3);
t3=newJComboBox<String>();
t3.setModel(newDefaultComboBoxModel<String>(newString[]{"王老師","周老師"}));
panel_4.add(t3);
JPanelpanel_5=newJPanel();
panel.add(panel_5);
JButtonjbOk=newJButton("確定");
panel_5.add(jbOk);
JButtonjbRest=newJButton("重填");
panel_5.add(jbRest);
JPanelpanelSouth=newJPanel();
contentPane.add(panelSouth,BorderLayout.SOUTH);
JLabellabe=newJLabel("選課信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo=newJTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanelpanelNorth=newJPanel();
contentPane.add(panelNorth,BorderLayout.NORTH);
JLabellabelTitle=newJLabel("學生選課界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(newFont("宋體",Font.BOLD,20));
panelNorth.add(labelTitle);

//給確定按鈕添加事件處理代碼
jbOk.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
StringBuilderinfo=newStringBuilder();
Stringname=tfName.getText();
Stringnum=tfNum.getText();
Stringsex;
if(rb1.isSelected()){
sex="男";
}else{
sex="女";
}
info.append(name+num+sex);
if(cb1.isSelected()){
Stringc=cb1.getText();
Stringt=t1.getSelectedItem().toString();
info.append(""+c+t);
}
if(cb2.isSelected()){
Stringc=cb2.getText();
Stringt=t2.getSelectedItem().toString();
info.append(""+c+t);

}
if(cb3.isSelected()){
Stringc=cb3.getText();
Stringt=t3.getSelectedItem().toString();
info.append(""+c+t);
}
allInfo.setText(info.toString());//把學生信息和選課信息放到文本框
}
});
//給重填按鈕設置事件處理代碼
jbRest.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
tfName.setText("");
tfNum.setText("");
rb1.setSelected(true);
cb1.setSelected(false);
t1.setSelectedIndex(0);
cb2.setSelected(false);
t2.setSelectedIndex(0);
cb3.setSelected(false);
t3.setSelectedIndex(0);
allInfo.setText("");
}
});
}
}

2. 用java程序編寫一個簡單的登錄界面怎麼寫

程序如下:

mport java.awt.HeadlessException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

@SuppressWarnings("serial")

public class MainFrame extends JFrame {

JLabel lbl1 = new JLabel("用戶名:");

JLabel lbl2 = new JLabel("密 碼:");

JTextField txt = new JTextField("admin",20);

JPasswordField pwd = new JPasswordField(20);

JButton btn = new JButton("登錄");

JPanel pnl = new JPanel();

private int error = 0;

public MainFrame(String title) throws HeadlessException {

super(title);

init();

}

private void init() {

this.setResizable(false);

pwd.setEchoChar('*');

pnl.add(lbl1);

pnl.add(txt);

btn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if ("admin".equal花憨羔窖薏忌割媳公顱s(new String(pwd.getPassword()))){

pnl.removeAll();

JLabel lbl3 = new JLabel();

ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));

lbl3.setIcon(icon);

pnl.add(lbl3);

}

else{

if(error < 3){

JOptionPane.showMessageDialog(null,"密碼輸入錯誤,請再試一次");

error++;

}

else{

JOptionPane.showMessageDialog(null,"對不起,您不是合法用戶");

txt.setEnabled(false);

pwd.setEnabled(false);

btn.setEnabled(false);

}

}

}

});

}

public static void main(String[] args) {

MainFrame frm = new MainFrame("測試");

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setBounds(100, 100, 300, 120);

frm.setVisible(true);

}

}

3. 用java給一個系統寫界面應該怎麼寫

簡單例子可供參考:
import java.awt.*;
import javax.swing.*;

class text2 {
public static void main(String[] args) {
JTextField Name,Number,Class;
JFrame j = new JFrame();
j.setBounds(500, 300, 0, 0);
j.setTitle("Information");
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(4, 1));

final JLabel label_1 = new JLabel();
label_1.setText("Name:");
jp.add(label_1);
Name = new JTextField();
jp.add(Name);

final JLabel label_2 = new JLabel();
label_2.setText("Number:");
jp.add(label_2);
Number = new JTextField();
jp.add(Number);

final JLabel label_3 = new JLabel();
label_3.setText("Class:");
jp.add(label_3);
Class = new JTextField();
jp.add(Class);

final JButton save = new JButton();
jp.add(save);
save.setText("確認");

j.add(jp);
j.pack();
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

4. 用JAVA編寫一個用戶登陸界面,用戶驗證通過資料庫實現

這是我以前寫的系統登錄框的login
package java1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Rectangle;
import java.awt.Font;

public class login extends JFrame
{
public login() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args)
{
login frm=new login();
frm.setBounds(300,200,300,260);
frm.setVisible(true);

}

private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jLabel1.setForeground(Color.red);
jLabel1.setText("用戶名");
jLabel1.setBounds(new Rectangle(25, 72, 68, 29));
jButton2.setBounds(new Rectangle(142, 159, 81, 30));
jButton2.setText("取消");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton1.setBounds(new Rectangle(38, 159, 81, 29));
jButton1.setText("確定");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField1.setBounds(new Rectangle(109, 79, 136, 23));
jPasswordField1.setBounds(new Rectangle(109, 115, 136, 23));
this.getContentPane().add(jLabel1);
jLabel3.setFont(new java.awt.Font("隸書", Font.PLAIN, 24));
jLabel3.setText("學 生 考 試 系 統");
jLabel3.setBounds(new Rectangle(22, 15, 268, 47));
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jPasswordField1);
jLabel2.setForeground(Color.red);
jLabel2.setText("密碼");
jLabel2.setBounds(new Rectangle(26, 112, 68, 29));
}

JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel3 = new JLabel();
JPasswordField jPasswordField1 = new JPasswordField();
main2 wo=new main2();
int sum;

//判斷許可權的方法
public void success()
{
try{
ResultSet rs1;
String s1="select * from 管理許可權 where name='"+jTextField1.getText()+"' and password='"+jPasswordField1.getText()+"'";
rs1=sqlcx.Rs_jiluji(s1);
rs1.first();
sum=Integer.parseInt((String)(rs1.getString(3)));
System.out.println(sum);
very();
}
catch(Exception c){System.out.println("success error");}
}

//根據許可權設置窗口
public void very()
{
if (sum==1)
{JOptionPane.showOptionDialog(this, "您現在是以教師許可權登陸", "登陸信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);

}
else
{
JOptionPane.showOptionDialog(this, "你現在是以學生許可權登陸", "登陸信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
wo.jButton2.setVisible(false);
wo.jButton3.setVisible(false);
wo.jButton4.setVisible(false);
wo.jButton5.setVisible(false);
wo.jButton6.setVisible(false);
wo.jButton7.setVisible(false);
wo.jButton8.setVisible(false);
wo.jMenu2.setVisible(false);
wo.jMenu3.setVisible(false);
wo.jMenu4.setVisible(false);
}

}

public void jButton1_actionPerformed(ActionEvent e)
{
//判斷用戶名是否為空
if (jTextField1.getText().length() == 0) {
JOptionPane.showOptionDialog(this, "用戶名不能為空", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
//判斷密碼是否為空
else if (jPasswordField1.getText().length()==0)
{
JOptionPane.showOptionDialog(this,"密碼不能為空", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
else
{
try
{
ResultSet rs;//聲明記錄集
String sql="select * from 管理許可權 where name='"+jTextField1.getText()+"'";
rs = sqlcx.Rs_jiluji(sql);
if (rs.next())
{
if(jPasswordField1.getText().equals(rs.getString(2)))
{

success();
wo.setBounds(60,40,700,600);
wo.setVisible(true);
this.dispose();
}
else
{
JOptionPane.showOptionDialog(this,"密碼錯誤", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
else
{
JOptionPane.showOptionDialog(this,"沒有所要找的用戶名", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}

}
catch(Exception c){
JOptionPane.showOptionDialog(this,"連接資料庫失敗", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
}
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
}

5. 鐢╦ava鍐欎竴涓鐧婚檰鐣岄潰浠g爜銆

姒傝堪

鍏蜂綋妗嗘灦浣跨敤jframe錛屾枃鏈妗嗙粍浠訛細JTextField錛涘瘑鐮佹嗙粍浠訛細JPasswordField錛涙爣絳劇粍浠訛細JLabel錛涘嶉夋嗙粍浠訛細JCheckBox錛涘崟閫夋嗙粍浠訛細JRadioButton錛涙寜閽緇勪歡JButton銆

鐧誨綍鐣岄潰錛

Swing 鏄涓涓涓篔ava璁捐$殑GUI宸ュ叿鍖呫

Swing鏄疛AVA鍩虹綾葷殑涓閮ㄥ垎銆

Swing鍖呮嫭浜嗗浘褰㈢敤鎴風晫闈錛圙UI錛夊櫒浠跺傦細鏂囨湰妗嗭紝鎸夐挳錛屽垎闅旂獥鏍煎拰琛ㄣ

Swing鎻愪緵璁稿氭瘮AWT鏇村ソ鐨勫睆騫曟樉紺哄厓緔犮傚畠浠鐢ㄧ函Java鍐欐垚錛屾墍浠ュ悓Java鏈韜涓鏍峰彲浠ヨ法騫沖彴榪愯岋紝榪欎竴鐐逛笉鍍廇WT銆傚畠浠鏄疛FC鐨勪竴閮ㄥ垎銆傚畠浠鏀鎸佸彲鏇存崲鐨勯潰鏉垮拰涓婚橈紙鍚勭嶆搷浣滅郴緇熼粯璁ょ殑鐗規湁涓婚橈級錛岀劧鑰屼笉鏄鐪熺殑浣跨敤鍘熺敓騫沖彴鎻愪緵鐨勮懼囷紝鑰屾槸浠呬粎鍦ㄨ〃闈涓婃ā浠垮畠浠銆傝繖鎰忓懗鐫浣犲彲浠ュ湪浠繪剰騫沖彴涓婁嬌鐢↗AVA鏀鎸佺殑浠繪剰闈㈡澘銆傝交閲忕駭緇勪歡鐨勭己鐐瑰垯鏄鎵ц岄熷害杈冩參錛屼紭鐐瑰氨鏄鍙浠ュ湪鎵鏈夊鉤鍙頒笂閲囩敤緇熶竴鐨勮屼負銆

姒傚康瑙f瀽錛

JFrame_ java鐨凣UI紼嬪簭鐨勫熀鏈鎬濊礬鏄浠JFrame涓哄熀紜錛屽畠鏄灞忓箷涓妛indow鐨勫硅薄錛岃兘澶熸渶澶у寲銆佹渶灝忓寲銆佸叧闂銆

JPanel_ Java鍥懼艦鐢ㄦ埛鐣岄潰(GUI)宸ュ叿鍖卻wing涓鐨勯潰鏉垮瑰櫒綾伙紝鍖呭惈鍦╦avax.swing 鍖呬腑錛屽彲浠ヨ繘琛屽祵濂楋紝鍔熻兘鏄瀵圭獥浣撲腑鍏鋒湁鐩稿悓閫昏緫鍔熻兘鐨勭粍浠惰繘琛岀粍鍚堬紝鏄涓縐嶈交閲忕駭瀹瑰櫒錛屽彲浠ュ姞鍏ュ埌JFrame紿椾綋涓銆傘

JLabel_ JLabel 瀵硅薄鍙浠ユ樉紺烘枃鏈銆佸浘鍍忔垨鍚屾椂鏄劇ず浜岃呫傚彲浠ラ氳繃璁劇疆鍨傜洿鍜屾按騫沖歸綈鏂瑰紡錛屾寚瀹氭爣絳炬樉紺哄尯涓鏍囩懼唴瀹瑰湪浣曞勫歸綈銆傞粯璁ゆ儏鍐典笅錛屾爣絳懼湪鍏舵樉紺哄尯鍐呭瀭鐩村眳涓瀵歸綈銆傞粯璁ゆ儏鍐典笅錛屽彧鏄劇ず鏂囨湰鐨勬爣絳炬槸寮濮嬭竟瀵歸綈錛涜屽彧鏄劇ず鍥懼儚鐨勬爣絳懼垯姘村鉤灞呬腑瀵歸綈銆

JTextField_涓涓杞婚噺綰х粍浠訛紝瀹冨厑璁哥紪杈戝崟琛屾枃鏈銆

JPasswordField_ 鍏佽告垜浠杈撳叆浜嗕竴琛屽瓧鍍忚緭鍏ユ嗭紝浣嗛殣鈃忔槦鍙(*) 鎴栫偣鍒涘緩瀵嗙爜(瀵嗙爜)

JButton_ JButton 綾葷殑瀹炰緥銆傜敤浜庡壋寤烘寜閽綾諱技瀹炰緥涓鐨 "Login"銆

熱點內容
安卓平板叫什麼名字 發布:2024-11-27 15:48:12 瀏覽:171
稅盤密碼忘了去改需要帶什麼 發布:2024-11-27 15:41:10 瀏覽:276
拉筋要加密 發布:2024-11-27 15:38:51 瀏覽:323
電腦當伺服器怎麼降低功耗 發布:2024-11-27 15:30:45 瀏覽:641
蘋果手機誇克緩存的視頻怎麼轉為本地視頻 發布:2024-11-27 15:24:05 瀏覽:810
linuxm4 發布:2024-11-27 15:15:12 瀏覽:321
演算法交易現狀 發布:2024-11-27 15:05:49 瀏覽:825
搜同網伺服器為什麼登錄不上 發布:2024-11-27 15:05:41 瀏覽:287
如何讓無線網速變快華為安卓手機 發布:2024-11-27 15:04:13 瀏覽:595
cod17編譯著色器卡住c盤滿了 發布:2024-11-27 14:55:56 瀏覽:386