Java Swing客户端小项目

记录一下两个用java swing写的客户端。

项目1:

关键词:swing  jtable

代码如下:

1、主类:

package com.my.agent.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class AgentClinet implements ActionListener{

    JTextField textField = null;
    MyTabelModel tm = null;
    JFrame jframe = null;
    public AgentClinet(){
        jframe = new JFrame("");
        jframe.setSize(600, 400);
        jframe.setBackground(Color.BLUE);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setAlwaysOnTop(true);
        jframe.setLayout(new FlowLayout());

        //上面按钮
        JPanel jpanel = new JPanel();
        JButton button = new JButton("添加信息");
        button.addActionListener(this);
        jpanel.add(button);
        button = new JButton("一键启动");
        button.addActionListener(this);
        jpanel.add(button);
        button = new JButton("一键停止");
        button.addActionListener(this);
        jpanel.add(button);
        jframe.add(jpanel);

        //数据展示
        tm = new MyTabelModel(null);
        final JTable table = new JTable(tm);
        table.setPreferredScrollableViewportSize(new Dimension(550, 220));
        JScrollPane s = new JScrollPane(table);
        jframe.add(s);

        //下层按钮
        jpanel = new JPanel(new FlowLayout(0));
        jpanel.setPreferredSize(new Dimension(580, 30));
        JLabel label = new JLabel("单个代理home:");
        textField = new JTextField(30);
        jpanel.add(label);
        jpanel.add(textField);

        button = new JButton("启动");
        button.addActionListener(this);
        jpanel.add(button);
        button = new JButton("停止");
        button.addActionListener(this);
        jpanel.add(button);

        jframe.add(jpanel);

        label = new JLabel("删除功能暂不支持");
        jframe.add(label);
        jframe.setVisible(true);

        table.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                  //得到选中的行列的索引值
                   int r= table.getSelectedRow();
                   //int c= table.getSelectedColumn();
                   //得到选中的单元格的值,表格中都是字符串
                   Object value= table.getValueAt(r, 2);
                   textField.setText(value.toString());
                  /* String info=r+"行"+c+"列值 : "+value.toString();
                   System.out.println(info);*/
            }
            @Override
            public void mouseReleased(MouseEvent arg0) {
            }
            @Override
            public void mousePressed(MouseEvent arg0) {
            }
            @Override
            public void mouseExited(MouseEvent arg0) {
            }
            @Override
            public void mouseEntered(MouseEvent arg0) {
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        try {
            if(btn.getText().equals("添加信息")){
                JFileChooser fileChooser = new JFileChooser("目录选择:");
                fileChooser.setFileFilter(new FileNameExtensionFilter("", "bat"));
                fileChooser.setAcceptAllFileFilterUsed(true);
                fileChooser.showOpenDialog(jframe);
                if(null != fileChooser.getSelectedFile() ){
                    String filePath = fileChooser.getSelectedFile().getAbsolutePath();
                    //代理目录
                    String str = filePath.substring(0,filePath.lastIndexOf("\\"));
                    if(MyDao.addLineData(null, str)){
                        jframe.dispose();
                        new AgentClinet();
                    };
                }

            }else if(btn.getText().equals("一键启动")){
                String[][] model = tm.getModel();
                for (int i = 0; i < model.length; i++) {
                    Runtime.getRuntime().exec("cmd /c start  "+model[i][2]+"\\startup.bat");
                }
            }else if(btn.getText().equals("一键停止")){
                String[][] model = tm.getModel();
                for (int i = 0; i < model.length; i++) {
                    Runtime.getRuntime().exec("cmd /c start  "+model[i][2]+"\\stop.bat");
                }
            }else if(btn.getText().equals("启动")){
                Runtime.getRuntime().exec("cmd /c start  "+textField.getText()+"\\startup.bat");
            }else if(btn.getText().equals("停止")){
                Runtime.getRuntime().exec("cmd /c start  "+textField.getText()+"\\stop.bat");
            }
        } catch (IOException e1) {
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        new AgentClinet();
    }

}

MyTabelModel.java

package com.my.agent.client;

import javax.swing.table.AbstractTableModel;

public class MyTabelModel extends AbstractTableModel{

    private static final long serialVersionUID = 1L;

    String[] tableHeader = new String[]{"序号","名称","HOME"};

    private String[][] model = null;

    public MyTabelModel(String path) {
        model = MyDao.getDataFromFile(path);
    }

    public String[][] getModel() {
        return model;
    }

    public void setModel(String[][] model) {
        this.model = model;
    }

    @Override
    public int getColumnCount() {
        return tableHeader.length;
    }

    @Override
    public int getRowCount() {
        return model.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        return model[row][col];
    }
    /**
     * 此方法返回col位置的列的名称
     */
    @Override
    public String getColumnName(int column) {
        return tableHeader[column];
    }

}

MyTabelModel.java

数据来源,放到一个txt文件里面:

package com.my.agent.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyDao {

    private static BufferedWriter bw;

    public static void main(String[] args) {
        getDataFromFile(null);
    }

    public static String[][] getDataFromFile(String dir){
        String agentsFile = "";
        if(null==dir || "".equals(dir)){
            dir =  System.getProperty("user.home");
        }
        File dirF = new File(dir);
        if(!dirF.isDirectory()){
            throw new RuntimeException("配置的地址不是一个目录");
        }else{
            agentsFile = dirF.getAbsolutePath()+"/agents.txt";
        }
        File file = new File(agentsFile);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
            }
        }
        List<String> datas = getLineData(agentsFile);
        String[][] ret = new String[datas.size()][];
        for (int i = 0; i < datas.size(); i++) {
            String[] table = ((i+1)+"&"+datas.get(i).trim()).split("&");
            ret[i] = table;
        }
        /*
        for (int i = 0; i < ret.length; i++) {
            System.out.println(ret[i][0]+"=="+ret[i][1]+"==="+ret[i][2]);
        }
        */
        return ret;
    }

    public static List<String> getLineData(String path) {
        List<String> urls = new ArrayList<String>();
        try {
            FileReader reader = new FileReader(path);
            BufferedReader br = new BufferedReader(reader);
            String str = null;
            while ((str = br.readLine()) != null) {
                if(str!=null && str.contains("&")){
                    urls.add(str);
                }
            }
            br.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return urls;
    }

    public static boolean addLineData(String filename,String filePath){
        if(filename==null){
            filename =  System.getProperty("user.home")+"\\agents.txt";
        }
        String agentName = filePath.substring(filePath.lastIndexOf("\\")+1);
        try {
            File file = new File(filename);
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(filename,true);
            bw = new BufferedWriter(fileWriter);
            bw.write(agentName+"&"+filePath);
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (Throwable e) {
            //do noting
            return false;
        }
        return true;
    }

}

以上是第一个小工具,只是用了txt文件进行数据存储,第二个客户端按数据源分为以下两种:

1.查询MongoDB数据库:

主类:

 package com.my.mssage.client;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MessageClient implements ActionListener{

    JFrame jframe = null;
    JTextField pathText = null;
    JTextField userNameText = null;
    JTextField pwdText = null;
    JTextArea textArea = null;
    JButton startBtn = null;
    JButton stopBtn = null;
    JTextField timeText = null;

    boolean timerRun = false;

    JLabel dateLabel = new JLabel("监控时间(-至今)");
    JTextField dateText = null;
    boolean dateshow = false;
    public MessageClient(){
        jframe = new JFrame("监控");
        jframe.setSize(600, 400);
        jframe.setBackground(Color.BLUE);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setAlwaysOnTop(true);
        jframe.setLayout(new FlowLayout(0));
        jframe.getContentPane().add(new JLabel("数据库ip地址:"));
        pathText = new JTextField(10);
        pathText.setText("");
        jframe.getContentPane().add(pathText);
        jframe.getContentPane().add(new JLabel("端口:"));
        userNameText = new JTextField(10);
        jframe.getContentPane().add(userNameText);
        jframe.getContentPane().add(new JLabel("文档名称:"));
        pwdText = new JTextField(10);
        jframe.getContentPane().add(pwdText);
        jframe.getContentPane().add(new JLabel("刷新时间(秒):"));
        timeText = new JTextField(5);
        timeText.setText("300");
        jframe.getContentPane().add(timeText);

        dateText = new JTextField(10);
        dateText.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        jframe.getContentPane().add(dateLabel);
        jframe.getContentPane().add(dateText);
        dateLabel.setVisible(dateshow);
        dateText.setVisible(dateshow);

        JPanel panel = new JPanel(new FlowLayout(2));
        startBtn = new JButton("开始监控");
        startBtn.addActionListener(this);
        panel.add(startBtn);
        stopBtn = new JButton("停止监控");
        stopBtn.addActionListener(this);
        panel.add(stopBtn);
        jframe.add(panel);
        textArea = new JTextArea(18,53);
        JScrollPane scrollPane = new JScrollPane(textArea);
        jframe.add(scrollPane);

        timeText.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {}
            @Override
            public void keyReleased(KeyEvent e) {}
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_F11){
                    dateshow = !dateshow;
                    dateLabel.setVisible(dateshow);
                    dateText.setVisible(dateshow);
                }
            }
        });

        jframe.setVisible(true);
    }

    public static void main(String[] args) {
        new MessageClient();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        JButton btn = (JButton)e.getSource();
        final Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                String startDate = null;
                if(dateshow){
                    startDate = dateText.getText();
                }
                if(timerRun){
                    textArea.setText("开始监控:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\r\n");
                    List<WorkInfo> list = MongoDao.queryWorkitem(pathText.getText(),
                            userNameText.getText(), pwdText.getText(), startDate);

                    for (WorkInfo workInfo : list) {
                        textArea.append(workInfo.getStopTime()+"======"+workInfo.getWorkName()+":"+workInfo.getWorkType()+":"+workInfo.getWorkState()+":"+workInfo.getStateDetail()+"\r\n");
                        SoundPlay.playSound(workInfo.getWorkType());
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }else{
                    timer.cancel();
                    this.cancel();
                    textArea.setText("停止监控......");
                }
                System.gc();
            }
        };

        if(btn.getText().equals("开始监控")){
            timerRun = true;
            System.out.println(pathText.getText()+"=="+userNameText.getText()+"=="+pwdText.getText());
            timer.schedule(task,0, Long.parseLong(timeText.getText())*1000);
        }else if(btn.getText().equals("停止监控")){
            timerRun = false;
            System.out.println("stop......");
            task.cancel();
            timer.cancel();
        }else{
            System.out.println("......");
        }

    }
}

