import javax.swing.*; import java.awt.*; import java.awt.event.*; public class lianliankan implements ActionListener { pan.baidu.com/share/link?shareid=925645354&uk=1900108346 pan.baidu.com/share/link?shareid=926169270&uk=1900108346 pan.baidu.com/share/link?shareid=926687816&uk=1900108346 pan.baidu.com/share/link?shareid=927205796&uk=1900108346 pan.baidu.com/share/link?shareid=927734865&uk=1900108346 pan.baidu.com/share/link?shareid=928250932&uk=1900108346 pan.baidu.com/share/link?shareid=928772995&uk=1900108346 pan.baidu.com/share/link?shareid=929290281&uk=1900108346 JFrame mainFrame; //主面板 Container thisContainer; JPanel centerPanel,southPanel,northPanel; //子面板 JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组 JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮 JLabel fractionLable=new JLabel("0"); //分数标签 JButton firstButton,secondButton; //分别记录两次被选中的按钮 int grid[][] = new int[8][7];//储存游戏按钮位置 static boolean pressInformation=false; //判断是否有按钮被选中 int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标 int i,j,k,n;//消除方法控制 public void init(){ mainFrame=new JFrame("JKJ连连看"); thisContainer = mainFrame.getContentPane(); thisContainer.setLayout(new BorderLayout()); centerPanel=new JPanel(); southPanel=new JPanel(); northPanel=new JPanel(); thisContainer.add(centerPanel,"Center"); thisContainer.add(southPanel,"South"); thisContainer.add(northPanel,"North"); centerPanel.setLayout(new GridLayout(6,5)); for(int cols = 0;cols < 6;cols++){ for(int rows = 0;rows < 5;rows++ ){ diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1])); diamondsButton[cols][rows].addActionListener(this); centerPanel.add(diamondsButton[cols][rows]); } } pan.baidu.com/share/link?shareid=929807256&uk=1900108346 pan.baidu.com/share/link?shareid=930333409&uk=1900108346 pan.baidu.com/share/link?shareid=955147097&uk=1900108346 pan.baidu.com/share/link?shareid=943596053&uk=1900108346 pan.baidu.com/share/link?shareid=944094515&uk=1900108346 pan.baidu.com/share/link?shareid=952688439&uk=1900108346 pan.baidu.com/share/link?shareid=945151103&uk=1900108346 exitButton=new JButton("退出"); exitButton.addActionListener(this); resetButton=new JButton("重列"); resetButton.addActionListener(this); newlyButton=new JButton("再来一局"); newlyButton.addActionListener(this); southPanel.add(exitButton); southPanel.add(resetButton); southPanel.add(newlyButton); fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()))); northPanel.add(fractionLable); mainFrame.setBounds(280,100,500,450); mainFrame.setVisible(true); } pan.baidu.com/share/link?shareid=945670579&uk=1900108346 pan.baidu.com/share/link?shareid=946190072&uk=1900108346 pan.baidu.com/share/link?shareid=953208346&uk=1900108346 pan.baidu.com/share/link?shareid=947259211&uk=1900108346 pan.baidu.com/share/link?shareid=947859305&uk=1900108346 pan.baidu.com/share/link?shareid=980118417&uk=725681721 pan.baidu.com/share/link?shareid=980663775&uk=725681721 public void randomBuild() { int randoms,cols,rows; for(int twins=1;twins<=15;twins++) { randoms=(int)(Math.random()*25+1); for(int alike=1;alike<=2;alike++) { cols=(int)(Math.random()*6+1); rows=(int)(Math.random()*5+1); while(grid[cols][rows]!=0) { cols=(int)(Math.random()*6+1); rows=(int)(Math.random()*5+1); } this.grid[cols][rows]=randoms; } } } pan.baidu.com/share/link?shareid=992363762&uk=725681721 pan.baidu.com/share/link?shareid=981772300&uk=725681721 pan.baidu.com/share/link?shareid=982306258&uk=725681721 pan.baidu.com/share/link?shareid=982806192&uk=725681721 pan.baidu.com/share/link?shareid=983306193&uk=725681721 pan.baidu.com/share/link?shareid=983846901&uk=725681721 pan.baidu.com/share/link?shareid=984340977&uk=725681721 pan.baidu.com/share/link?shareid=1006542659&uk=725681721 public void fraction(){ fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100)); } public void reload() { int save[] = new int[30]; int n=0,cols,rows; int grid[][]= new int[8][7]; for(int i=0;i<=6;i++) { for(int j=0;j<=5;j++) { if(this.grid[i][j]!=0) { save[n]=this.grid[i][j]; n++; } } } n=n-1; this.grid=grid; while(n>=0) { cols=(int)(Math.random()*6+1); rows=(int)(Math.random()*5+1); while(grid[cols][rows]!=0) { cols=(int)(Math.random()*6+1); rows=(int)(Math.random()*5+1); } this.grid[cols][rows]=save[n]; n--; } mainFrame.setVisible(false); pressInformation=false; //这里一定要将按钮点击信息归为初始 init(); for(int i = 0;i < 6;i++){ for(int j = 0;j < 5;j++ ){ if(grid[i+1][j+1]==0) diamondsButton[i][j].setVisible(false); } } } * JAVA小游戏-拼图 我做的第一个小游戏 * Cell类是继承的按钮类,并加上相应图形,形成方格 *MyCanvas是一个面板,加载Cell类的对象(方格),是这三个类中的核心 */ import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Choice; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Icon; import javax.swing.JButton; public class MyMainFrame extends JFrame implements ActionListener { MyCanvas myCanvas; JPanel panelNorth,panelPreview;//定义上方的面板,及预览所需的面板 Button start,preview,set;//定义开始,预览,设定按钮 Container container;//容器,得到内容面板 public MyMainFrame() {//初使化 container=this.getContentPane(); start=new Button("开始"); start.addActionListener(this); preview=new Button("预览"); preview.addActionListener(this); set = new Button("设置"); set.addActionListener(this); panelPreview=new JPanel(); panelPreview.setLayout(null); Icon icon=new ImageIcon("pic_"+MyCanvas.pictureID+".jpg"); JLabel label=new JLabel(icon); label.setBounds(0,0,300,300); panelPreview.add(label); pan.baidu.com/share/link?shareid=1007081564&uk=725681721 pan.baidu.com/share/link?shareid=1014818667&uk=725681721 pan.baidu.com/share/link?shareid=1008171037&uk=725681721 pan.baidu.com/share/link?shareid=1008677489&uk=725681721 pan.baidu.com/share/link?shareid=1009218690&uk=725681721 pan.baidu.com/share/link?shareid=1009736810&uk=725681721 pan.baidu.com/share/link?shareid=1010258773&uk=725681721 panelNorth=new JPanel(); panelNorth.setBackground(Color.red); panelNorth.add(start); panelNorth.add(preview); panelNorth.add(set); myCanvas=new MyCanvas(); container.add(myCanvas,BorderLayout.CENTER); container.add(panelNorth,BorderLayout.NORTH); this.setTitle("拼图小游戏-明"); this.setLocation(300,200); this.setSize(308,365); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(3); } public static void main(String[] args) { // TODO 自动生成方法存根 new MyMainFrame(); } public void actionPerformed(ActionEvent arg0) {//对三个按钮事件的处理 // TODO 自动生成方法存根 Button button=(Button)arg0.getSource(); if(button==start){ myCanvas.Start(); }else if(button==preview){ if(button.getLabel()=="预览"){ container.remove(myCanvas); container.add(panelPreview); panelPreview.updateUI(); container.repaint(); button.setLabel("返回"); }else{ container.remove(panelPreview); container.add(myCanvas); container.repaint(); button.setLabel("预览"); }pan.baidu.com/share/link?shareid=1010788596&uk=725681721 pan.baidu.com/share/link?shareid=1035653632&uk=2336405278 pan.baidu.com/share/link?shareid=1036170864&uk=2336405278 pan.baidu.com/share/link?shareid=1036700435&uk=2336405278 pan.baidu.com/share/link?shareid=1037232617&uk=2336405278 }else if(button==set){//修改所选图片 Choice pic = new Choice(); pic.add("小猫"); pic.add("小猪"); pic.add("云"); pic.add("QQ"); pic.add("卡通"); pic.add("花"); int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION); if(i==JOptionPane.YES_OPTION){ MyCanvas.pictureID=pic.getSelectedIndex()+1; myCanvas.reLoadPictrue(); Icon icon=new ImageIcon("pic_"+MyCanvas.pictureID+".jpg"); JLabel label=new JLabel(icon); label.setBounds(0,0,300,300); panelPreview.removeAll(); panelPreview.add(label); panelPreview.repaint(); } } } }pan.baidu.com/share/link?shareid=1037781125&uk=2336405278 pan.baidu.com/share/link?shareid=1038310282&uk=2336405278 pan.baidu.com/share/link?shareid=1038833689&uk=2336405278 class MyCanvas extends JPanel implements MouseListener { boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件,FALSE是尚未添加动作事件 Cell cell[];//定义方格 Rectangle cellNull;//定义空方格区域 public static int pictureID=1;//当前选择的图片代号 public MyCanvas() { this.setLayout(null); this.setSize(400,400); cellNull=new Rectangle(200,200,100,100);//空方格区域在第三行每三列 cell=new Cell[9]; Icon icon; for (int i = 0; i < 3; i++) {//为9个方格加载图片,并初使化坐标,形成三行三列 for(int j=0;j<3;j++){ icon=new ImageIcon("pic_"+pictureID+"_"+(i*3+j+1)+".jpg"); cell[i*3+j]=new Cell(icon); cell[i*3+j].setLocation(j*100,i*100); this.add(cell[i*3+j]); } }pan.baidu.com/share/link?shareid=1039362572&uk=2336405278 pan.baidu.com/share/link?shareid=1039904016&uk=2336405278 pan.baidu.com/share/link?shareid=1040431819&uk=2336405278 this.remove(cell[8]);//移除最后一个多余的方格 } import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener { String[] columnNames = new String[] { "姓名", "学期", "课程ID", "成绩" }; DefaultTableModel dtmScore = new DefaultTableModel(columnNames, 57); JTable jtAddScore = new JTable(dtmScore); JScrollPane jspScore = new JScrollPane(jtAddScore); JComboBox jcbCourse = new JComboBox(); JComboBox jcbClassName = new JComboBox(); JComboBox jcbTerm = new JComboBox(); Connection con = JDBCUtil.getConnection(); // Connection con2 = JdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData meta = null; String sql = null; int cols = 0; int rowCount = 0; public AddScore() { CreateAddScoreGUI(); } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理 searchScore(); } else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理 tryCommit(); } else if ("退出".equals(str)) { this.setVisible(false); } } public void tryCommit() { if (cols < 1) return; sql = "insert into scorexx values(?,?,?,?)"; try { ps = con.prepareStatement(sql); String[] value = new String[4]; for (int k = 0; k < rowCount; k++) { for (int i = 1; i <= cols + 1; i++) { // 如果成绩为空,取消提交操作,并给出提示。 if (jtAddScore.getValueAt(k, i - 1) != null) { value[i - 1] = jtAddScore.getValueAt(k, i - 1) .toString(); } else { if (i == 4) JOptionPane .showMessageDialog(null, "无效成绩输入!成绩不能为空"); return; } // 如果成绩字段里面不是数字,则取消本次操作?? // String str = "^[0-9]{1}[0-9]?[0-9]?$"; // if (i == 4) { // if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){ // if( // Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100) // {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100"); // return;} // } // } System.out.print(value[i - 1]); } ps.setString(1, value[0]); ps.setString(2, value[1]); ps.setString(3, value[2]); ps.setInt(4, Integer.parseInt(value[3])); ps.executeUpdate(); con.commit(); System.out.println(); } JOptionPane .showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!"); } catch (Exception ew) { ew.printStackTrace(); } } // 自动添加课程下拉列表中的课程 public void addCourseToJCheckBoxCourse() { String courseName = null; sql = "select cname from coursexx"; try { ps = con.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData meta = rs.getMetaData(); int cols = meta.getColumnCount(); while (rs.next()) { for (int i = 1; i <= cols; i++) { courseName = rs.getString(i); System.out.println(courseName); jcbCourse.addItem(courseName); rowCount++; } } con.commit(); } catch (Exception ers) { ers.printStackTrace(); try { con.rollback(); } catch (Exception er) { er.printStackTrace(); } } } public void searchScore() { // 每次查询之前清空之前的结果 for (int k = 0; k < 50; k++) { for (int i = 0; i < cols; i++) { jtAddScore.setValueAt(null, k, i); } } // Method1:JDBC Method2:Hibernate String cName = jcbCourse.getSelectedItem().toString().trim(); sql = "select x.sname,k.term,k.cno from " + "choicesxx x join Coursexx k " + "on x.cno=k.cno and k.cname=?"; try { ps = con.prepareStatement(sql); ps.setString(1, cName); rs = ps.executeQuery(); meta = rs.getMetaData(); cols = meta.getColumnCount(); boolean flag = true; String result = null; int k = 1; while (rs.next()) { for (int i = 1; i <= cols; i++) { result = rs.getString(i); jtAddScore.setValueAt(result, k - 1, i - 1); } rowCount = k++; } } catch (Exception exx) { exx.printStackTrace(); } } //产生用户界面 public void CreateAddScoreGUI() { this.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:"); jcbCourse.addActionListener(this); jcbCourse.addItem(new String("请选择课程")); addCourseToJCheckBoxCourse(); this.add(jlCourse); this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:"); // jcbClassName.addActionListener(this); // jcbClassName.addItem(new String("请选择班级")); // addClassNameToJCheckBoxClassName(); // this.add(jlClassName); // this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:"); // jcbTerm.addActionListener(this); // jcbTerm.addItem(new String("请选择学期")); // addTermToJCheckBoxTerm(); // this.add(jlTerm); // this.add(jcbTerm); JButton jbSearch = new JButton("查询"); jbSearch.addActionListener(this); JButton jbOK = new JButton("提交"); jbOK.addActionListener(this); JButton jbExit = new JButton("退出"); jbExit.addActionListener(this); this.add(jbSearch); this.add(jbOK); this.add(jbExit); this.add(jspScore); this.setVisible(true); this.setSize(500, 600); } public static void main(String[] args) { new AddScore(); } } import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class StudentChooseCourse extends JFrame implements ActionListener { JTextField jtfSearch = new JTextField(11); String[] columnNames = new String[] { "课程ID", "课程名", "学时", "学分", "学期", "性质" }; DefaultTableModel dtmSearch = new DefaultTableModel(columnNames, 27); JTable jtSearch = new JTable(dtmSearch); JScrollPane jspSearch = new JScrollPane(jtSearch); JComboBox jcbSearch = new JComboBox(); JTextField jtfSelectedCourse = new JTextField(10); Connection con = null; PreparedStatement ps = null; ResultSet rs = null; private static String usrName; private static String passwrd; //在构造方法中添加两个参数。以便在"提交"时,将学生的身份连同所选的课程,一同记录在学生选课表中。 public StudentChooseCourse(String usrName, String passwrd) { createSearchCourse(); this.usrName = usrName; this.passwrd = passwrd; } public String getUsrName() { return usrName; } public void setUsrName(String usrName) { this.usrName = usrName; } public String getPasswrd() { return passwrd; } public void setPasswrd(String passwrd) { this.passwrd = passwrd; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class StudentChooseCourse extends JFrame implements ActionListener { JTextField jtfSearch = new JTextField(11); String[] columnNames = new String[] { "课程ID", "课程名", "学时", "学分", "学期", "性质" }; DefaultTableModel dtmSearch = new DefaultTableModel(columnNames, 27); JTable jtSearch = new JTable(dtmSearch); JScrollPane jspSearch = new JScrollPane(jtSearch); JComboBox jcbSearch = new JComboBox(); JTextField jtfSelectedCourse = new JTextField(10); Connection con = null; PreparedStatement ps = null; ResultSet rs = null; private static String usrName; private static String passwrd; //在构造方法中添加两个参数。以便在"提交"时,将学生的身份连同所选的课程,一同记录在学生选课表中。 public StudentChooseCourse(String usrName, String passwrd) { createSearchCourse(); this.usrName = usrName; this.passwrd = passwrd; } public String getUsrName() { return usrName; } public void setUsrName(String usrName) { this.usrName = usrName; } public String getPasswrd() { return passwrd; } public void setPasswrd(String passwrd) { this.passwrd = passwrd; } } package day1; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import javax.swing.*; public class userRegister implements ActionListener { ArrayList<User> list = new ArrayList<User>(); JTextField jtf1 = new JTextField(14); JTextField jtf2 = new JTextField(14); JTextField jtf3 = new JTextField(14); JTextField jtf4 = new JTextField(14); JFrame jf; public userRegister() throws Exception { createGUI(); } //当事件发生时,自动调用actionPerformed()方法 public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if ("注册".equals(str)) { User user = new User(); user.setUserName(jtf1.getText()); user.setPassWord(jtf2.getText()); user.setEmail(jtf4.getText()); String rePassWord = jtf3.getText(); if (!(user.getPassWord().equalsIgnoreCase(rePassWord))) { jtf2.setText("密码输入错误"); jtf3.setText("密码输入错误"); } else { jf.setTitle("注册成功" + "欢迎您" + user.getUserName()); jtf2.setText("******"); jtf3.setText("******"); list.add(user); } } else if ("登录".equals(str)) { try { readFromFile(); } catch (Exception e1) { e1.printStackTrace();}}} //createGui()方法,用于生成图形用户界面 public void createGUI() { jf = new JFrame("用户注册"); //当用户关闭窗口时,多个用户信息自动保存到本地文件系统(通过调用writeToFile()方法) jf.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { try { writeToFile(); } catch (IOException e1) { e1.printStackTrace(); } System.exit(0); } }); jf.setLayout(new GridLayout(4, 2)); JPanel jp1 = new JPanel(); jf.add(jp1); JLabel jl1 = new JLabel("User Name:"); jp1.add(jl1);jp1.add(jtf1); JPanel jp2 = new JPanel(); jf.add(jp2); JLabel jl2 = new JLabel("Pass Word:"); jp2.add(jl2);jp2.add(jtf2); JPanel jp3 = new JPanel(); jf.add(jp3); JLabel jl3 = new JLabel("ReInputPwd:"); jp3.add(jl3); jp3.add(jtf3); JPanel jp4 = new JPanel(); jf.add(jp4); JLabel jl4 = new JLabel("E-mail:"); jp4.add(jl4);jp4.add(jtf4); JPanel jp5 = new JPanel(); jf.add(jp5); JButton jb51 = new JButton("注册"); jb51.addActionListener(this); JButton jb52 = new JButton("登录"); jb52.addActionListener(this); jp5.add(jb51);jp5.add(jb52); jf.setSize(500, 300); jf.setVisible(true); } //readFromFile()方法用于从文件中读取用户对象信息,并将用户对象添加进线性表(list) public void readFromFile() throws IOException, Exception { FileInputStream fis = new FileInputStream(new File( "d:\\userRegister.dat")); ObjectInputStream ois = new ObjectInputStream(fis); User read = null; try { while (true) { read = (User) ois.readObject(); if (read == null) { break; } list.add(read); } } catch (Exception e) { } // System.out.println(list); User temp = new User(jtf1.getText(), jtf2.getText(), "q"); match(temp); ois.close(); } //writeToFile()方法,实现用户信息保存功能。 public void writeToFile() throws IOException { File file = new File("d:\\userRegister.dat"); file.createNewFile(); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( file)); for (User usr : list) { oos.writeObject(usr); } oos.flush(); oos.close(); } //当用户试图登录时,match()方法可以将用户输入与保存的信息,进行匹配。 //如果用户名和密码正确,登录成功;否则,登录失败。 public void match(User user) { boolean match = false; for (User usr : list) { if (usr.getUserName().equals(user.getUserName()) && usr.getPassWord().equals(user.getPassWord())) { jf.setTitle("登录成功"); match = true; break; } } if (!match) { jf.setTitle("用户名或密码错误,请重新输入!"); jtf1.setText(""); jtf2.setText(""); jtf3.setText(""); jtf4.setText(""); jtf1.requestFocus(); } } public static void main(String[] args) throws Exception { new userRegister(); } } //------------------------------------------------------------// User类是个用户信息类,包括userName,passWord,email等属性。 public class User implements java.io.Serializable { private String userName; private String passWord; private String email; public User() { } public User(String userName, String passWord, String email) { super(); this.setUserName(userName); this.setPassWord(passWord); this.setEmail(email); } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String toString(){ return userName+":"+passWord+":"+email; } } Listener { Connection con; Statement stmt; String sql; ResultSet rs; StringBuffer sb = new StringBuffer(); JTextField jtf2 = new JTextField("张飞",10); JTextField jtf3 = new JTextField(10); JTextField jtf4 = new JTextField(10); JTextField jtf7 = new JTextField(10); JTextField jtf5 = new JTextField(10); JTextField jtf6 = new JTextField(10); JTextField jtf8 = new JTextField(10); JTextField jtf9 = new JTextField(10); JTextArea jta10 = new JTextArea(50, 40); public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp() searchEmp(); } else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp() addEmp(); } else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp() alterEmp(); } else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp() deleteEmp(); } } //this is the constructor. EmpManageSys() { createGUI(); connectToDataBase(); // searchEmp(); // addEmp(); // deleteEmp(); alterEmp(); } //connect to the database by using the method getConnection from the class of JdbcUtil. //You can find the class of JdbcUtil at the button of this page. public Connection connectToDataBase() { con = JdbcUtil.getConnection(); System.out.println(con); return con; } public void searchEmp() { jta10.setText(""); try { stmt = con.createStatement(); // sql = "select * from Mstar where id=" + jtf3.getText(); // sql1="select * from sd100343 where ="+jtf3.getText(); sql = "select * from Mstar where chineseName=" + jtf2.getText().toLowerCase().trim() + "or id=" + jtf3.getText().toLowerCase().trim() + "or engName=" + jtf4.getText().toLowerCase().trim() + "or UNIT=" + jtf5.getText().toLowerCase().trim() + "or TEAM=" + jtf6.getText().toLowerCase().trim() + "or Phone=" + jtf7.getText().toLowerCase().trim() + "or region=" + jtf8.getText().toLowerCase().trim() + "or busStation=" + jtf9.getText().toLowerCase().trim(); stmt.executeQuery(sql); rs = stmt.getResultSet(); ResultSetMetaData meta = rs.getMetaData(); int cols = meta.getColumnCount(); while (rs.next()) { for (int i = 1; i <= cols; i++) { sb.append(" " + meta.getColumnName(i) + " ="); sb.append(rs.getString(i)); } sb.append("\n"); jta10.setText(sb.toString()); } } catch (SQLException e11) { e11.printStackTrace(); } } public void addEmp() { try { stmt = con.createStatement(); sql = "update Mstar values(" + jtf2.getText() + jtf3.getText() + jtf4.getText() + jtf5.getText() + jtf6.getText() + jtf7.getText() + jtf8.getText() + jtf9.getText() + ")"; int i = stmt.getUpdateCount(); if ((jtf2.getText() != null) && (jtf4.getText() != null) && (jtf6.getText() != null) && (jtf7.getText() != null)) { stmt.executeUpdate(sql); jta10.setText("添加记录成功" + i + "条"); } else { jta10.setText("带*号项为添加记录时不能为空"); } } catch (SQLException e1) { e1.printStackTrace(); } } public void deleteEmp() { searchEmp(); try { stmt = con.createStatement(); sql = "delete from Mstar where chineseName=" + jtf2.getText().toLowerCase().trim() + "or id=" + jtf3.getText().toLowerCase().trim() + "or engName=" + jtf4.getText().toLowerCase().trim() + "or UNIT=" + jtf5.getText().toLowerCase().trim() + "or TEAM=" + jtf6.getText().toLowerCase().trim() + "or Phone=" + jtf7.getText().toLowerCase().trim() + "or region=" + jtf8.getText().toLowerCase().trim() + "or busStation=" + jtf9.getText().toLowerCase().trim(); stmt.executeUpdate(sql); int i = stmt.getUpdateCount(); jta10.setText("撤除操作成功" + i + "条"); } catch (SQLException e) { e.printStackTrace(); } } public void alterEmp() { searchEmp(); sql = "update Mstar set chineseName=" + jtf2.getText().toLowerCase().trim() + "and id=" + jtf3.getText().toLowerCase().trim() + "and engName=" + jtf4.getText().toLowerCase().trim() + "and UNIT=" + jtf5.getText().toLowerCase().trim() + "and TEAM=" + jtf6.getText().toLowerCase().trim() + "and Phone=" + jtf7.getText().toLowerCase().trim() + "and region=" + jtf8.getText().toLowerCase().trim() + "and busStation=" + jtf9.getText().toLowerCase().trim(); int i = 0; try { stmt.executeUpdate(sql); i = stmt.getUpdateCount(); } catch (SQLException e) { e.printStackTrace(); } jta10.setText("修改操作成功" + i + "条"); } public void createGUI() { JFrame jf = new JFrame("员工信息管理系统"); jf.setLayout(new GridLayout(2, 1)); // jf.setLayout(new GridLayout(10,2)); JPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel(); JButton jb11 = new JButton("查询"); jb11.addActionListener(this); JButton jb12 = new JButton("增加"); jb12.addActionListener(this); jp1.add(jb11); jp1.add(jb12); jp00.add(jp1); JPanel jp11 = new JPanel(); JButton jb111 = new JButton("修改"); jb111.addActionListener(this); JButton jb112 = new JButton("撤除"); jb112.addActionListener(this); jp11.add(jb111); jp11.add(jb112); jp00.add(jp11); JPanel jp2 = new JPanel(); JLabel jl2 = new JLabel("中 文 名 * "); jp2.add(jl2); jp2.add(jtf2); jp00.add(jp2); JPanel jp3 = new JPanel(); JLabel jl3 = new JLabel("工 号 "); jp3.add(jl3); jp3.add(jtf3); jp00.add(jp3); JPanel jp4 = new JPanel(); JLabel jl4 = new JLabel("EngName*"); jp4.add(jl4); jp4.add(jtf4); jp00.add(jp4); JPanel jp5 = new JPanel(); JLabel jl5 = new JLabel("UNIT "); jp5.add(jl5); jp5.add(jtf5); jp00.add(jp5); JPanel jp6 = new JPanel(); JLabel jl6 = new JLabel("TEAM* "); jp6.add(jl6); jp6.add(jtf6); jp00.add(jp6); JPanel jp7 = new JPanel(); JLabel jl7 = new JLabel("Phone* "); jp7.add(jl7); jp7.add(jtf7); jp00.add(jp7); JPanel jp8 = new JPanel(); JLabel jl8 = new JLabel("区域 "); jp8.add(jl8); jp8.add(jtf8); jp00.add(jp8); JPanel jp9 = new JPanel(); JLabel jl9 = new JLabel("公交站 "); jp9.add(jl9); jp9.add(jtf9); jp00.add(jp9); jf.add(jp00); JPanel jp01 = new JPanel(); jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作"); jp01.add(jta10); jf.add(jp01); jf.setSize(700, 500); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public static void main(String[] args) { new EmpManageSys(); } } import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; /** * 此类用于取出本机的IP地址 * * @author http://www.javaweb.cc * @date 2010-05-20 * @time 上午10:41:13 */ public class IPAddrFetcher{ public String getIPInfo(){ StringBuilder sb=new StringBuilder(); try{ Enumeration<NetworkInterface> interfaces=NetworkInterface.getNetworkInterfaces(); while(interfaces.hasMoreElements()){ NetworkInterface ni=interfaces.nextElement(); sb.append("Interface "+ni.getName()+":\r\n"); Enumeration<InetAddress> inetAddresses=ni.getInetAddresses(); while(inetAddresses.hasMoreElements()){ InetAddress address=inetAddresses.nextElement(); sb.append("Address"); if(address instanceof Inet4Address){ sb.append("(v4)"); } else{ sb.append("(v6)"); } sb.append(":address="+address.getHostAddress()+" name="+address.getHostName()+"\r\n"); } } }catch(Exception ex){ ex.printStackTrace(); } return sb.toString(); } public static void main(String[] args){ IPAddrFetcher iPAddrFetcher=new IPAddrFetcher(); System.out.println(iPAddrFetcher.getIPInfo()); } } 运行结果: C:\java>java IPAddrFetcher Interface lo: Address(v4):address=127.0.0.1 name=localhost Interface eth0: Address(v4):address=192.168.1.102 name=javaweb.cc