java gui 读取文件夹_java GUI读取文本和Excel

/** 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.*/

packaget1;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.util.Vector;importjava.util.logging.Level;importjava.util.logging.Logger;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;/***

*@authorLenovo*/

public class t1 extendsjavax.swing.JFrame {/*** Creates new form t1*/

publict1() {

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")//

private voidinitComponents() {

b1= newjavax.swing.ButtonGroup();

jScrollPane1= newjavax.swing.JScrollPane();

list= new javax.swing.JList<>();

jButton1= newjavax.swing.JButton();

jButton2= newjavax.swing.JButton();

jLabel1= newjavax.swing.JLabel();

txt= newjavax.swing.JLabel();

jLabel3= newjavax.swing.JLabel();

jLabel4= newjavax.swing.JLabel();

p= newjavax.swing.JPanel();

r1= newjavax.swing.JRadioButton();

r2= newjavax.swing.JRadioButton();

r3= newjavax.swing.JRadioButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setViewportView(list);

jButton1.setText("读取文本文件");

jButton1.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setText("读取Excel文件");

jButton2.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(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(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

r1ActionPerformed(evt);

}

});

r2.setText("求平均值");

r2.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

r2ActionPerformed(evt);

}

});

r3.setText("求素数");

r3.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

r3ActionPerformed(evt);

}

});

javax.swing.GroupLayout pLayout= newjavax.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= newjavax.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();

}//

private voidjButton2ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

try{

File file= new File("D:\\Excel.xlsx");

FileInputStream fis= newFileInputStream(file);

Workbook wb=newXSSFWorkbook(file);

Sheet filesheet=(Sheet)wb.getSheet("Sheet1");int numrows =filesheet.getLastRowNum()-filesheet.getFirstRowNum();

Vector data= newVector();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 voidjButton1ActionPerformed(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= newBufferedReader(fr);

String data=br.readLine();

Vector data2= newVector();while(data!=null){

data2.add(data);

data=br.readLine();

}

list.setListData(data2);

}catch(FileNotFoundException ex) {

}catch(IOException ex) {

}

}public boolean IsPrime(intnumber) {int sqrt = (int) Math.sqrt(number) + 1;for (int i = 2; i < sqrt; i++) {if (number % i == 0) {return false;

}

}return true;

}private voidr1ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

Vector data = newVector();int j=0;

String number="";for(int k=0;k<8;k++)

data.add(list.getModel().getElementAt(k));for(int i=0;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 voidr2ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

Vector data = newVector();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

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 voidr3ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

Vector data = newVector();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

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));

}/***@paramargs the command line arguments*/

public static voidmain(String args[]) {/*Set the Nimbus look and feel*/

//

/*If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details seehttp://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);

}//

/*Create and display the form*/java.awt.EventQueue.invokeLater(newRunnable() {public voidrun() {new t1().setVisible(true);

}

});

}//Variables declaration - do not modify

privatejavax.swing.ButtonGroup b1;privatejavax.swing.JButton jButton1;privatejavax.swing.JButton jButton2;privatejavax.swing.JLabel jLabel1;privatejavax.swing.JLabel jLabel3;privatejavax.swing.JLabel jLabel4;privatejavax.swing.JScrollPane jScrollPane1;private javax.swing.JListlist;privatejavax.swing.JPanel p;privatejavax.swing.JRadioButton r1;privatejavax.swing.JRadioButton r2;privatejavax.swing.JRadioButton r3;privatejavax.swing.JLabel txt;//End of variables declaration

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值