当前位置:首页 » 编程语言 » 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"銆

热点内容
存储hur 发布:2024-11-27 16:37:30 浏览:433
手机锁密码是多少 发布:2024-11-27 16:36:46 浏览:632
大宋脚本下载 发布:2024-11-27 16:21:01 浏览:89
sql认证培训 发布:2024-11-27 15:53:54 浏览:635
php的异常处理 发布:2024-11-27 15:53:54 浏览:417
电脑设置邮件服务器 发布:2024-11-27 15:53:07 浏览:4
安卓平板叫什么名字 发布:2024-11-27 15:48:12 浏览:172
税盘密码忘了去改需要带什么 发布:2024-11-27 15:41:10 浏览:277
拉筋要加密 发布:2024-11-27 15:38:51 浏览:324
电脑当服务器怎么降低功耗 发布:2024-11-27 15:30:45 浏览:642