java下面自定义异常类点击按钮后,无法正常显示异常对话框,跪求为什么

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 ActionListener
{ 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);

}
class MyException extends Exception
{
String message; //定义String类型变量
public MyException(String ErrorMessagr)
{ message = ErrorMessagr;
}
public String getMessage(){ //覆盖getMessage()方法
return message;
}
}

public void actionPerformed(ActionEvent e) throws MyException
{if(e.getSource()==bt1)
if(text_1.getText().equals("123"))
{ try
{ throw new MyException("错误:输入不可以是123啊");}
catch(MyException e1){
JOptionPane.showMessageDialog(pane2,jl1+"e1.getMessage()");}
}
}

public void caretUpdate(CaretEvent e)
{
// TODO Auto-generated method stub

}

public static void main(String arg[])
{
new Shiyan();

}
}
展开
 我来答
邗启3V
2012-07-14 · TA获得超过7271个赞
知道大有可为答主
回答量:1413
采纳率:100%
帮助的人:1393万
展开全部
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
public class Shiyan extends JFrame implements ActionListener {
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);
}
class MyException extends Exception {
String message; // 定义String类型变量
public MyException(String ErrorMessagr) {
// super(ErrorMessagr);
message = ErrorMessagr;
}
public String getMessage() { // 覆盖getMessage()方法
return message;
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt1)
if (text_1.getText().equals("123")) {
try {
throw new MyException("错误:输入不可以是123啊");
} catch (MyException e1) {
JOptionPane.showMessageDialog(pane2, e1.getMessage());
}
}
}
public void caretUpdate(CaretEvent e) {
// TODO Auto-generated method stub
}
public static void main(String arg[]) {
new Shiyan();
}
}

帮你修改了下小地方
你对比下看看吧
希望对你有帮助
追问
你改的什么地方呢?讲一下原理呗???
283233407
2012-07-14 · TA获得超过661个赞
知道小有建树答主
回答量:352
采纳率:0%
帮助的人:206万
展开全部
缺少GTK包,跟系统有关系,与软件无关。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
shenbeiaia521
2012-07-14 · TA获得超过178个赞
知道答主
回答量:329
采纳率:100%
帮助的人:209万
展开全部
正解
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zhya1234
2012-07-14
知道答主
回答量:10
采纳率:0%
帮助的人:4.1万
展开全部
public void actionPerformed(ActionEvent e) throws MyException
{if(e.getSource()==bt1)
if(text_1.getText().equals("123"))
{ try
{ throw new MyException("错误:输入不可以是123啊");}
catch(MyException e1){
JOptionPane.showMessageDialog(pane2,jl1+"e1.getMessage()");}
}
}

把 throws MyException去掉,或者把这里的try catch 去掉

你是说不能显示对话框 还是不能显示异常

如果是后者:你没加e1.printStackTrace();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式