java主程序里面catch()语句中catch( MyException e)中的MyException下面显示红线错误,跪求解释
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.C...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Shiyan extends JFrame implements CaretListener
{ private static JTextArea text_1;
private static JLabel jl1;
private JLabel jl2;
private JButton bt1,bt2,bt3,bt4;
private JComboBox cbx;
static JPanel pane2=new JPanel(new GridLayout(1,2));
static JPanel pane1=new JPanel(new GridLayout(1,2));
public Shiyan()
{
super("猫狗小虎威");
Container contentPane = getContentPane();
this.setLocation(200,200);
this.setBounds(300,240,360,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(5,1));
text_1=new JTextArea(4,4);
//JPanel pane1=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane1);
//JPanel pane2=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane2);
JPanel pane3=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane3);
JPanel pane4=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane4);
pane2.add(text_1);
JLabel jl1=new JLabel(" ——————异常处理部分——————");
pane1.add(jl1);
JLabel jl2=new JLabel("标签B");
pane2.add(jl2);
bt1=new JButton("插入");
bt1.addActionListener((ActionListener) this);
pane3.add(bt1);
setVisible(true);
JComboBox jc = new JComboBox();
pane3.add(jc);
jc.addItem("英文单词");
setVisible(true);
}
public void actionPerformed(ActionEvent e) throws MyException
{if(e.getSource()==bt1)
if(text_1.getText().equals("123"))
{throw new MyException("错误:输入不可以是123啊");}
}
class MyException extends Exception
{
String message; //定义String类型变量
public MyException(String ErrorMessagr)
{ message = ErrorMessagr;
}
public String getMessage(){ //覆盖getMessage()方法
return message;
}
}
public void caretUpdate(CaretEvent e)
{
// TODO Auto-generated method stub
}
public static void main(String arg[])
{ try
{new Shiyan();
}
catch(MyException e){//System.out.println(e.getMessage());
JOptionPane.showMessageDialog(pane2,jl1+"e.getMessage()");
}
catch(Exception e){System.out.println(e.getMessage());}
finally{}
} 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Shiyan extends JFrame implements CaretListener
{ private static JTextArea text_1;
private static JLabel jl1;
private JLabel jl2;
private JButton bt1,bt2,bt3,bt4;
private JComboBox cbx;
static JPanel pane2=new JPanel(new GridLayout(1,2));
static JPanel pane1=new JPanel(new GridLayout(1,2));
public Shiyan()
{
super("猫狗小虎威");
Container contentPane = getContentPane();
this.setLocation(200,200);
this.setBounds(300,240,360,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(5,1));
text_1=new JTextArea(4,4);
//JPanel pane1=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane1);
//JPanel pane2=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane2);
JPanel pane3=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane3);
JPanel pane4=new JPanel(new GridLayout(1,2));
this.getContentPane().add(pane4);
pane2.add(text_1);
JLabel jl1=new JLabel(" ——————异常处理部分——————");
pane1.add(jl1);
JLabel jl2=new JLabel("标签B");
pane2.add(jl2);
bt1=new JButton("插入");
bt1.addActionListener((ActionListener) this);
pane3.add(bt1);
setVisible(true);
JComboBox jc = new JComboBox();
pane3.add(jc);
jc.addItem("英文单词");
setVisible(true);
}
public void actionPerformed(ActionEvent e) throws MyException
{if(e.getSource()==bt1)
if(text_1.getText().equals("123"))
{throw new MyException("错误:输入不可以是123啊");}
}
class MyException extends Exception
{
String message; //定义String类型变量
public MyException(String ErrorMessagr)
{ message = ErrorMessagr;
}
public String getMessage(){ //覆盖getMessage()方法
return message;
}
}
public void caretUpdate(CaretEvent e)
{
// TODO Auto-generated method stub
}
public static void main(String arg[])
{ try
{new Shiyan();
}
catch(MyException e){//System.out.println(e.getMessage());
JOptionPane.showMessageDialog(pane2,jl1+"e.getMessage()");
}
catch(Exception e){System.out.println(e.getMessage());}
finally{}
} 展开
3个回答
展开全部
(1)既然要让窗口作为自己的监听器,那么看你的程序内容,实现监听器只要实现ActionListener接口就完全可以满足要求啊
(2)虽然CaretListener和ActionListener都继承自超级接口EventListener,但若非要让它实现CaretListener接口,反而不能强制转换成ActionListener(程序不会提示有红线),而添加监听器的方法addActionListener(…)的参数必须是ActionListener
(3)如果你把实现接口改成ActionListener,你会发现接口回调的方法actionPerformed()是不允许抛出异常的,即不允许方法throws,若有异常必须则必须在方法体内try……catch……掉
希望可以帮到你。。。
(2)虽然CaretListener和ActionListener都继承自超级接口EventListener,但若非要让它实现CaretListener接口,反而不能强制转换成ActionListener(程序不会提示有红线),而添加监听器的方法addActionListener(…)的参数必须是ActionListener
(3)如果你把实现接口改成ActionListener,你会发现接口回调的方法actionPerformed()是不允许抛出异常的,即不允许方法throws,若有异常必须则必须在方法体内try……catch……掉
希望可以帮到你。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误:在相应的 try 语句主体中不能抛出异常 Shiyan.MyException,所以也就没有必要catch了。
你只是new Shiyan();调用构造方法的时候不会抛出MyException。
应该直接在你的 actionPerformed里面catch 就不用throws MyException了
你只是new Shiyan();调用构造方法的时候不会抛出MyException。
应该直接在你的 actionPerformed里面catch 就不用throws MyException了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
肯定啊...哪有MyException
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询