数据查询:

package com.my.mssage.client;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoDao {

    public static void main(String[] args) {

    }

    public static List<WorkInfo> queryWorkitem(String ip, String port, String dbname,String startDate) {

        List<String> list = new ArrayList<String>();
        List<WorkInfo> workList = new ArrayList<WorkInfo>();
        MongoClient client = new MongoClient(ip,Integer.parseInt(port));
        MongoDatabase db = client.getDatabase(dbname);

        try {
            /*
            MongoIterable<String> cos = db.listCollectionNames();
            for (String name : cos) {
                System.out.println(name);
            }
             */
            MongoCollection<Document> collection = db.getCollection("nSite.wf.workitems");
            if(startDate == null || "".equals(startDate)){
                startDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            }
            BasicDBObject query = new BasicDBObject();
            query.put("state", "Exception");
            DBObject timeQuery = new BasicDBObject();
            timeQuery.put("$gt", new SimpleDateFormat("yyyy-MM-dd").parse(startDate));
            timeQuery.put("$lt", new Date());
            query.put("startTime", timeQuery);
            FindIterable<Document> doc = collection.find(query);

            int i = 0;

            for (Document document : doc) {
                Date tempDate = document.getDate("stopTime");
                String stateDetail = document.getString("stateDetails");
                WorkInfo workInfo = new WorkInfo(document.getString("name"),
                        document.getString("activityDefineName"), "出错了");
                workInfo.setStopTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tempDate));
                workInfo.setStateDetail(stateDetail);
                workList.add(workInfo);
                i++;
                list.add(document.get("name")+"=="+document.get("activityDefineName")+"==出错了");
                System.out.println(document.get("name")+"=="+document.get("activityDefineName")+"==出错了");
            }
            System.out.println(i+"===="+list.size());

        } catch (Throwable e) {
            throw new RuntimeException(e.getMessage());
        }finally {
            client.close();
        }
        return workList;
    }

}

MongoDao.java

2.查询Mysql数据库:

主类:

package com.my.mssage.client;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MysqlClient implements ActionListener{

    JFrame jframe = null;
    JTextField pathText = null;
    JTextField portText = null;
    JTextField userNameText = null;
    JTextField pwdText = null;
    JTextArea textArea = null;
    JButton startBtn = null;
    JButton stopBtn = null;
    JTextField timeText = null;

    boolean timerRun = false;

    JLabel dateLabel = new JLabel("监控时间(-至今)");
    JTextField dateText = null;
    boolean dateshow = false;

    public MysqlClient(){
        jframe = new JFrame("监控");
        jframe.setSize(600, 400);
        jframe.setBackground(Color.BLUE);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setAlwaysOnTop(true);
        jframe.setLayout(new FlowLayout(0));
        jframe.getContentPane().add(new JLabel("db地址:端口:"));
        pathText = new JTextField(12);
        pathText.setText("");
        jframe.getContentPane().add(pathText);
        jframe.getContentPane().add(new JLabel("db名称:"));
        portText = new JTextField(5);
        portText.setText("");
        jframe.getContentPane().add(portText);
        jframe.getContentPane().add(new JLabel("db用户名:"));
        userNameText = new JTextField(5);
        jframe.getContentPane().add(userNameText);
        jframe.getContentPane().add(new JLabel("db密码:"));
        pwdText = new JTextField(5);
        jframe.getContentPane().add(pwdText);
        jframe.getContentPane().add(new JLabel("刷新时间(秒):"));
        timeText = new JTextField(6);
        timeText.setText("300");
        jframe.getContentPane().add(timeText);

        dateText = new JTextField(10);
        dateText.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        jframe.getContentPane().add(dateLabel);
        jframe.getContentPane().add(dateText);
        dateLabel.setVisible(dateshow);
        dateText.setVisible(dateshow);

        JPanel panel = new JPanel(new FlowLayout(2));
        startBtn = new JButton("开始监控");
        startBtn.addActionListener(this);
        panel.add(startBtn);
        stopBtn = new JButton("停止监控");
        stopBtn.addActionListener(this);
        panel.add(stopBtn);
        jframe.add(panel);
        textArea = new JTextArea(18,53);
        JScrollPane scrollPane = new JScrollPane(textArea);
        jframe.add(scrollPane);
        timeText.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {}
            @Override
            public void keyReleased(KeyEvent e) {}
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_F11){
                    dateshow = !dateshow;
                    dateLabel.setVisible(dateshow);
                    dateText.setVisible(dateshow);
                }
            }
        });

        jframe.setVisible(true);
    }

    public static void main(String[] args) {
        new MysqlClient();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        JButton btn = (JButton)e.getSource();

        final Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                String startDate = null;
                if(dateshow){
                    startDate = dateText.getText();
                }
                if(timerRun){

                    textArea.setText("开始监控:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\r\n");
                    List<WorkInfo> list = NSDao.query(pathText.getText(), portText.getText(),
                            userNameText.getText(), pwdText.getText(), startDate);
                    for (WorkInfo workInfo : list) {
                        textArea.append(workInfo.getStopTime()+"======"+workInfo.getWorkName()+":"+workInfo.getWorkType()+":"+workInfo.getWorkState()+":"+workInfo.getStateDetail()+"\r\n");
                        SoundPlay.playSound(workInfo.getWorkType());
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }else{
                    timer.cancel();
                    this.cancel();
                    textArea.setText("停止监控......");
                }
                System.gc();
            }
        };

        if(btn.getText().equals("开始监控")){
            timerRun = true;
            System.out.println(pathText.getText()+"=="+userNameText.getText()+"=="+pwdText.getText());
            timer.schedule(task,0, Long.parseLong(timeText.getText())*1000);
        }else if(btn.getText().equals("停止监控")){
            timerRun = false;
            System.out.println("stop......");
            task.cancel();
            timer.cancel();
        }else{
            System.out.println("......");
        }
    }
}

