package gui; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Scanner; import javax.swing.*; class guiTest extends JFrame implements ActionListener { private JTextArea ta=new JTextArea(50,80); private JFileChooser jfc=new JFileChooser(new File(".")); private JButton bOpen,bSave; public guiTest() { super("文档选择框应用程序"); JScrollPane ps=new JScrollPane(ta); bOpen=new JButton("选取源文件"); bSave=new JButton("存盘"); JButton bCount = new JButton("转换"); //这个是自定义的按钮 setLayout(new FlowLayout(FlowLayout.CENTER,15,10)); add(ps); add(bOpen); add(bSave); add(bCount); bOpen.addActionListener(this); bSave.addActionListener(this); bCount.addActionListener(this); setVisible(true); setSize(1600,1560); } public static void main(String[] args) { guiTest frm=new guiTest(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { JButton jbt=(JButton)e.getSource(); if(jbt==bOpen) { int status=jfc.showOpenDialog(this); if(status!=JFileChooser.APPROVE_OPTION) ta.setText("没有选择文件"); else { try{ File file1=jfc.getSelectedFile(); Scanner scan=new Scanner(file1); String info=""; while(scan.hasNext()) { String str=scan.nextLine(); info+=str+" "; } scan.close(); ta.setText(info); } catch(FileNotFoundException ex){ } } //ta.setText("按钮一"); }else if(jbt==bSave){ ta.setText("保存的按钮"); }else{ ta.setText("计算的按钮"); } } }