import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JOptionPanelDemo implements ActionListener {
JFrame frame=new JFrame("JOptionPane Demo");
JTextField tf=new JTextField();
JButton messageButton,ConfirmButton,InputButton,OptionButton;

public static void main(String args[]){
JOptionPanelDemo opd=new JOptionPanelDemo();
opd.go();

}
public void go() {
messageButton=new JButton("Message Dialog");
messageButton.addActionListener(this);
ConfirmButton=new JButton("ConfirmButton Dialog");
ConfirmButton.addActionListener(this);
InputButton=new JButton("InputButton Dialog");
InputButton.addActionListener(this);
OptionButton=new JButton("OptionButton Dialog");
OptionButton.addActionListener(this);

JPanel jp=new JPanel();
jp.add(messageButton);
jp.add(ConfirmButton);
jp.add(InputButton);
jp.add(OptionButton);

Container cp=frame.getContentPane();
cp.add(jp,BorderLayout.CENTER);
cp.add(tf,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton button=(JButton)e.getSource();
//信息对话框
if(button==messageButton) {
JOptionPane.showMessageDialog(frame,"Field not found.","An error",JOptionPane.ERROR_MESSAGE);
}
//确认对话框
if(button==ConfirmButton) {
int select=JOptionPane.showConfirmDialog(frame,"Create one","Confirm",JOptionPane.YES_NO_OPTION);
if(select==JOptionPane.YES_OPTION) {
tf.setText("Choose YES");
if(select==JOptionPane.NO_OPTION) {
tf.setText("Choose NO");
if(select==JOptionPane.CLOSED_OPTION) {
tf.setText("Choose CLOSE");
}
//输入对话框
if(button==InputButton) {
Object[] possibleValues={"First","Second","Third"};
Object selectedValue=JOptionPane.showInputDialog(frame,
"Choose one","Input",JOptionPane.INFORMATION_MESSAGE,null,
possibleValues,possibleValues[0]);
if(selectedValue!=null)
tf.setText(selectedValue.toString());
else
tf.setText("Closed");

}
//选项对话框
if(button==OptionButton) {
Object[]options={"OK","CANCEL"};
int select1=JOptionPane.showOptionDialog(frame,"Click OK to continue",
"Warning",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,
null,options,options[0]);
if(select1==0) 
tf.setText("choose OK");
else if(select1==1)
tf.setText("choose CANCEL");
else if(select1==-1)
tf.setText("Closed");

}
}
}

解决方案 »

  1.   

    少了3个}
    if(select==JOptionPane.YES_OPTION) {
                tf.setText("Choose YES");
    }
            if(select==JOptionPane.NO_OPTION) {
                tf.setText("Choose NO");
    }
            if(select==JOptionPane.CLOSED_OPTION) {
                tf.setText("Choose CLOSE");
    }
      

  2.   

    大括号不对应。你把代码放到Eclipse里面就能很明显的看到格式或者简单逻辑哪儿有错误了。
      

  3.   

    大哥 小弟非常佩服你写的代码 都是写什么啊 我要是你的项目经理肯定把你丢了!!!
    括号全错了!你那IF语句里面虽然只有一句语句 希望以后你也能加上括号!!!
    正确的代码如下:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JOptionPanelDemo implements ActionListener {
        JFrame frame=new JFrame("JOptionPane Demo");
        JTextField tf=new JTextField();
        JButton messageButton,ConfirmButton,InputButton,OptionButton;
        
        public static void main(String[] args){
            JOptionPanelDemo opd=new JOptionPanelDemo();
            opd.go();        
        }public void actionPerformed(ActionEvent e) {
         JButton button=(JButton)e.getSource();
         //信息对话框
        if(button==messageButton) {
            JOptionPane.showMessageDialog(frame,"Field not found.","An error",JOptionPane.ERROR_MESSAGE);
        }
        //确认对话框
        if(button==ConfirmButton) {
            int select=JOptionPane.showConfirmDialog(frame,"Create one","Confirm",JOptionPane.YES_NO_OPTION);
            if(select==JOptionPane.YES_OPTION)
                tf.setText("Choose YES");
            if(select==JOptionPane.NO_OPTION)
                tf.setText("Choose NO");
            if(select==JOptionPane.CLOSED_OPTION)
                tf.setText("Choose CLOSE");
        }
    //输入对话框
        if(button==InputButton) {
            Object[] possibleValues={"First","Second","Third"};
            Object selectedValue=JOptionPane.showInputDialog(frame,
                "Choose one","Input",JOptionPane.INFORMATION_MESSAGE,null,
                possibleValues,possibleValues[0]);
        if(selectedValue!=null){
            tf.setText(selectedValue.toString());
            }else{
            tf.setText("Closed"); 
            }
        }
        //选项对话框
        if(button==OptionButton) {
            Object[]options={"OK","CANCEL"};
            int select1=JOptionPane.showOptionDialog(frame,"Click OK to continue",
                    "Warning",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,
                    null,options,options[0]);
            if(select1==0) {
                tf.setText("choose OK");
            }else if(select1==1){
               tf.setText("choose CANCEL");
            } else if(select1==-1){
                tf.setText("Closed");
            }
        }
    }
    public void go() {
        messageButton=new JButton("Message Dialog");
        messageButton.addActionListener(this);
        ConfirmButton=new JButton("ConfirmButton Dialog");
        ConfirmButton.addActionListener(this);
        InputButton=new JButton("InputButton Dialog");
        InputButton.addActionListener(this);
        OptionButton=new JButton("OptionButton Dialog");
        OptionButton.addActionListener(this);
        
        JPanel jp=new JPanel();
        jp.add(messageButton);
        jp.add(ConfirmButton);
        jp.add(InputButton);
        jp.add(OptionButton);
        
        Container cp=frame.getContentPane();
        cp.add(jp,BorderLayout.CENTER);
        cp.add(tf,BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    }
      

  4.   

    if(button==ConfirmButton) {
            int select=JOptionPane.showConfirmDialog(frame,"Create one","Confirm",JOptionPane.YES_NO_OPTION);
            if(select==JOptionPane.YES_OPTION) {
                tf.setText("Choose YES");
            if(select==JOptionPane.NO_OPTION) {
                tf.setText("Choose NO");
            if(select==JOptionPane.CLOSED_OPTION) {
                tf.setText("Choose CLOSE");
        }少了}1楼眼神真好啊