数据来源:

package com.my.mssage.client;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NSDao {

        private static String driver = "com.mysql.jdbc.Driver";

        static Connection conn = null;
        static Statement statement = null;
        static PreparedStatement ps = null;
        static ResultSet rs = null;
        static List<String> list = null;

        /*
         * 连接数据库
         */
         public static void connDB(String iport,String dbname,String user,String pwd) {
              try {
               String url = "jdbc:mysql://"+iport+"/"+dbname;
               Class.forName(driver).newInstance();
               conn = DriverManager.getConnection(url, user, pwd);
               if (!conn.isClosed()) {
                  System.out.println("Succeeded connecting to MySQL!");
               }

               statement = conn.createStatement();
              } catch (Exception e) {
               e.printStackTrace();
              }
             }

        /*
         * 关闭数据库
         */
         public static void closeDB() {
              if(rs != null ){
                  try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
              }
              if(statement != null){
                  try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
              if(conn != null){
               try {
                conn.close();
               } catch (SQLException e) {
                e.printStackTrace();
                   }
               }
             }             

         /*
          * 查询数据表
          */
         public static List<WorkInfo> query(String iport,String dbname,String user,String pwd,String startDate){
             List<WorkInfo> workList = new ArrayList<WorkInfo>();
             connDB(iport,dbname,user,pwd);
            if(startDate == null || "".equals(startDate)){
                startDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            }
            list = new ArrayList<String>();//返回所有记录
            String str_query = "";
            try {
                String sql = "select workname,activitydefname,stoptime,statedetail,createtime from wf_workitem where currentstate = 5 "
                        + " and "
                        +" starttime BETWEEN ‘"
                        +startDate
                        +"‘ and ‘"
                        + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
                        +"‘ order by createtime";
                rs = statement.executeQuery(sql);
                while(rs.next()){
                    WorkInfo workInfo = new WorkInfo(rs.getString(1),
                            rs.getString(2), "出错了");
                    workInfo.setStopTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rs.getDate(3)));
                    workInfo.setStateDetail(rs.getString(4));
                    str_query = rs.getString(1)+"=="+rs.getString(2)+"==出错了";
                    System.out.println(str_query);
                    list.add(str_query);
                    workList.add(workInfo);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeDB();
            }
            return workList;
         }

         public static void main(String[] args) {
             List<WorkInfo> workList = null;
             for (int i = 0; i < workList.size(); i++) {
                System.out.println(list.get(i));
            }
         }

}

