⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 java EJB 编程源代码。
💻 JAVA
字号:
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关联属性监听器 */  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;                 // 文件的总行数  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,"文件内容");    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.setBackground(Color.white);    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("参数设置");    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); // 梆定事件监听器    this.add(maxintervaltimebean,null);    maxintervaltimebean.addPropertyChangeListener(this);  }  void jButton1_actionPerformed(ActionEvent e)  {   /* 调用FileNamePathBean来获取文件名和文件路径 */   filenamepathbean.setFileNameAndPath();  }  void jButton3_actionPerformed(ActionEvent e)  {   /* 退出应用程序 */   System.exit(0);  }  public void propertyChange(PropertyChangeEvent e)  {   /* 实现关联属性的propertyChange方法 */   String source = e.getPropertyName();   if(source.equals("fileNameAndPath"))    {     /* 获取已经改变的属性值 */     newFileNamePathValue = (String)e.getNewValue();     jLabel3.setText(newFileNamePathValue);    }   if(source.equals("maxIntervalTimeValue"))    {     /* 获取已经改变的属性值 */     newIntervalTimeValue = maxintervaltimebean.getmaxIntervalTimeValue();    }  }  public Action BeginRunning()  {   /* 进度的监控和文件打印 */   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)    {     /* 实现进度监控 */     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 + -