新手寫的一個隨機點名器
阿新 • • 發佈:2019-02-02
package one;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class demo_dm extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton but ;
private JButton butt ;
private JPanel jp;
private JPanel jps;
private JLabel label;
private JLabel show;
private JButton reset;
private List<String> list = new ArrayList<String>();
private List<String> les=new ArrayList<String>();
private String le = null;
public Connection conn=null;
Random ran;
private String strFilename="G:\\qq.txt";
//一些可變動的變數可以獨立設定,便於修改,而不至於改動一個就需要改大部分程式碼
public static void main(String[] args) throws IOException {
new demo_dm();
}
demo_dm() throws IOException{
init();
initListDate();
}
public void init(){
//初始化swing框架
JFrame jf=new JFrame();
jp=new JPanel();
jf.getContentPane().add(jp);
jps=new JPanel();
jf.getContentPane().add(jps);
jf.setLayout(new GridLayout(2,5,5,5));
jp.setLayout(new GridLayout(2,5,5,5));
jps.setLayout(new GridLayout(1,3,5,5));
but = new JButton("開始點名");
but.addActionListener(this);
butt = new JButton("缺席");
butt.addActionListener(this);
label = new JLabel("隨機點名",SwingConstants.CENTER);
label.setFont(new Font("楷體",Font.BOLD,40));
show = new JLabel("",SwingConstants.CENTER);
show.setFont(new Font("楷體",Font.BOLD,30));
reset = new JButton("重新點名");
reset.addActionListener(this);
jp.add(label);
jp.add(show);
jps.add(but);
jps.add(butt);
jps.add(reset);
jf.setVisible(true);
jf.setResizable(true);
jf.setBounds(100,100,550,350);
jf.setTitle("點名");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/wkk";//這個資料庫的database是自己建立的
String user="root";
String password="";
conn=DriverManager.getConnection(url,user,password);
if(conn!=null)
{System.out.println("資料庫連線成功!!");}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public void serchdatas(){
String sql="select * from java;";
ResultSet rs=null;
try {
Statement stas=conn.createStatement();
rs=stas.executeQuery(sql);
while(rs.next()){
list.add(rs.getString("id")+","+rs.getString("name")+
","+rs.getString("class"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void initListDate() throws IOException{
//初始化匯入的資料
getConnection();
serchdatas();
}
/**
* 傳入檔名以及字串, 將字串資訊儲存到檔案中
*
* @param strFilename
* @param strBuffer
*/
public void TextToFile( String strBuffer)
{
try
{
// 建立檔案物件
File fileText = new File(strFilename);
if(!fileText.exists())
{fileText.createNewFile();}
// 向檔案寫入物件寫入資訊
FileWriter fileWriter = new FileWriter(fileText);
// 寫檔案
fileWriter.write(strBuffer);
// 關閉
fileWriter.close();
}
catch (IOException e)
{
//
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
/*
* */
Object obj = e.getSource();
if(obj == but){
if(list.size()==0){
JOptionPane.showMessageDialog(null,"沒有人了!");
return ;
}else{
ran = new Random();
int num = ran.nextInt(list.size());
String ss=list.get(num);
le=ss;//將資料傳給字串le然後顯示在缺席按鈕上
les.add(ss+";");//將資料傳給連結串列,然後再寫入到txt文字中
// num++;
show.setText(ss);
list.remove(ss);
}
}
if(obj==butt){
// for(int i=0;i<num;i++){
TextToFile(les.toString());
show.setText(le+"缺席!");
}
if(obj == reset){
show.setText(" ");
les.clear();//清空les連結串列,然後將空表傳給文字
TextToFile(les.toString());
try {
initListDate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class demo_dm extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton but ;
private JButton butt ;
private JPanel jp;
private JPanel jps;
private JLabel label;
private JLabel show;
private JButton reset;
private List<String> list = new ArrayList<String>();
private List<String> les=new ArrayList<String>();
private String le = null;
public Connection conn=null;
Random ran;
private String strFilename="G:\\qq.txt";
//一些可變動的變數可以獨立設定,便於修改,而不至於改動一個就需要改大部分程式碼
public static void main(String[] args) throws IOException {
new demo_dm();
}
demo_dm() throws IOException{
init();
initListDate();
}
public void init(){
//初始化swing框架
JFrame jf=new JFrame();
jp=new JPanel();
jf.getContentPane().add(jp);
jps=new JPanel();
jf.getContentPane().add(jps);
jf.setLayout(new GridLayout(2,5,5,5));
jp.setLayout(new GridLayout(2,5,5,5));
jps.setLayout(new GridLayout(1,3,5,5));
but = new JButton("開始點名");
but.addActionListener(this);
butt = new JButton("缺席");
butt.addActionListener(this);
label = new JLabel("隨機點名",SwingConstants.CENTER);
label.setFont(new Font("楷體",Font.BOLD,40));
show = new JLabel("",SwingConstants.CENTER);
show.setFont(new Font("楷體",Font.BOLD,30));
reset = new JButton("重新點名");
reset.addActionListener(this);
jp.add(label);
jp.add(show);
jps.add(but);
jps.add(butt);
jps.add(reset);
jf.setVisible(true);
jf.setResizable(true);
jf.setBounds(100,100,550,350);
jf.setTitle("點名");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/wkk";//這個資料庫的database是自己建立的
String user="root";
String password="";
conn=DriverManager.getConnection(url,user,password);
if(conn!=null)
{System.out.println("資料庫連線成功!!");}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public void serchdatas(){
String sql="select * from java;";
ResultSet rs=null;
try {
Statement stas=conn.createStatement();
rs=stas.executeQuery(sql);
while(rs.next()){
list.add(rs.getString("id")+","+rs.getString("name")+
","+rs.getString("class"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void initListDate() throws IOException{
//初始化匯入的資料
getConnection();
serchdatas();
}
/**
* 傳入檔名以及字串, 將字串資訊儲存到檔案中
*
* @param strFilename
* @param strBuffer
*/
public void TextToFile( String strBuffer)
{
try
{
// 建立檔案物件
File fileText = new File(strFilename);
if(!fileText.exists())
{fileText.createNewFile();}
// 向檔案寫入物件寫入資訊
FileWriter fileWriter = new FileWriter(fileText);
// 寫檔案
fileWriter.write(strBuffer);
// 關閉
fileWriter.close();
}
catch (IOException e)
{
//
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
/*
* */
Object obj = e.getSource();
if(obj == but){
if(list.size()==0){
JOptionPane.showMessageDialog(null,"沒有人了!");
return ;
}else{
ran = new Random();
int num = ran.nextInt(list.size());
String ss=list.get(num);
le=ss;//將資料傳給字串le然後顯示在缺席按鈕上
les.add(ss+";");//將資料傳給連結串列,然後再寫入到txt文字中
// num++;
show.setText(ss);
list.remove(ss);
}
}
if(obj==butt){
// for(int i=0;i<num;i++){
TextToFile(les.toString());
show.setText(le+"缺席!");
}
if(obj == reset){
show.setText(" ");
les.clear();//清空les連結串列,然後將空表傳給文字
TextToFile(les.toString());
try {
initListDate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/*基本思路:1.將資料匯入資料庫,然後運用java連線資料庫,運用查詢語句將其存入到String型別的list中
2,初始化匯入資料,然後再初始化swing,佈局相關窗體,新增監聽事件
3,將初始化函式加入建構函式中,當類例項化就執行所有初始化方法
缺陷:匯入到Txt中沒辦法換行,導致資料一行一行存放,顯示很混亂
還可以加:缺席次數,放到另一個文字中,
*/