3、共有代码bean:

package com.my.mssage.client;

public class WorkInfo {

    private String workName;

    private String workType;

    private String workState;

    private String stopTime;

    private String stateDetail;

    public WorkInfo(String workName,String workType,String workState,
            String stopTime,String stateDetail){
        this.workName = workName;
        this.workType = workType;
        this.workState = workState;
        this.stopTime = stopTime;
        this.stateDetail = stateDetail;
    }

    public WorkInfo(String workName,String workType,String workState){
        this.workName = workName;
        this.workType = workType;
        this.workState = workState;
    }

    public String getStopTime() {
        return stopTime;
    }

    public void setStopTime(String stopTime) {
        this.stopTime = stopTime;
    }

    public String getStateDetail() {
        return stateDetail;
    }

    public void setStateDetail(String stateDetail) {
        this.stateDetail = stateDetail;
    }

    public String getWorkName() {
        return workName;
    }

    public void setWorkName(String workName) {
        this.workName = workName;
    }

    public String getWorkType() {
        return workType;
    }

    public void setWorkType(String workType) {
        this.workType = workType;
    }

    public String getWorkState() {
        return workState;
    }

    public void setWorkState(String workState) {
        this.workState = workState;
    }

}

声音播放:

package com.my.mssage.client;

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class SoundPlay {

    public static void playSound(String type){

        try {
                String dir =  System.getProperty("user.home")+"\\jkaudio";
                File file = new File(dir);
                switch (type) {
                case "打包":
                    file = new File(dir+"\\1.wav");
                    break;
                case "送演播室":
                    file = new File(dir+"\\2.wav");
                    break;
                case "审片回退通知":
                    file =new File(dir+"\\3.wav");
                    break;
                case "自动技审":
                    file =new File(dir+"\\4.wav");
                    break;
                case "TitleToEditsphere":
                    file =new File(dir+"\\5.wav");
                    break;
                default:
                    file = new File(dir+"\\6.wav");
                    break;
                }

                System.out.println(file.getAbsolutePath());
                AudioInputStream cin = AudioSystem.getAudioInputStream(file);
                AudioFormat format = cin.getFormat();
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
                line.open(format);//或者line.open();format参数可有可无
                line.start();
                int nBytesRead = 0;
                byte[] buffer = new byte[512];
                while (true) {
                    nBytesRead = cin.read(buffer, 0, buffer.length);
                    if (nBytesRead <= 0)
                        break;
                    line.write(buffer, 0, nBytesRead);
                }
                line.drain();
                line.close();
            } catch (Throwable e) {
                System.out.println(e.getMessage());
                // do noting
                e.printStackTrace();
            }
    }

    public static void main(String[] args) {
        for (int i = 1; i < 5; i++) {
            playSound(i+"");
        }
    }

}

