河南成教 / Java程序设计
[填空题] 某程序的界面包含了两个按钮button1和button2,一个文本框text。要求实现当点击button1时,文本框中显示“请认真做题!”;当点击button2时,文本框中内容消失。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Class2801 extends JFrame
{
JButton butt1,butt2;
JTextField text1;
JFrame myWin;
public Class2801(String s)
{
myWin=new JFrame(s);
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时,整个应用程序关闭。若省略,关闭窗口时程序不终止,相当于setVisible(false)
Container con=myWin.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
butt1=new JButton("b1");
butt2=new JButton("b2");
butt1.addActionListener(this);
butt2.addActionListener(this);
con.add(butt1);con.add(butt2);
text1=new JTextField(20);
con.add(text1);
myWin.pack();
myWin.setVisible(true);
}
public static void main(String[] args)
{
Class2801 my=new Class2801("Class2801");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butt1)
text1.setText("请认真做题!");
else
;
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Class2801 extends JFrame
{
JButton butt1,butt2;
JTextField text1;
JFrame myWin;
public Class2801(String s)
{
myWin=new JFrame(s);
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时,整个应用程序关闭。若省略,关闭窗口时程序不终止,相当于setVisible(false)
Container con=myWin.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
butt1=new JButton("b1");
butt2=new JButton("b2");
butt1.addActionListener(this);
butt2.addActionListener(this);
con.add(butt1);con.add(butt2);
text1=new JTextField(20);
con.add(text1);
myWin.pack();
myWin.setVisible(true);
}
public static void main(String[] args)
{
Class2801 my=new Class2801("Class2801");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butt1)
text1.setText("请认真做题!");
else
;
}
}
参考答案:
佳题速递: