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();
}
} 展开
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();
}
} 展开
4个回答
展开全部
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();
}
}
帮你修改了下小地方
你对比下看看吧
希望对你有帮助
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();
}
}
帮你修改了下小地方
你对比下看看吧
希望对你有帮助
追问
你改的什么地方呢?讲一下原理呗???
展开全部
缺少GTK包,跟系统有关系,与软件无关。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
正解
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
{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();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询