| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2729 人关注过本帖
标题:关于IOException异常类
只看楼主 加入收藏
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
 问题点数:0 回复次数:8 
关于IOException异常类
我引入了*包 ,我想通过“throws”抛出这个异常,但我把main函数单独的定义在一个类里,其他的操作都放在另外的一个类里(两个类在同一个包),这种情况下我在main 函数中用“throws”抛出异常能行吗(我试了一下 好像不行 但我是新手 可能在哪个地方有错也不知 所以也不能确定) 如果这样真的不行 那该怎么办???(我不想用try...catch捕获) 望高手指点
搜索更多相关主题的帖子: IOException main java throws 函数 
2008-04-12 14:42
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你把代码贴出来...

学习需要安静。。海盗要重新来过。。
2008-04-12 14:49
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
得分:0 
package ChapterPak;

public class MyMain {
public static void main(String argv[]){
    Login login = new Login("登陆");
}
}
2008-04-12 14:56
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
得分:0 
package ChapterPak;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;

import *;

public class MainFrame extends JFrame{
  JTabbedPane tabbedPane = new JTabbedPane();
  JTextArea  textAreal = new JTextArea();
  JScrollPane scrollPane = new JScrollPane(textAreal,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  JPanel panel= new JPanel();
  JMenuBar menuBar;
  JMenu menu;
  JMenuItem menuItem;

  Login login;
   public MainFrame(Login ll)
  {
     this.login = ll;

     this.setTitle("标签面板显示");

     menuBar = new JMenuBar();
     this.setJMenuBar(menuBar);

     menu = new JMenu("文件");
     menuBar.add(menu);
     menuItem = new JMenuItem("打开");
     menuItem.addActionListener(new ActionHandler_1(this));
     menu.add(menuItem);
     menuItem = new JMenuItem("新建");
     menu.add(menuItem);
     menuItem = new JMenuItem("退出");
     menuItem.addActionListener(new ActionHandler_1(this));
     menu.add(menuItem);

     menu = new JMenu("帮助");
     menuBar.add(menu);
     menuItem = new JMenuItem("关于");
     menu.add(menuItem);
     menuItem = new JMenuItem("说明");
     menu.add(menuItem);


     tabbedPane.addTab("第一页",null,scrollPane,"这是第一页");
     tabbedPane.addTab("第二页",null,panel,"这是第二页");
     this.getContentPane().add(tabbedPane);

     this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
     this.addWindowListener(new WindowHandler(this));
     this.setSize(800,800);
     this.setLocationRelativeTo(null);
     this.setVisible(true);
  }
  public void WindowClosing(WindowEvent e)
  {
    int rr = JOptionPane.showConfirmDialog(null,"你真的要退出吗??","确认框",JOptionPane.YES_NO_OPTION);
    if( rr == JOptionPane.YES_OPTION)
    {
        login.text_1.setText("");
        login.passwordField.setText("");
        this.hide();
        login.loginFrame.show();
    }
  }
  public void actionHandler(ActionEvent e)
  {
    StringBuffer stringBuffer = new StringBuffer();

    if(e.getActionCommand().equals("打开"))
    {
       JFileChooser fileChoose = new JFileChooser();
       int rt = fileChoose.showOpenDialog(null);
       if(rt == JFileChooser.APPROVE_OPTION)
       {
           FileReader fileReader;
          try{
            File f = fileChoose.getSelectedFile();
            fileReader = new FileReader(f);
            int c;
            c = fileReader.read();
            while(c != -1)
            {
                  stringBuffer.append(String.valueOf(c));
                  c = fileReader.read();
            }
            textAreal.setText("stringBuffer");
          }
          catch(Exception e1)
          {
            JOptionPane.showMessageDialog(null,"没有找到文件");
          }
          System.out.println(stringBuffer);
      //    fileReader.close();
       }

    }
    else if(e.getActionCommand().equals("退出"))
    {
       int rr = JOptionPane.showConfirmDialog(null,"你真的要退出麻??",
                                              "确认框",JOptionPane.YES_NO_OPTION);
       if(rr == JOptionPane.YES_OPTION)
       {
         login.text_1.setText("");
         login.passwordField.setText("");
         this.hide();
         login.loginFrame.show();
       }
   }

  }
}

class WindowHandler extends WindowAdapter
{
    MainFrame mf;
    public WindowHandler(MainFrame mf)
    {
       this.mf = mf;
    }
    public void windowClosing(WindowEvent e)
    {
         mf.WindowClosing(e);
    }
}

