?? mainframe.java~132~
字號:
package readfileprocessbarbean;/* 這是一個主界面 */import java.awt.*;import com.borland.jbcl.layout.*;import javax.swing.*;import javax.swing.border.*;import java.awt.event.*;import java.beans.*;import java.io.*;public class MainFrame extends Frame implements PropertyChangeListener{ /* 引用PropertyChangeListener接口,構架JavaBean關聯(lián)屬性監(jiān)聽器 */ XYLayout xYLayout1 = new XYLayout(); JLabel jLabel1 = new JLabel(); Border border1; JPanel jPanel1 = new JPanel(); Border border2; TitledBorder titledBorder1; Border border3; XYLayout xYLayout2 = new XYLayout(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JButton jButton1 = new JButton(); JPanel jPanel2 = new JPanel(); Border border4; TitledBorder titledBorder2; Border border5; JProgressBar jProgressBar1 = new JProgressBar(); XYLayout xYLayout3 = new XYLayout(); Border border6; JPanel jPanel3 = new JPanel(); Border border7; TitledBorder titledBorder3; Border border8; XYLayout xYLayout4 = new XYLayout(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JButton jButton4 = new JButton(); Timer timer = null; FileNamePathBean filenamepathbean = new FileNamePathBean(); // 定義FileNamePathBean的實例對象 maxIntervalTimeBean maxintervaltimebean = new maxIntervalTimeBean(); //定義maxIntervalTimeBean實例對象 String newFileNamePathValue = null; // 存放文件名和路徑 int newIntervalTimeValue = -1; // 存放進度條的最大間隔時間 BufferedReader file = null; int TotalLength = -1; // 文件的總行數(shù) int i = 0; String str = null; String outputStr = null; public MainFrame() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { MainFrame mainFrame = new MainFrame(); mainFrame.setLocation(150,80); mainFrame.setSize(421,400); mainFrame.show(); } private void jbInit() throws Exception { border1 = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(new Color(118, 150, 255),1),BorderFactory.createEmptyBorder(1,1,1,1)); border2 = BorderFactory.createLineBorder(new Color(79, 223, 184),1); titledBorder1 = new TitledBorder(border2,"選擇文件所在的路徑"); border3 = BorderFactory.createCompoundBorder(titledBorder1,BorderFactory.createEmptyBorder(1,1,1,1)); border4 = BorderFactory.createLineBorder(new Color(255, 138, 255),1); titledBorder2 = new TitledBorder(border4,"文件讀取進度"); border5 = BorderFactory.createCompoundBorder(titledBorder2,BorderFactory.createEmptyBorder(1,1,1,1)); border6 = BorderFactory.createLineBorder(SystemColor.textText,2); border7 = BorderFactory.createLineBorder(new Color(203, 118, 39),1); titledBorder3 = new TitledBorder(border7,"文件內(nèi)容"); border8 = BorderFactory.createCompoundBorder(titledBorder3,BorderFactory.createEmptyBorder(1,1,1,1)); jLabel1.setFont(new java.awt.Font("Dialog", 0, 16)); jLabel1.setBorder(border1); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jLabel1.setText("一個顯示文件讀取進度的JavaBean實例"); xYLayout1.setWidth(404); xYLayout1.setHeight(365); this.setBackground(SystemColor.info); this.setTitle("一個顯示文件讀取進度的JavaBean實例"); this.setLayout(xYLayout1); jPanel1.setBackground(SystemColor.info); jPanel1.setBorder(border3); jPanel1.setLayout(xYLayout2); jLabel2.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel2.setText("文件路徑:"); jLabel3.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel3.setOpaque(true); jLabel3.setHorizontalAlignment(SwingConstants.LEFT); jLabel3.setHorizontalTextPosition(SwingConstants.LEFT); jLabel3.setText(" (選擇路徑)"); jButton1.setFont(new java.awt.Font("Monospaced", 0, 15)); jButton1.setHorizontalTextPosition(SwingConstants.CENTER); jButton1.setText("..."); jButton1.setVerticalAlignment(SwingConstants.BOTTOM); jButton1.setVerticalTextPosition(SwingConstants.BOTTOM); jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this)); jPanel2.setBackground(SystemColor.info); jPanel2.setBorder(border5); jPanel2.setLayout(xYLayout3); jProgressBar1.setBackground(Color.white); jProgressBar1.setForeground(new Color(0, 255, 151)); jProgressBar1.setBorder(BorderFactory.createLineBorder(Color.black)); jProgressBar1.setDoubleBuffered(true); jProgressBar1.setStringPainted(true); jPanel3.setBackground(SystemColor.info); jPanel3.setBorder(border8); jPanel3.setLayout(xYLayout4); jButton2.setFont(new java.awt.Font("Dialog", 0, 13)); jButton2.setText("運 行"); jButton2.addActionListener(new MainFrame_jButton2_actionAdapter(this)); jButton3.setFont(new java.awt.Font("Dialog", 0, 13)); jButton3.setText("退 出"); jButton3.addActionListener(new MainFrame_jButton3_actionAdapter(this)); jTextArea1.setFont(new java.awt.Font("Dialog", 0, 11)); jTextArea1.setEditable(false); jButton4.setFont(new java.awt.Font("Dialog", 0, 13)); jButton4.setActionCommand("設置進度"); jButton4.setText("參數(shù)設置"); jButton4.addActionListener(new MainFrame_jButton4_actionAdapter(this)); this.add(jPanel1, new XYConstraints(19, 57, 368, 62)); jPanel1.add(jLabel2, new XYConstraints(15, 3, -1, -1)); jPanel1.add(jLabel3, new XYConstraints(81, 3, 218, -1)); jPanel1.add(jButton1, new XYConstraints(310, 1, 36, 22)); this.add(jPanel2, new XYConstraints(21, 131, 365, 53)); jPanel2.add(jProgressBar1, new XYConstraints(15, 0, 326, 22)); this.add(jPanel3, new XYConstraints(23, 199, 360, 115)); jPanel3.add(jScrollPane1, new XYConstraints(12, 0, 328, 84)); jScrollPane1.getViewport().add(jTextArea1, null); this.add(jLabel1, new XYConstraints(18, 13, 366, 27)); this.add(jButton4, new XYConstraints(51, 324, -1, -1)); this.add(jButton3, new XYConstraints(285, 324, -1, -1)); this.add(jButton2, new XYConstraints(171, 324, -1, -1)); this.add(filenamepathbean,null); // 添加FileNamepathBean的實例對象 filenamepathbean.addPropertyChangeListener(this); // 梆定事件監(jiān)聽器 this.add(maxintervaltimebean,null); maxintervaltimebean.addPropertyChangeListener(this); } void jButton1_actionPerformed(ActionEvent e) { /* 調(diào)用FileNamePathBean來獲取文件名和文件路徑 */ filenamepathbean.setFileNameAndPath(); } void jButton3_actionPerformed(ActionEvent e) { /* 退出應用程序 */ System.exit(0); } public void propertyChange(PropertyChangeEvent e) { /* 實現(xiàn)關聯(lián)屬性的propertyChange方法 */ String source = e.getPropertyName(); if(source.equals("fileNameAndPath")) { /* 獲取已經(jīng)改變的屬性值 */ newFileNamePathValue = (String)e.getNewValue(); jLabel3.setText(newFileNamePathValue); } if(source.equals("maxIntervalTimeValue")) { /* 獲取已經(jīng)改變的屬性值 */ newIntervalTimeValue = maxintervaltimebean.getmaxIntervalTimeValue(); } } public Action BeginRunning() { /* 進度的監(jiān)控和文件打印 */ return new AbstractAction() { public void actionPerformed(ActionEvent e) { try { if(TotalLength != 0) { /* 計算進度條的偏移量 */ if(jProgressBar1.getValue() < jProgressBar1.getMaximum()) { jProgressBar1.setValue(jProgressBar1.getValue() + 1); jTextArea1.append(outputStr.substring(i, i + 1)); i++; } else { if(timer != null) { timer.stop(); timer = null; TotalLength = -1; i = 0; str = null; outputStr = null; JOptionPane.showMessageDialog(null,"文件讀取完成!","完成啦!",1); jProgressBar1.setValue(0); jProgressBar1.setMaximum(0); } } } } catch(Exception err) {System.out.println(err);} } }; } void jButton2_actionPerformed(ActionEvent e) { /* 開始顯示文件讀取進度 */ if(newFileNamePathValue.length() > 1 && newFileNamePathValue != null && !(newFileNamePathValue.equals("unknown")) && newIntervalTimeValue != -1) { /* 實現(xiàn)進度監(jiān)控 */ try { TotalLength = 0; file = new BufferedReader(new FileReader(newFileNamePathValue.trim())); while((str = file.readLine()) != null) { /* 讀取文件的 */ outputStr = (outputStr == null) ? str + "\n" : outputStr + "\n" + str; } TotalLength = outputStr.length(); // 文件總長度 jProgressBar1.setMaximum(TotalLength); if(timer == null) { timer = new Timer(newIntervalTimeValue,BeginRunning()); timer.start(); } } catch(IOException err) { JOptionPane.showMessageDialog(null,"文件讀取出錯!","出錯啦!",0); } } else { JOptionPane.showMessageDialog(null,"請正確輸入文件名稱和路徑以及進度條的最大邊界值!","出錯啦!",0); } } void jButton4_actionPerformed(ActionEvent e) { /* 定義進度條最大間隔時間 */ maxintervaltimebean.setmaxIntervalTimeValue(); }}class MainFrame_jButton1_actionAdapter implements java.awt.event.ActionListener{ MainFrame adaptee; MainFrame_jButton1_actionAdapter(MainFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class MainFrame_jButton3_actionAdapter implements java.awt.event.ActionListener{ MainFrame adaptee; MainFrame_jButton3_actionAdapter(MainFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed(e); }}class MainFrame_jButton2_actionAdapter implements java.awt.event.ActionListener{ MainFrame adaptee; MainFrame_jButton2_actionAdapter(MainFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}class MainFrame_jButton4_actionAdapter implements java.awt.event.ActionListener{ MainFrame adaptee; MainFrame_jButton4_actionAdapter(MainFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton4_actionPerformed(e); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -