📄 monitor.java
字号:
package myprojects.monitor;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.util.*;
import javax.comm.*;
import java.math.*;
class Monitor extends JFrame implements ItemListener, ActionListener
{
public static void main(String args[]) {
System.out.println("Starting Monitor...");
Monitor mainFrame = new Monitor();
mainFrame.setSize(600, 500);
mainFrame.setTitle("深海化学及物理量分析系统");
mainFrame.setVisible(true);
/*try
{
DBConnection dBConnection = new DBConnection();
Connection c = dBConnection.getConnection();
System.out.println("Data saved?");
Statement monitor = c.createStatement();
System.out.println("Data saved?");
monitor.execute("INSERT temperature(temperature) VALUES('222222')");
monitor.close();
System.out.println("Data saved!");
} catch(SQLException e){
e.printStackTrace();
}*/
}
public String timeSave;
public String temperatureSave;
public String H2SSave;
public String SO4Save;
public String PHSave;
public String tempDateString;
public static String sheetName;
Color blueColor = new Color(190,210,240);
Font myFont = new Font("TimesRoman",Font.BOLD,24);
Font endFont = new Font("TimesRoman",Font.BOLD+Font.ITALIC,20);
Label mainLabel = new Label(" 深海化学及物理量分析系统");
Label end = new Label(" 浙江大学");
Label comPrompt = new Label("请选择串口并配置参数——");
Choice comChoice = new Choice();
Panel comPanel = new Panel();
Label filling = new Label("");
Label timeLabel = new Label("时间:");
Label tmperatureLabel = new Label("温度(摄氏度):");
Label H2SLabel = new Label("H2S(mV):");
Label SO4Label = new Label("碳酸根(mV):");
Label PHLabel = new Label("PH值(mV):");
TextField showTime = new TextField(20);
TextField showTemperature = new TextField(20);
TextField showH2S = new TextField(20);
TextField showSO4 = new TextField(20);
TextField showPH = new TextField(10);
Label NameLabel = new Label("数据保存为:");
TextField showName = new TextField(20);// show the name of Excel
Vector vecComOperation = new Vector();
Enumeration portListMain = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portIdMain;
public Monitor() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try
{
ComOperation.Excel.close();
dispose();
System.exit(0);
}catch(Exception exc){}
}
});
GridBagLayout gbLayout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.getContentPane().setLayout(gbLayout);
/**
* add mainLabel
*/
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(0,5,20,10);
mainLabel.setBackground(blueColor);
mainLabel.setFont(myFont);
gbLayout.setConstraints(mainLabel,gbc);
this.getContentPane().add(mainLabel);
/**
* add "please choose com label"
*/
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(0,5,0,10);
comPrompt.setBackground(blueColor);
gbLayout.setConstraints(comPrompt,gbc);
this.getContentPane().add(comPrompt);
/**
* add comChoice
*/
while(portListMain.hasMoreElements())
{
portIdMain = (CommPortIdentifier)portListMain.nextElement();
if(portIdMain.getPortType() == CommPortIdentifier.PORT_SERIAL)
comChoice.addItem(portIdMain.getName());
}
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(0,0,0,250);
comChoice.addItemListener(this);
gbLayout.setConstraints(comChoice,gbc);
this.getContentPane().add(comChoice);
/**
* add comPanel
*/
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(comPanel,gbc);
comPanel.setBackground(blueColor);
comPanel.setLayout(new GridLayout(3,1));
comPanel.add(filling);
this.getContentPane().add(comPanel);
/**
* add parameters
*/
// time
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(timeLabel,gbc);
timeLabel.setBackground(blueColor);
this.getContentPane().add(timeLabel);
gbc.gridx = 2;
gbc.gridy = 4;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
this.showTime.setText("time");
gbLayout.setConstraints(showTime,gbc);
this.getContentPane().add(showTime);
//temperature
gbc.gridx = 1;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(tmperatureLabel,gbc);
tmperatureLabel.setBackground(blueColor);
this.getContentPane().add(tmperatureLabel);
gbc.gridx = 2;
gbc.gridy = 5;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
this.showTemperature.setText("temperature");
gbLayout.setConstraints(showTemperature,gbc);
this.getContentPane().add(showTemperature);
//H2S
gbc.gridx = 1;
gbc.gridy = 6;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(H2SLabel,gbc);
H2SLabel.setBackground(blueColor);
this.getContentPane().add(H2SLabel);
gbc.gridx = 2;
gbc.gridy = 6;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
this.showH2S.setText("H2S");
gbLayout.setConstraints(showH2S,gbc);
this.getContentPane().add(showH2S);
//SO4
gbc.gridx = 1;
gbc.gridy = 7;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(SO4Label,gbc);
SO4Label.setBackground(blueColor);
this.getContentPane().add(SO4Label);
gbc.gridx = 2;
gbc.gridy = 7;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
this.showSO4.setText("CO3");
gbLayout.setConstraints(showSO4,gbc);
this.getContentPane().add(showSO4);
//PH
gbc.gridx = 1;
gbc.gridy = 8;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(PHLabel,gbc);
PHLabel.setBackground(blueColor);
this.getContentPane().add(PHLabel);
gbc.gridx = 2;
gbc.gridy = 8;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
this.showPH.setText("PH");
gbLayout.setConstraints(showPH,gbc);
this.getContentPane().add(showPH);
// Excel name
gbc.gridx = 1;
gbc.gridy = 9;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(NameLabel,gbc);
NameLabel.setBackground(blueColor);
this.getContentPane().add(NameLabel);
gbc.gridx = 2;
gbc.gridy = 9;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
gbLayout.setConstraints(showName,gbc);
this.getContentPane().add(showName );
//end
gbc.gridx = 1;
gbc.gridy = 10;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.insets = new Insets(15,5,0,5);
end.setFont(this.endFont);
gbLayout.setConstraints(end,gbc);
this.getContentPane().add(end);
}
public boolean isResizable()
{
return false;
}
public void itemStateChanged(ItemEvent e)
{
boolean flagComJudge = true;
if(e.getItemSelectable()==comChoice)
{
Enumeration selectedComs = vecComOperation.elements();
// avoid to select a com more than one time
while(selectedComs.hasMoreElements())
{
ComOperation comOperation = (ComOperation)(selectedComs.nextElement());
if(comOperation.getComName().equals(comChoice.getSelectedItem()))
{
flagComJudge = !flagComJudge;
break;
}
}
// after choose a before-unselected com, add a ComOperation object to vecComOperation
// and a textfield which is a property of ComOperation to comPanel
if(flagComJudge)
{
System.out.println (vecComOperation.size());
ComOperation comOperation = new ComOperation(comChoice.getSelectedItem(),this);
vecComOperation.addElement(comOperation);
comPanel.add(comOperation.bag);
comOperation.bag.doLayout();
comPanel.doLayout();
}
}
}
public void actionPerformed(ActionEvent e)
{
}
public void saveData(String str)
{
try
{
TimeStyle now = new TimeStyle();
tempDateString = now.getTime();
String time16 = str.substring(2,14);
timeSave = time16.substring(0,2)+"年"+time16.substring(2,4)+"月"+time16.substring(4,6)+"日"+time16.substring(6,8)+"时"+time16.substring(8,10)+"分"+time16.substring(10,12)+"秒";
String temperature8 = str.substring(14,20);
Integer temperatureInteger = Integer.valueOf(temperature8,16);
System.out.println("Integer:"+temperatureInteger);
int temperatureInt = temperatureInteger.intValue();
double temperature = (temperatureInt*160.0)/16777215;
temperature =(temperature-59.543)/0.1836666666;
System.out.println("double:"+temperature);
BigDecimal btem = new BigDecimal(temperature);
temperature = btem.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println("BigDecimal:"+temperature);
temperatureSave = "" + temperature;
String H2S = str.substring(20,26);
Integer H2SInteger = Integer.valueOf(H2S,16);
int H2SInt = H2SInteger.intValue();
double H2Sf = (H2SInt-8388608.0)*1280.0/8388607;
BigDecimal bH2S = new BigDecimal(H2Sf);
H2Sf = bH2S.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
H2SSave = "" + H2Sf;
String SO4 = str.substring(26,32);
Integer SO4Integer = Integer.valueOf(SO4,16);
int SO4Int = SO4Integer.intValue();
double SO4f = (SO4Int-8388608.0)*1280.0/8388607;
BigDecimal bSO4f = new BigDecimal(SO4f);
SO4f = bSO4f.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
SO4Save = "" + SO4f;
String PH = str.substring(32,38);
Integer PHInteger = Integer.valueOf(PH,16);
int PHInt = PHInteger.intValue();
double PHf = (PHInt-8388608.0)*1280.0/8388607;
BigDecimal bPHf = new BigDecimal(PHf);
PHf = bPHf.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
PHSave = "" + PHf;
this.showTime.setText(timeSave);
this.showTemperature.setText(temperatureSave);
this.showH2S.setText(H2SSave);
this.showSO4.setText(SO4Save);
this.showPH.setText(PHSave);
//Jexcel2.save(timeSave,temperatureSave,H2SSave,SO4Save,PHSave,tempDateString);
/*DBConnection dBConnection = new DBConnection();
Connection c = dBConnection.getConnection();
Statement monitor = c.createStatement();
monitor.execute(
"INSERT parameters(timesave,timerough,temperature,temrough,H2S,SO4,PH,systemtime) VALUES('"+timeSave+"','"+time16+"','"+temperatureSave+"','"+temperature8+"','"+H2SSave+"','"+SO4Save+"','"+PHSave+"','"+tempDateString+"')");
monitor.close();*/
} catch(Exception e){
e.printStackTrace();
}
/*try
{
DBConnection dBConnection = new DBConnection();
Connection c = dBConnection.getConnection();
System.out.println("Data saved?");
Statement monitor = c.createStatement();
System.out.println("Data saved?");
monitor.execute("INSERT parameters(temperature) VALUES('888888')");
monitor.close();
System.out.println("Data saved!");
} catch(SQLException e){
e.printStackTrace();
}*/
}
}
class TimeStyle
{
Calendar now = Calendar.getInstance();
String nowStr;
public TimeStyle()
{
nowStr = now.get(now.YEAR)+"-"+now.get(now.MONTH)+"-"
+now.get(now.DAY_OF_MONTH)+" "+now.get(now.HOUR_OF_DAY)+":"
+now.get(now.MINUTE)+":"+now.get(now.SECOND);
}
public String getTime()
{
return(nowStr);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -