JOptionPane.showMessageDialog(this,"测试","警告");

解决方案 »

  1.   

    public void actionPerformed(ActionEvent e){
       new JOptionPane().showMessageDialog(this,"测试","警告");
       System.exit(0);
     }
      

  2.   

    下面的代码是没有问题的.
    import java.awt.*;
    import java.awt.event.*;import javax.swing.*;public class Untitled2 extends JFrame implements ActionListener {
       private JButton jButton1 = new JButton();
       public void actionPerformed(ActionEvent e){
          JOptionPane.showMessageDialog(this,"测试","警告",JOptionPane.ERROR_MESSAGE);
          return;   }
       public Untitled2() throws HeadlessException {
          try {
             jbInit();
          }
          catch(Exception e) {
             e.printStackTrace();
          }
       }
       public static void main(String[] args) throws HeadlessException {
          Untitled2 untitled21 = new Untitled2();
          untitled21.setSize(300,300);
          untitled21.show();
       }
       private void jbInit() throws Exception {
          jButton1.setText("OK");
          jButton1.addActionListener(this);
          this.getContentPane().add(jButton1, BorderLayout.CENTER);
       }
    }
      

  3.   

    to:xmagicwu(死过方生) 不行呀,那岂不整个程序都退出了吗?
      

  4.   

    to:: helpall() ( 在JFrame中用是没问题,但是你在JDialog中用用看
      

  5.   

    改动如下:
    class A extends JFrame{
     public static void main(String[] args){
       JDialog B=new JmyDialog(this);
       B.show();
     }
    }
    class JmyDialog extends JDialog implenents ActionListener{
     JButton jButton;
     Frame parent;
     public B(Frame _parent){
      super(_parent);
    //   一些布局初始化
      jButton=new JButton("弹出消息");
      jButton.addActionListener(this);
     }
     
     public void actionPerformed(ActionEvent e){
       JOptionPane.showMessageDialog(parent,"测试","警告");//这里的this应该是Frame才对
       return;
    }
      

  6.   

    import javax.swing.JFrame;
    import javax.swing.JDialog;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import java.awt.event.ActionEvent;
    public class A extends JFrame{
     public static void main(String[] args){
       JDialog b=new B();
       b.show();
     }
    }
    class B extends JDialog implements ActionListener{
     JButton jButton;
     public B(){
    //   一些布局初始化
         this.setBounds(100,100,200,200);
      jButton=new JButton("弹出消息");
      jButton.addActionListener(this);
      this.getContentPane().add(jButton);
     } public void actionPerformed(ActionEvent e){
       JOptionPane.showMessageDialog(this,"测试");
     }
    }
    我试了,没问题的
      

  7.   

    在JFrame中用是没问题,但是你在JDialog中用用看
    ===>很幸运,还是没有问题.
      

  8.   

    geyf(我在学java) :你的方法我也尝试过,不行的
      

  9.   

    你那是不是有多个线程啊,如果是,
    需要用SwingUtilities.invokeLater()之类的方式来显示你的对话框
      

  10.   

    to  geyf(我在学java) 不是多线程,就是简单的一个Frame显示一个Dialog,然后Dialog用JOptionPane.showMessage...
    显示一个消息对话框,但是按消息对话框的那个确定按钮之后,消息框不消息,但是后面的语句都执行了,怀疑和UI重新paint有关!!!
      

  11.   

    我碰到的问题是:按钮事件中用setJMenuBar(jmnUser)后,显示还是原来的菜单,要resize以后才能正确显示。
    请问如果是repaint问题如何解决?
      

  12.   

    import javax.swing.*;
    import java.awt.event.*;public class A extends JFrame{
      JButton jButton  =new JButton("弹出消息");
      public A(){
        this.getContentPane().add(jButton);
        jButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            JDialog b = new B();
            b.show();
          }
        });
      }
     public static void main(String[] args){
       A a=new A();
       a.setSize(400,300);
       a.show();
     }
    }
    class B extends JDialog implements ActionListener{
     JButton jButton;
     public B(){
    //   一些布局初始化
      jButton=new JButton("弹出消息");
      this.getContentPane().add(jButton);
      jButton.addActionListener(this);
     } public void actionPerformed(ActionEvent e){
       new JOptionPane().showMessageDialog(null,"测试","警告",JOptionPane.OK_CANCEL_OPTION);
       return;
     }
    }