问题 问答题

本题的功能是展示4种不同的对话框。窗口中有4个按钮:“消息”、“输入”、“确定”和“选择”,单击任意一个按钮,就能弹出一个对应的对话框。其中,消息对话框只有一个提示信息和一个“确定”按钮,输入对话框有一个供输入的文本框及“确定”和“撤销”两个按钮;确定对话框中有一个提示信息和三个按钮“是”、“否”和“撤销”;而选择对话框中有一个提示信息和两个按钮“确定”和“取消”。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class java3 extends JFrame implements ButtonSelecteActionListener

JButton btnMessage=new JButton("消息");
JButton btnInput=new JButton("输入");
JButton btnConfirm=new JButton("确认");
JButton btnOption=new JButton("选择");
public java3()

super("java3");
btnMessage.addActionListener(this);
btnInput.addActionListener(this);
btnConfirm.addActionListener(this);
btnOption.addActionListener(this);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(btnMessage);
getContentPane().add(btnInput);
getContentPane().add(btnConfirm);
getContentPane().add(btnOption);
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);

);

public static void main(String args[])

java3 fr=new java3();
fr.pack();
fr.setVisible(true);

Public void actionperformed(ActionEvent e)

Object[]opt="确认","取消";
JButton instance=(JButton)e.getObject();
if(instance==btnMessage)
JOptionPane.sbowMessageDialog(this,"消息对话框");
else if(instance==btnInput)
JOptionPane.showInputDialog(this,"输入对话框");
else if(instance==btnConfirm)
JOptionPane.showConfirmDialog(this,"确认对话框");
else
JOptionPane.showOptionDialog(this,"选择对话框","选择",JOptionPane.YES_OPTION,JOptionPane,QUESTION_MESSAGE,null,opt,opt[1]);

答案

参考答案:第1处:extends JFrame implements ActionListener
第2处:public void actionPerformed(ActionEvent e)
第3处:JButton instantce=(JButton)e.getSource()

解析: 第1处是实现与ActionEvent事件对应的接口,使之能够处理ActionEvent事件,相应的接口应为ActionListener;第2处是actionPerformed方法通过读取ActionEvent对象的相关信息来得到事件发生时的情况,Java是大小写敏感的;第3处是在Java的事件类中java.util.EventObject类是所有事件对象的基础父类,通过getSource()方法可以得到事件源对象。

选择题
选择题