 class ActionHandler_1 implements ActionListener
 {
    MainFrame m;
    public ActionHandler_1(MainFrame m)
    {
       this.m = m;
    }
    public void actionPerformed(ActionEvent e)
    {
       m.actionHandler(e);
    }
 }
2008-04-12 14:57
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
得分:0 
这是一部分
2008-04-12 14:58
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
得分:0 
还有 随便帮我看看为什么我把stringBuffer字符串中的内容放到textAreal中用           textAreal.setText(stringBuffer); 不行
2008-04-12 15:01
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
Login是你自己定义的吗?

学习需要安静。。海盗要重新来过。。
2008-04-12 15:14
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
得分:0 
是 的
2008-04-12 15:24
翔影
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-7
收藏
得分:0 
package ChapterPak;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;

public class Login{
  JFrame loginFrame;
  JPanel panel = new JPanel();
  JPanel panel_1 = new JPanel();
  JPanel panel_3 = new JPanel();
  JPanel panel_2 = new JPanel();
  JLabel label_1,label_2;
  JTextField text_1;
  JPasswordField passwordField;
  JButton button_1,button_2;

  public Login(String title)
  {
    loginFrame = new JFrame(title);
   /* loginFrame.getContentPane().add(panel_1);
    loginFrame.getContentPane().add(panel_2);
    loginFrame.getContentPane().add(panel_3);*/


    label_1 = new JLabel("用户名:");
    label_2 = new JLabel("密码:");
    BoxLayout boxlayout_1 = new BoxLayout(panel_1,BoxLayout.PAGE_AXIS);
    panel_1.setLayout(boxlayout_1);
    panel_1.add(label_1);
    panel_1.add(Box.createRigidArea(new Dimension(0,10)));
    panel_1.add(label_2);

    text_1 = new JTextField(20);
    passwordField = new JPasswordField(20);
    BoxLayout boxlayout_2 = new BoxLayout(panel_2,BoxLayout.PAGE_AXIS);
    panel_2.setLayout(boxlayout_2);
    panel_2.add(text_1);
    panel_2.add(Box.createRigidArea(new Dimension(0,10)));
    panel_2.add(passwordField);

    button_1 = new JButton("确定");
    button_2 = new JButton("取消");
    button_1.addActionListener(new ActionHandler(this));
    button_2.addActionListener(new ActionHandler(this));
    BoxLayout boxlayout_3 = new BoxLayout(panel_3,BoxLayout.LINE_AXIS);
    panel_3.setLayout(boxlayout_3);
    panel_3.add(button_1);
    panel_3.add(button_2);

    panel.add(panel_1,BorderLayout.WEST);
    panel.add(panel_2,BorderLayout.EAST);
    panel.add(panel_3,BorderLayout.CENTER);

    loginFrame.getContentPane().add(panel);

   // loginFrame.pack();
    loginFrame.setSize(new Dimension(300,150));
    loginFrame.setDefaultCloseOperation(loginFrame.EXIT_ON_CLOSE);
    loginFrame.setLocationRelativeTo(null);
    loginFrame.setVisible(true);
  }
  public void OkButton(Login login)
  {
   //  Login ll;
     if(this.text_1.getText().equals("destiny") && this.passwordField.getText().equals("123"))
     {
           JOptionPane.showMessageDialog(null,"欢迎进入**系统");
           loginFrame.hide();
           MainFrame mainFrame = new MainFrame(login);
     }
     else
     {
        JOptionPane.showMessageDialog(null,"密码或帐号错误","提示框",JOptionPane.INFORMATION_MESSAGE);
        text_1.setText("");
        passwordField.setText("");
     }
  }
}

class ActionHandler implements ActionListener
{
    Login l;
    public ActionHandler(Login l)
    {
       this.l = l;
    }
    public void actionPerformed(ActionEvent e)
    {
       if(e.getSource() == l.button_1)
       {
           l.OkButton(l);
       }

       else if(e.getSource() == l.button_2)
       {
           l.text_1.setText("");
           l.passwordField.setText("");
       }
    }
}
2008-04-12 15:30
快速回复:关于IOException异常类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019318 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved