以下方法實(shí)現(xiàn)了用戶界面登陸
我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、蓬溪ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的蓬溪網(wǎng)站制作公司
import java.awt.*;
import java.awt.event.*;
public class DengLuJieMian extends Frame implements ActionListener
{
Label username=new Label("用戶名:");//使用文本創(chuàng)建一個(gè)用戶名標(biāo)簽
TextField t1=new TextField();//創(chuàng)建一個(gè)文本框?qū)ο?/p>
Label password=new Label("密碼:");//創(chuàng)建一個(gè)密碼標(biāo)簽
TextField t2=new TextField();
Button b1=new Button("登陸");//創(chuàng)建登陸按鈕
Button b2=new Button("取消");//創(chuàng)建取消按鈕
public DengLuJieMian()
{
this.setTitle("學(xué)生信息管理系統(tǒng)");//設(shè)置窗口標(biāo)題
this.setLayout(null);//設(shè)置窗口布局管理器
username.setBounds(50,40,60,20);//設(shè)置姓名標(biāo)簽的初始位置
this.add(username);// 將姓名標(biāo)簽組件添加到容器
t1.setBounds(120,40,80,20);// 設(shè)置文本框的初始位置
this.add(t1);// 將文本框組件添加到容器
password.setBounds(50,100,60,20);//密碼標(biāo)簽的初始位置
this.add(password);//將密碼標(biāo)簽組件添加到容器
t2.setBounds(120,100,80,20);//設(shè)置密碼標(biāo)簽的初始位置
this.add(t2);//將密碼標(biāo)簽組件添加到容器
b1.setBounds(50,150,60,20);//設(shè)置登陸按鈕的初始位置
this.add(b1);//將登陸按鈕組件添加到容器
b2.setBounds(120,150,60,20);//設(shè)置取消按鈕的初始位置
this.add(b2);// 將取消按鈕組件添加到容器
b1.addActionListener(this);//給登陸按鈕添加監(jiān)聽(tīng)器
b2.addActionListener(this);// 給取消按鈕添加監(jiān)聽(tīng)器
this.setVisible(true);//設(shè)置窗口的可見(jiàn)性
this.setSize(300,200);//設(shè)置窗口的大小
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});//通過(guò)內(nèi)部類重寫關(guān)閉窗體的方法
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)//處理登陸事件
{
String name=t1.getText();
String pass=t2.getText();
if(name!=nullpass.equals("000123"))//判斷語(yǔ)句
{
new StudentJieMian();
}
}
}
public static void main(String args[])//主函數(shù)
{
new DengLuJieMian();
}
}
以下方法實(shí)現(xiàn)了學(xué)生界面設(shè)計(jì)
import java.awt.*;
import java.awt.event.*;
class StudentJieMian extends Frame implements ActionListener
{
MenuBar m=new MenuBar();//創(chuàng)建菜單欄
Menu m1=new Menu("信息");//創(chuàng)建菜單“信息”
MenuItem m11=new MenuItem("插入");//創(chuàng)建“插入”的菜單項(xiàng)
MenuItem m12=new MenuItem("查詢");
Menu m2=new Menu("成績(jī)");//創(chuàng)建菜單“成績(jī)”
MenuItem m21=new MenuItem("查詢");
public StudentJieMian()
{
this.setTitle("學(xué)生界面");//設(shè)置窗口標(biāo)題
this.setLayout(new CardLayout());//設(shè)置窗口布局管理器
this.setMenuBar(m);//將菜單欄組件添加到容器
m.add(m1);//將信息菜單放入菜單欄
m.add(m2);
m1.add(m11);//將“插入”菜單項(xiàng)添加到“信息”菜單
m1.add(m12); //將“查詢”菜單項(xiàng)添加到“信息”菜單
m2.add(m21); //將“查詢”菜單項(xiàng)添加到“成績(jī)”菜單
m11.addActionListener(this); //給“插入”菜單項(xiàng)添加監(jiān)聽(tīng)器
m12.addActionListener(this); //給“查詢”菜單項(xiàng)添加監(jiān)聽(tīng)器
m21.addActionListener(this); //給“查詢”菜單項(xiàng)添加監(jiān)聽(tīng)器
this.setVisible(true); //設(shè)置窗口的可見(jiàn)性
this.setSize(300,200); //設(shè)置窗口的大小
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//關(guān)閉窗口
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==m11) //處理“添加信息”事件
{
new AddStudent();
}
if(e.getSource()==m12) //處理“查詢信息”事件
{
new SelectStudent();
}
if(e.getSource()==m21) //處理“查詢成績(jī)”事件
{
new ChengJiStudent();
}
}
public static void main(String args[])
{ new StudentJieMian(); //創(chuàng)建一個(gè)對(duì)象 }
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
public class MainFrame extends JFrame implements ActionListener{
InsertPanel ip = null;
SelectPanel sp = null;
JPanel pframe;
JButton jb1,jb2,jb3;
JMenuItem jm11,jm21,jm22,jm23,jm31,jm32,jm41,jm42;
CardLayout clayout;
public MainFrame(String s){
super(s);
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
JMenu m1 = new JMenu("系統(tǒng)");
JMenu m2 = new JMenu("基本信息");
JMenu m3 = new JMenu("成績(jī)");
JMenu m4 = new JMenu("獎(jiǎng)懲");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
jm11 = new JMenuItem("退出系統(tǒng)");
jm21 = new JMenuItem("輸入");
jm22 = new JMenuItem("查詢");
jm23 = new JMenuItem("更改");
jm31 = new JMenuItem("輸入成績(jī)");
jm32 = new JMenuItem("查詢成績(jī)");
jm41 = new JMenuItem("獎(jiǎng)勵(lì)");
jm42 = new JMenuItem("處分");
m1.add(jm11);
m2.add(jm21);
m2.add(jm22);
m2.add(jm23);
m3.add(jm31);
m3.add(jm32);
m4.add(jm41);
m4.add(jm42);
Icon i1 = new ImageIcon();
Icon i2 = new ImageIcon();
Icon i3 = new ImageIcon();
jb1 = new JButton(i1);
jb1.setToolTipText("輸入");
jb2 = new JButton(i2);
jb2.setToolTipText("查詢");
jb3 = new JButton(i3);
jb3.setToolTipText("退出");
JToolBar tb = new JToolBar("系統(tǒng)工具");
tb.add(jb1);
tb.add(jb2);
tb.add(jb3);
add(tb,BorderLayout.NORTH);
jm11.addActionListener(this);
jm21.addActionListener(this);
jm22.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
clayout = new CardLayout();
pframe = new JPanel(clayout);
add(pframe);
JPanel mainp = new JPanel(new BorderLayout());
JLabel mainl = new JLabel("學(xué)生信息管理平臺(tái)",SwingConstants.CENTER);
mainl.setFont(new Font("serif",Font.BOLD,30));
mainp.add(mainl);
pframe.add(mainp,"main");
clayout.show(pframe, "main");
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jm21 || e.getSource() == jb1){
if(ip == null){
ip= new InsertPanel();
pframe.add(ip,"insert");
}
clayout.show(pframe, "insert");
this.setTitle("輸入學(xué)生信息");
}
else if(e.getSource() == jm22 || e.getSource() == jb2){
if(sp == null){
sp= new SelectPanel();
pframe.add(sp,"select");
}
clayout.show(pframe, "select");
this.setTitle("查詢學(xué)生信息");
}
else if(e.getSource() == jm11 || e.getSource() == jb3){
System.exit(0);
}
}
}
第二個(gè):
import javax.swing.JFrame;
public class MainTest {
public static void main(String [] args){
MainFrame f = new MainFrame("學(xué)生信息管理平臺(tái)");
f.setSize(400,300);
f.setLocation(350,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
第二個(gè):
import java.sql.Connection;
import java.sql.DriverManager;
public class MySQLConnection {
static Connection getCon(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123");
}
catch(Exception e){
System.out.println("建立數(shù)據(jù)庫(kù)連接遇到異常!");
}
return con;
}
}
第四個(gè):
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class SelectPanel extends JPanel implements ActionListener{
JButton jb;
JTextField jt;
JTextField jt1,jt2,jt3,jt4;
public SelectPanel(){
JLabel jl = new JLabel("請(qǐng)輸入學(xué)號(hào):",SwingConstants.CENTER);
jt = new JTextField();
jb = new JButton("確定");
JPanel jp1 = new JPanel(new GridLayout(1,3));
jp1.add(jl);
jp1.add(jt);
jp1.add(jb);
JLabel j1,j2,j3,j4;
j1 = new JLabel("學(xué)號(hào):",SwingConstants.CENTER);
j2 = new JLabel("姓名:",SwingConstants.CENTER);
j3 = new JLabel("性別:",SwingConstants.CENTER);
j4 = new JLabel("年齡:",SwingConstants.CENTER);
jt1 = new JTextField(6);
jt1.setEditable(false);
jt2 = new JTextField(6);
jt2.setEditable(false);
jt3 = new JTextField(6);
jt3.setEditable(false);
jt4 = new JTextField(6);
jt4.setEditable(false);
JPanel jp2 = new JPanel(new BorderLayout());
JPanel jp3 = new JPanel(new GridLayout(4,2));
jp2.add(new JLabel(""),BorderLayout.NORTH);
jp3.add(j1);
jp3.add(jt1);
jp3.add(j2);
jp3.add(jt2);
jp3.add(j3);
jp3.add(jt3);
jp3.add(j4);
jp3.add(jt4);
jp2.add(jp3);
this.setLayout(new BorderLayout());
add(jp1,BorderLayout.NORTH);
add(jp2);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jb){
String stuNo = jt.getText().trim();
Student s = new Student();
boolean b = true;
try{
b = s.selectByStuNo(stuNo);
}
catch(Exception ex){
System.out.println("查詢學(xué)生信息遇到異常!");
}
if(b){
jt1.setText(s.getStuNo());
jt2.setText(s.getName());
jt3.setText(s.getGender());
int a = s.getAge();
Integer i = new Integer(a);
jt4.setText(i.toString());
}
else{
JOptionPane.showMessageDialog(null, "無(wú)此學(xué)生!");
}
}
}
}
第五個(gè):
import javax.swing.JFrame;
public class SelectTest {
public static void main(String [] args){
JFrame f = new JFrame("查詢學(xué)生信息");
SelectPanel p = new SelectPanel();
f.add(p);
f.setSize(400,300);
f.setLocation(300,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
第六個(gè):
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Student {
String stuNo;
String name;
String gender;
int age;
public Student(){}
public Student(String stuNo,String name,String gender, int age){
this.stuNo = stuNo;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender = gender;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public boolean insertStudent(){
boolean b = true;
try{
Connection con = MySQLConnection.getCon();
Statement statement = con.createStatement();
String sql = "insert into student values('" + stuNo + "','" + name +"','" + gender + "'," + age + ")";
sql = new String(sql.getBytes("gb2312"),"ISO8859_1");
statement.executeUpdate(sql);
con.close();
}
catch(Exception e){
b = false;
System.out.println("插入數(shù)據(jù)庫(kù)遇到異常!");
}
return b;
}
public boolean selectByStuNo(String stuNo)throws Exception{
boolean b = true;
Connection con = MySQLConnection.getCon();
Statement statement = con.createStatement();
String sql = "select * from student where stuNo =" + stuNo;
ResultSet rs = statement.executeQuery(sql);
if(rs != null rs.next()){
String no = rs.getString(1);
this.setStuNo(no);
String n = rs.getString(2);
n = new String(n.getBytes("ISO8859_1"),"gb2312");
this.setName(n);
String g = rs.getString(3);
g = new String (g.getBytes("ISO8859_1"),"gb2312");
this.setGender(g);
this.setAge(rs.getInt(4));
b = true;
}
rs.close();
statement.close();
con.close();
return b;
}
}
數(shù)據(jù)庫(kù)你自己弄吧,我沒(méi)時(shí)間弄了!初學(xué)得多動(dòng)手哦
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.Comparator;
import?java.util.List;
public?class?Sort?{
public?static?void?main(String[]?args)?{
Student?p1?=?new?Student(1001,?"小明",?20);
Student?p2?=?new?Student(1002,?"小紅",?21);
Student?p3?=?new?Student(1003,?"小黑",?19);
ListStudent?list?=?new?ArrayListStudent();
list.add(p1);
list.add(p2);
list.add(p3);
Collections.sort(list,?new?ComparatorStudent()?{
/*
?*?int?compare(Student?o1,?Student?o2)?返回一個(gè)基本類型的整型,?返回負(fù)數(shù)表示:o1?小于o2,
?*?返回0?表示:o1和o2相等,?返回正數(shù)表示:o1大于o2。
?*/
public?int?compare(Student?o1,?Student?o2)?{
//?按照學(xué)生的學(xué)號(hào)進(jìn)行升序排列
if?(o1.getId()??o2.getId())?{
return?1;
}
if?(o1.getId()?==?o2.getId())?{
return?0;
}
return?-1;
}
});
write(list);
System.out.println("---------------------");
Collections.sort(list,?new?ComparatorStudent()?{
/*
?*?int?compare(Student?o1,?Student?o2)?返回一個(gè)基本類型的整型,?返回負(fù)數(shù)表示:o1?小于o2,
?*?返回0?表示:o1和o2相等,?返回正數(shù)表示:o1大于o2。
?*/
public?int?compare(Student?o1,?Student?o2)?{
//?按照學(xué)生的年齡進(jìn)行升序排列
if?(o1.getAge()??o2.getAge())?{
return?1;
}
if?(o1.getAge()?==?o2.getAge())?{
return?0;
}
return?-1;
}
});
write(list);
}
public?static?void?write(ListStudent?list)?{
for?(Student?s?:?list)?{
System.out.println(s.getId()?+?"\t"?+?s.getName()?+?"\t"
+?s.getAge());
}
}
}
public?class?Student?{
private?int?id?;
private?String?name;
private?int?age;
//構(gòu)造方法
public?Student(int?id,String?name,int?age){
this.id?=?id;
this.name?=?name;
this.age?=?age;
}
public?int?getId()?{
return?id;
}
public?void?setId(int?id)?{
this.id?=?id;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getAge()?{
return?age;
}
public?void?setAge(int?age)?{
this.age?=?age;
}
}
用java寫的話,可以用List來(lái)實(shí)現(xiàn)學(xué)生管理系統(tǒng):\x0d\x0a首先,管理系統(tǒng)是針對(duì)學(xué)生對(duì)象的,所以我們先把學(xué)生對(duì)象就寫出來(lái):\x0d\x0apackage bean;\x0d\x0apublic class Student {\x0d\x0a String name;\x0d\x0a String studentId;\x0d\x0a String sex;\x0d\x0a int grade;\x0d\x0a public Student(String name,String studentId,String sex,int grade){\x0d\x0a this.name= name;\x0d\x0a this.studentId= studentId;\x0d\x0a this.sex = sex;\x0d\x0a this.grade = grade; \x0d\x0a }\x0d\x0a public int getGrade(){\x0d\x0a return grade;\x0d\x0a }\x0d\x0a public String getName(){\x0d\x0a return name;\x0d\x0a }\x0d\x0a public String getSex(){\x0d\x0a return sex;\x0d\x0a }\x0d\x0a public void setGrade(int g){\x0d\x0a this.grade = g;\x0d\x0a }\x0d\x0a public String getStudentId(){\x0d\x0a return studentId;\x0d\x0a }\x0d\x0a}\x0d\x0a這里面定義了一些得到當(dāng)前學(xué)生對(duì)象數(shù)據(jù)的一些get方法,和成績(jī)修改的set方法,代碼很簡(jiǎn)單,就不做詳細(xì)的解答。\x0d\x0a就下來(lái)就是我們的正文了。\x0d\x0a雖然我們暫時(shí)不用swing來(lái)做界面,但是總得要看的過(guò)去吧,所以,先做了一個(gè)比較簡(jiǎn)單的界面:\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("*歡迎來(lái)到學(xué)生管理系統(tǒng) *");\x0d\x0a System.out.println("*1:增加學(xué)生 *");\x0d\x0a System.out.println("*2:刪除學(xué)生 *");\x0d\x0a System.out.println("*3:修改成績(jī) *");\x0d\x0a System.out.println("*4:查詢成績(jī) *");\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("您想選擇的操作是:");\x0d\x0a這里可以看到,我們的是用一個(gè)1234來(lái)選擇項(xiàng)目,說(shuō)以不得不講一下Java如何獲取到鍵盤所輸入的數(shù)據(jù)---------Scanner ,要使用這個(gè),首先需要import進(jìn)來(lái)一個(gè)包:\x0d\x0a例如這里:\x0d\x0aimport java.util.*;\x0d\x0a之后的兩行代碼搞定輸入:\x0d\x0aScanner sc = new Scanner(System.in);\x0d\x0a int choice = sc.nextInt();\x0d\x0a接下來(lái)就是各個(gè)功能的實(shí)現(xiàn):\x0d\x0a\x0d\x0apackage test;\x0d\x0aimport java.util.*;\x0d\x0aimport bean.Student;\x0d\x0apublic class Manager {\x0d\x0a static List StudentList = new LinkedList();\x0d\x0a public static void main(String[] agrs){\x0d\x0a select(StudentList); \x0d\x0a }\x0d\x0a private static void select(List StudentList ){\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("*歡迎來(lái)到學(xué)生管理系統(tǒng) *");\x0d\x0a System.out.println("*1:增加學(xué)生 *");\x0d\x0a System.out.println("*2:刪除學(xué)生 *");\x0d\x0a System.out.println("*3:修改成績(jī) *");\x0d\x0a System.out.println("*4:查詢成績(jī) *");\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("您想選擇的操作是:");\x0d\x0a Scanner sc = new Scanner(System.in);\x0d\x0a int choice = sc.nextInt(); \x0d\x0a switch(choice){\x0d\x0a //增加學(xué)生\x0d\x0a case 1:\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的姓名:");\x0d\x0a Scanner Sname = new Scanner(System.in);\x0d\x0a String name = Sname.nextLine();\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的性別:");\x0d\x0a Scanner Ssex = new Scanner(System.in);\x0d\x0a String sex = Ssex.nextLine();\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的學(xué)號(hào):");\x0d\x0a Scanner SId = new Scanner(System.in);\x0d\x0a String studentId = SId.nextLine();\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的成績(jī):");\x0d\x0a Scanner Sgrade = new Scanner(System.in);\x0d\x0a int grade = Sgrade.nextInt();\x0d\x0a StudentList.add(new Student(name,studentId,sex,grade));\x0d\x0a System.out.println("添加成功!?。。?!");\x0d\x0a select(StudentList);\x0d\x0a break;\x0d\x0a //刪除學(xué)生成績(jī)\x0d\x0a case 2:\x0d\x0a System.out.print("請(qǐng)告訴我需要?jiǎng)h除學(xué)生的學(xué)號(hào):");\x0d\x0a Scanner Sid = new Scanner(System.in);\x0d\x0a String SstudentId = Sid.nextLine();\x0d\x0a boolean isfindDelete = false;\x0d\x0a for (int i = 0; i
回答于?2022-11-16