问答题

本程序的功能是获得对话框的输入。主窗口中有一个只读的文本域和一个“输入内容”按钮,单击该按钮后弹出一个对话框,对话框中有一个文本框和两个按钮:“确定”和“取消”,在文本框中输入内容,如果单击“确定”按钮后返回主窗口,则把文本框中输入的内容添加到主窗口的文本域中,如果单击“取消”按钮则直接返回主窗口,如图所示。


import java.awt.*;
import java.awt.event.*;
public class exam6 extends Frame implements ActionListener {
boolean inAnApplet=true;
private SimpleDialog dialog;
private TextArea textArea;
String newline;
public exam_6() {
textArea=new TextArea(5, 40);
textArea.setUnEditable(true);
add("Center", textArea);
Button button=new Button("输入内容");
button.addActionListener(this);
Panel panel=new Panel();
panel.add(button);
add("South", panel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
setVisible(false);
dispose();
} else {
System.exit(0);
}
}
});
newline=System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event) {
if (dialog == null) {
dialog=new SimpleDialog(this,"exam_6");
}
dialog.setVisible(true);
}
public void setText(String text) {
textArea.append(text + newline);
}
public static void main(String args[]) {
exam_6 window=new exam_6();
window.inAnApplet=false;
window.setTitle("exam_6");
window.pack();
window.setVisible(true);
}
}
class SimpleDialog extends Dialog implements ActionListener {
TextField field;
exam_6 parent;
Button setButton;
SimpleDialog(Frame dw, String title) {
super(dw, title, false);
parent=(exam_6)dw;
Panel p1=new Panel();
Label label=new Label ("请输入内容: ");
p1.add(label);
field=new TextField(40);
field.addActionListener();
p1.add(field);
add("Center", p1);
Panel p2=new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
Button b=new Button("取消");
b.addActionListener(this);
setButton=new Button("确定");
setButton.addActionListener(this);
p2.add(b);
p2.add(setButton);
add("South", p2);
pack();
}
public void actionPerformed(ActionEvent event) {
Object source=event.getSource();
if ( (source == setButton)
| (source == field)) {
parent.setText(field.getText());
}
field.selectAll();
setVisible(true);
}

【参考答案】

第1处:textArea.setEditable(false)
第2处:field.addActionList......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
本程序的功能是监听对于列表项的操作。窗口中有一个列表和三个按钮:“添加”、“删除”和“关闭”。单击“添加”按钮,则会在当前所选列表项后添加一个名为“新增表项”的列表项,同时后台输出列表中的表项数量。单击“删除”按钮后,如果未选中表项,则弹出提示消息框“请选择表项”,否则将选中的表项删除,同时后台输出删除表项的内容和列表中的表项数量。单击“关闭”按钮退出程序,如图所示。import java.awt.*;import java.awt.event.*;import javax.swing. JOptionPane;public class exam_5{public static void main (String args[]){final Frame frmFrame=new Frame();Panel pnlPanel1=new Panel();Panel pnlPanel2=new Panel();final List istList=new List(8);for( int i=0; i<10; i++ ){String strName= 表项 + (new Integer(i+1)).toString();lstList.add( strName );}Button btnButton1=new Button( 添加 );Button btnButton2=new Button( 删除 );Button btnButton3=new Button( 关闭 );btnButton1.addActionListener( new ActionListener(){public void actionPerformed(ActionEvent e){lstList.add( 新增表项 , lstList.qetSelected()+1);System.out.println( 列表中的表项数量: + lstList.getItemCount() );}});btnButton2.addActionListener( new ActionListener(){public void actionPerformed(ActionEvent e){if(lstList.getSelected()__==_null_){JOptionPane.showMessageDialog(frmFrame, 请选择表项 );return;}System. out.println ( 删除表项的内容: +lstList.getSelectedItem());lstList.delete(lstList.getSelectedIndex()__);System. out. println ( 列表中的表项数量: + lstList, getItemCount ());}});btnButton3, addActionListener ( new ActionListener (){public void actionPerformed(ActionEvent e){System.exit (0);}});pnlPanel1, add (lstList);pnlPanel2, add (btnButton1);pnlPanel2, add (btnButton2);pnlPanel2, add (btnButton3);frmFrame, add ( North , pnlPanel1;frmFrame.add( South , pnlPanel2;frmFrame, setTitle ( exam_5 );frmFrame, pack ( );frmFrame, show ( );}}
问答题
本程序的功能是展示四种不同的对话框。窗口中有四个按钮:“消息”、“输入”、“确认”和“选择”,单击任意一个按钮,就能弹出一个对应的对话框。其中,“消息”对话框只有一个提示信息和一个“确定”按钮,“输入”对话框中有一个供输入的文本框和“确定”和“撤销”两个按钮,“确认”对话框中有一个提示信息和“是”、“否”和“撤销”三个按钮,而“选择”对话框中有一个提示信息和“确定”和“取消”两个按钮,如图所示。import javax.swing.*;import java.awt.event.*;import java.awt.*;public class exam_7 extends JFrame implements ButtonSelectedActionListener{JButton btnMessage=new JButton( 消息 );JButton btnInput =new JButton( 输入 );JButton btnConfirm=new JButton( 确认 );JButton btnOption =new JButton( 选择 );public exam_7(){super( exam_7 );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[]){exam_7 fr=new exam_7 ();fr.pack();fr. setVisible (true);}public void actionperformed(ActionEvent e){Object[] opt= { 确认 , 取消 };JButton instance=(JButton)e.getObject();if( instance == btnMessage )JOptionPane.showMessageDialog(this, 消息对话框 );else if( instance == btnInput )JOptionPane.showInputDialog(this, 输入对话框 );else if( instance == btnConfirm )JOptionPane.showConfirmDialog(this, 确认对话框 );elseJOptionPane.showOptionDialog(this, 选择对话框 , 选择 ,JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,opt,opt[1]);}}