/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package t1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* @author Lenovo
*/
public class t1 extends javax.swing.JFrame {
/**
* Creates new form t1
*/
public t1() {
initComponents();
b1.add(r1);
b1.add(r2);
b1.add(r3);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
b1 = new javax.swing.ButtonGroup();
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JList<>();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
txt = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
p = new javax.swing.JPanel();
r1 = new javax.swing.JRadioButton();
r2 = new javax.swing.JRadioButton();
r3 = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(list);
jButton1.setText("读取文本文件");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("读取Excel文件");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setText("计算结果");
txt.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
jLabel3.setText("---------------------------------------------------------------------------------------------------------------");
jLabel4.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jLabel4.setText("数据分析器");
p.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
r1.setText("求和");
r1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
r1ActionPerformed(evt);
}
});
r2.setText("求平均值");
r2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
r2ActionPerformed(evt);
}
});
r3.setText("求素数");
r3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
r3ActionPerformed(evt);
}
});
javax.swing.GroupLayout pLayout = new javax.swing.GroupLayout(p);
p.setLayout(pLayout);
pLayout.setHorizontalGroup(
pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addContainerGap()
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addComponent(r3, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(r1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(r2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
pLayout.setVerticalGroup(
pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(r1)
.addGap(18, 18, 18)
.addComponent(r2)
.addGap(18, 18, 18)
.addComponent(r3)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(p, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(jLabel4)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(111, 111, 111)
.addComponent(p, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
File file = new File("D:\\Excel.xlsx");
FileInputStream fis = new FileInputStream(file);
Workbook wb=new XSSFWorkbook(file);
Sheet filesheet=(Sheet)wb.getSheet("Sheet1");
int numrows =filesheet.getLastRowNum()-filesheet.getFirstRowNum();
Vector data = new Vector();
for(int i=0;i<=numrows;i++){
Row row=filesheet.getRow(i);
//获取每列的数据
String no=row.getCell(0).getStringCellValue();
data.add(no);
}
list.setListData(data);
}catch(Exception ex)
{
javax.swing.JOptionPane.showMessageDialog(rootPane,ex.toString());
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
// TODO add your handling code here:
//File f = new File("D:\\test.txt");
FileReader fr=new FileReader(new File("D:\\文本.txt"));
BufferedReader br = new BufferedReader(fr);
String data = br.readLine();
Vector data2 = new Vector();
while(data!=null){
data2.add(data);
data=br.readLine();
}
list.setListData(data2);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
}
public boolean IsPrime(int number) {
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 2; i < sqrt; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
private void r1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Vector data = new Vector();
int j=0;
String number="";
for(int k=0;k<8;k++)
data.add(list.getModel().getElementAt(k));
for(int i=0;i<data.size();i++){
number=(String)data.get(i);
System.out.println(number);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher((CharSequence)number);
boolean result = matcher.matches();
if(result){
j+=Integer.valueOf(number);
System.out.println(number);
}
}
txt.setText(String.valueOf(j));
}
private void r2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Vector data = new Vector();
int j=0;
int n=0;
String number="";
for(int k=0;k<8;k++)
data.add(list.getModel().getElementAt(k));
for(int i=0;i<data.size();i++){
number=(String)data.get(i);
System.out.println(number);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher((CharSequence)number);
boolean result = matcher.matches();
if(result){
j+=Integer.valueOf(number);
System.out.println(number);
n+=1;
}
}
txt.setText(String.valueOf(j/n));
}
private void r3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Vector data = new Vector();
int j=0;
int n=0;
String number="";
String ss="";
for(int k=0;k<8;k++)
data.add(list.getModel().getElementAt(k));
for(int i=0;i<data.size();i++){
number=(String)data.get(i);
System.out.println(number);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher((CharSequence)number);
boolean result = matcher.matches();
if(result){
if(IsPrime(Integer.valueOf(number)))
ss+=number;
}
}
txt.setText(String.valueOf(ss));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(t1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(t1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(t1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(t1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new t1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup b1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList<String> list;
private javax.swing.JPanel p;
private javax.swing.JRadioButton r1;
private javax.swing.JRadioButton r2;
private javax.swing.JRadioButton r3;
private javax.swing.JLabel txt;
// End of variables declaration
}