问答题

本程序实时读取两个文本框中的整数求和。窗口中有三个文本框,分别在前两个文本框中输入整数,则在第三个文本框中显示两个整数的和(如果前两个文本框中任意一个文本框中输入了非整数,则不做运算),如图所示。


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TextFieldTest
{
public static void main (String[] args)
{
TextFieldFrame frame=new TextFieldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TextFieldFrame extends JFrame
{
public TextFieldFrame()
{
setTitle("exam_45");
setSize(WIDTH, HEIGHT);
Container contentPane=getContentPane();
DocumentListener resultListener=______;
JPanel textPanel=new JPanel();
originText=new JTextField("Input Integer 1", 20);
originText.getDocument().addDocumentListener(resultListener);
addText=new JTextField("Input Integer 2", 20);
addText.getDocument().addDocumentListener(resultListener);
resultText=new JTextField("Get the Result", 20);
resultText.setEditable(false);
textPanel.add(originText);
textPanel.add(addText);
textPanel.add(resultText);
contentPane.add(textPanel);
}
public void getAnswer()
{
try
{
int add1, add2, answer;
String s;
add1=Integer.parseInt(originText.getText().trim());
add2=Integer.parseInt(addText.getText().trim());
answer=add1 + add2;
s=new String("=" + answer);
}
catch (NumberFormatException e)
{
}
}
private class ResultListener implements DocumentListener
{
public void insertUpdate(DocumentEvent e)
{
getAnswer();
}
public void removeUpdate(DocumentEvent e)
{
getAnswer();
}
public void changedUpdate(DocumentEvent e)
{
}
}
public static final int WIDTH=350;
public static final int HEIGHT=150;
private JTextField originText;
private JTextField addText;
private JTextField resultText;
}

【参考答案】

第1处:new ResultListener()
第2处:resultText.setText(s)
热门 试题

问答题
本程序中,主窗口中有一个文本框和“开始”、“关闭”两个按钮,单击“开始”按钮后,“开始”按钮处于按下状态,文本框中的数开始从0计数到49,每增加1间隔50毫秒,计数完毕后“开始”按钮变为可使用状态,单击“关闭”按钮则退出程序,如图所示。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class exam_46{public static void main(String[] args){JFrame frame=new CounterFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}class CounterFrame extends JFrame{public static final int WIDTH=250;public static final int HEIGHT=150;private TextField textField=new TextField(20);public CounterFrame(){setSize(WIDTH, HEIGHT);setTitle( exam_46 );Container contentPane=getContentPane();JPanel textPanel=new JPanel();textPanel.add(textField);JPanel buttonPanel=new JPanel();addButton(buttonPanel, 开始 ,new ActionListener(){public void actionPerformed(ActionEvent evt){addCounter();}});addButton (buttonPanel, 关闭 ,new ActionListener(){public void actionPerformed(ActionEvent evt){______;}});contentPane.add(textPanel, BorderLayout.NORTH);contentPane.add(buttonPanel, BorderLayout.SOUTH);}public void addButton(Container c, String title, ActionListenerlistener){JButton button=new JButton(title);c.add(button);button, addActionListener(listener);}public void addCounter(){for (int i=0; i < 50; i++){try{textField.setText(Integer.toString i)) ;______;}catch (InterruptedException e){}}}}
问答题
本程序是一个Applet,页面上有一个“请点击”按钮,单击该按钮后弹出一个对话框,对话框上有三个按钮:“橙色”、“蓝色”和“红色”,单击其中任意一个按钮,则可以将对话框的背景色设置为按钮名称所对应的颜色,如图所示。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class exam_47 extends JApplet{private JFrame frame;______(){frame=new JFrame();frame.setTitle( exam_47 );frame.setSize(300, 200);frame.getContentPane().add(new ButtonPanel());JButton PopButton=new JButton( 请点击 );getContentPane().add(PopButton);PopButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent evt){if (frame.isVisible()) frame.setVisible(false);else______;}});}}class ButtonPanel extends JPanel{private class ColorAction implements ActionListener{private Color backgroundColor;public void actionPerformed(ActionEvent actionevent){setBackground(backgroundColor);repaint();}public ColorAction(Color color){backgroundColor=color;}}public ButtonPanel(){JButton jbutton=new JButton( 橙色 );JButton jbutton1=new JButton( 蓝色 );JButton jbutton2=new JButton( 红色 );add(jbutton);add(jbutton1);add(jbutton2);ColorAction coloraction=new ColorAction(Color.orange);ColorAction coloractionl=new ColorAction(Color.blue);ColorAction coloraction2=new ColorAction(Color.red);jbutton.addActionListener(coloraction);jbuttonl.addActionListener(coloractionl);jbutton2.addActionListener(coloraction2);}exam_47.htmh:<html><head><title>exam_47< title>< head><body><appletcode = exam_47.java width = 100 height = 50 < applet>< body>< html>