读着没有发现问题,可是编译不了,不知道哪错了,求解。

sunwxb 2009-10-21 07:13:31
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");

}
}
}
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl3450341 2009-10-21
  • 打赏
  • 举报
回复
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楼眼神真好啊
yanenyi1987 2009-10-21
  • 打赏
  • 举报
回复
大哥 小弟非常佩服你写的代码 都是写什么啊 我要是你的项目经理肯定把你丢了!!!
括号全错了!你那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);
}
}
hardycheng 2009-10-21
  • 打赏
  • 举报
回复
大括号不对应。



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



justinavril 2009-10-21
  • 打赏
  • 举报
回复
哈哈哈 有写轮眼果然不一样
swandragon 2009-10-21
  • 打赏
  • 举报
回复
少了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");
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