以上!

时间: 2024-10-08 05:16:57

Java Swing客户端小项目的相关文章

java客房管理小项目,适合java小白练手的项目!

java客房管理小项目 这个客房管理小项目,适合java初学者练手.功能虽然不多,但是内容很齐全! 喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我! 祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!共有5层,每层10间客房,以数字101--509标示:具有入住,退房,搜索,退出四个简单功能: public class Hotel { static final int floor = 5; static fina

java客房管理小项目,适合小白练手

java客房管理小项目这个客房管理小项目,适合java初学者练手.功能虽然不多,但是内容很齐全!喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!共有5层,每层10间客房,以数字101--509标示:具有入住,退房,搜索,退出四个简单功能:public class Hotel {static final int floor = 5;static final int order = 10;private static int countFlo

java GUI(实例小项目--列出磁盘目录)

//java实例小项目:列出磁盘目录. import java.awt.*; import java.awt.event.*; import java.io.File; class MyWindDemo{ //定义一个对话框 private Dialog d; //定义一个标签 private Label lab; //定义一个对话框上的按钮 private Button but_dlg; //定义一个窗体 private Frame f; //定义一个文本框 private TextField

java练手小项目!要想java学得好,练手项目不可少!

Java小项目,实现电影院的自动售票机 运行界面如下 2.类的设计 该系统中必须包括两个类,类名及属性方法设置如下. 电影类(Movie) ²  名称(name) ²  上映日期(date) ²  票价(price) 售票机类(TicketSell) ²  方法:查询所有电影信息  查询指定电影票价以及上映日期  购买电影票 3.具体要求及推荐实现步骤 1.创建电影类 2.创建售票机类 3.开发售票机类初始化电影方法,查询方法和买票方法. 1)初始化方法initial():  初始化5个电影对象

Java太阳系模型小项目

首先定义一个窗口类 package cn.hxd.util; import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent; /** * 游戏项目中用到的常用方法 * @author HXD * */ public class MyFrame extends Frame{ /** * 加载窗口 */ public void launchFrame(){ setSize(Con

Java小项目之:教你做个聊天系统!

Java小项目之:聊天系统今天给大家带来的java练手小项目是一个简单的聊天室,界面简单,操作不难.分为注册系统,登录系统和聊天系统三部分,很适合java小白练手.完整的源码和素材请关注并私信我获取! 界面展示: 代码展示:package chatRoom2; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWr

Java Swing简单的加法器

1 package test; 2 import java.awt.*; 3 import javax.swing.*; 4 import java.awt.event.*; 5 public class FrameDemo2 6 { 7 static JTextField field1=new JTextField(5); 8 static JTextField field2=new JTextField(5); 9 static JTextField field3=new JTextFiel

Java Web学习(33): 阶段小项目使用MVC模型实现购物车效果

项目概述 前面的时候我写过一个商品浏记录的小例子,这一次我们使用实现购物车效果.前面的例子是: http://blog.csdn.net/erlian1992/article/details/52047258.这一次在此基础上来采用的是MVC三层模型实现 (JSP+Servlet+dao)来实现这个小项目. 三层架构: JSP视图层 Servlet控制层 dao模型层 DB数据库层 编码实现: 首先先来数据库脚本items.sql: /* Navicat MySQL Data Transfer

Java小项目之:文件的加密与解密!再也不怕存的小电影被别人发现了!

Java小项目之:文件的加密与解密!再也不怕存的小电影被别人发现了!今天带来的java小项目是加密解密系统,再也不怕别人偷看自己的电脑了,也可以正大光明的存小电影了.减少借别人电脑被看隐私的尴尬,从这个项目开始!界面展示: 部分代码展示:package wt.diy.encryption.gui; import java.io.File; import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JO