关于IOException异常类
我引入了java.io.*包 ,我想通过“throws”抛出这个异常,但我把main函数单独的定义在一个类里,其他的操作都放在另外的一个类里(两个类在同一个包),这种情况下我在main 函数中用“throws”抛出异常能行吗(我试了一下 好像不行 但我是新手 可能在哪个地方有错也不知 所以也不能确定) 如果这样真的不行 那该怎么办???(我不想用try...catch捕获) 望高手指点----------------解决方案--------------------------------------------------------
你把代码贴出来...
----------------解决方案--------------------------------------------------------
package ChapterPak;
public class MyMain {
public static void main(String argv[]){
Login login = new Login("登陆");
}
}
----------------解决方案--------------------------------------------------------
package ChapterPak;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
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);
}
}
----------------解决方案--------------------------------------------------------
这是一部分
----------------解决方案--------------------------------------------------------
还有 随便帮我看看为什么我把stringBuffer字符串中的内容放到textAreal中用 textAreal.setText(stringBuffer); 不行
----------------解决方案--------------------------------------------------------
Login是你自己定义的吗?
----------------解决方案--------------------------------------------------------
是 的
----------------解决方案--------------------------------------------------------
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("");
}
}
}
----------------解决方案--------------------------------------------------------