本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮:“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框中的文本变成对应的颜色,如果选中“Disable changes”复选项,则三个颜色按钮变为不可用,如果取消选中复选项,则三个按钮变为可用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class java3 extends JFrame{
private JPanel upper,middle,lower;
private JTextField text;
private JButton black,red,green;
private JCheckBox disable;
public java3(String titleText){
super(titleText);
addWindowListener(new WindowAdapter(){
public void
windowClosing(WindowEvent e){
System.exit(0);
}
}
);
upper=new JPanel();
upper.setBorder(BorderFactory.ereateTitledBor-
der("Sample text"));
upper.setlayout(new BorderLayout());
text=new JTextField("Change the color of this
text"):
upper.add(text,BorderLayout.CENTER);
middle=new JPanel();
middle.setBorder(BorderFactory.createTitledBor-
der("Text color control"));
middle.setLayout(new FlowLayout(FlowLayout.
CENTER)):
black=new JButton("Black",new ColorIcon
(Color.black));
black.addActionListener( new ButtonListener
(Color.black));
middle.add(black);
red=new JButton("Red",new ColorIcon(Col-
or.red));
red.addActionListener(new ButtonListener(Col-
or.red));
middle.add(red);
green=new JButton("Green",new ColorIcon
(Color.green));
green.addActionListener(new ButtonListener
(Color.green));
middle.add(green);
lower=new JPanel();
lower.setLayout(new FlowLayout(FlowLayout.
RIGHT));
disable=new JCheckBox("Disable changes"):
disable.addItemListener(new ItemListener()(
public void itemStateChanged(ItemEvent e){
boolean enabled
=(e.getStateChange()
= =ItemEvent.DESELECTED):
black.setEnabled(enabled);
red.setEnabled(enabled);
green.setEnabled(enabled);
}
}
);
lower.add(disable);
Container cp=getContentPane();
cp.add(upper,BorderLayout.NORTH);
cp.add(middle,BorderLayout.CENTER);
cp.add(10wer,BorderLayout.SoUTH);
pack();
setVisible(true);
}
class ButtonListener extends ActionListener{
private Color c;
public ButtonListener(Color c){
this.c=c;
}
public void actionPerformed(ActionEvent e){
text.setForeground(c);
}
}
class ColorIcon implements Icon{
private Color c;
private static final int DIAMETER=10;
public ColorIcon(Color c){
c=c;
}
public void paintlcon(Component cp,Graphics g,
int x,int y){
g.setColor(c);
g.fillOval(X,y,DIAMETER,DIAMETER);
g.setColor(Color.black);
g.drawOval(x,y,DIAMETER,DIAMETER);
}
public int getlconHeight(){
return DIAMETER;
}
public int getlconWidth(){
return DIAMETER;
}
}
public static void main(String[]args){
new java3("advance");
}
}
第1题:
以下各题使用下图,表单名为Form1,表单中有两个命令按钮(Comand1和 Command2)、两个标签、两个文本框(Text1和Text2)。
如果在运行表单时,要使表单的标题栏显示“登录窗口”,则可以在Forml的Load事件中加入语句
A.THISFORM.CAPTION=“登录窗口”
B.FORMl.CAPTION=“登录窗口”
C.THISFORM.NAME=“登录窗口”
D.FORMl.NAME=“登录窗口”
第2题:
( 19 )窗体上有名称为 Command1 的命令按钮和名称为 Text1 的文本框
Private Sub Command1_Click()
Text1.Text= ” 程序设计 ”
Text1.SetFocus
End Sub
Private Sub Text1_GotFocus()
Text1.Text= ” 等级考试 ”
End Sub
运行以上程序,单击命令按钮后
A )文本框中显示的是 “ 程序设计 ” ,且焦点在文本框中
B )文本框中显示的是 “ 等级考试 ” ,且焦点在文本框中
C )文本框中显示的是 “ 程序设计 ” ,且焦点在命令按钮上
D )文本框中显示的是 “ 等级考试 ” ,且焦点在命令按钮上
第3题:
在窗体中有两个文本框分别为 text1 和 text2 ,一个命令按钮 command1, 编写如下两个事件过程:
Private sub command1_click()
A=text1.value+text2.value
Msgbox a
End sub
Private sub form_load()
Text1.value= ””
Text2.value= ””
End sub
程序运行时,在文本框 text1 中 78 ,在文本框 text2 中输入 87 ,单击命令按钮,消息框中输出的结果为____ 11 ___
第4题:
窗体上有名称为Command1的命令按钮和名称为Textl的文本框 Private Sub Command1 Click( ) Text1.Text="程序设计" Text1.SeFocus End Sub Private Sub Text1 GotFocus( ) Text1.Text="等级考试" End Sub 运行以下程序,单击命令按钮后( )。
A.文本框中显示的是"程序设计",且焦点在文本框中
B.文本框中显示的是"等级考试",且焦点在文本框中
C.文本框中显示的是"程序设计",且焦点在命令按钮上
D.文本框中显示的是"等级考试",且焦点在命令按钮上
第5题:
假设一个表单里有一个文本框Text1和一个命令按钮组CommandGroup1,命令按钮组中包含Command1和Command2;两个命令按钮。如果要在Command1命令按钮的某个方法中访问文本框的Value属性值,下列式子中,正确的是
A.ThisForm.Text1.Value
B.ThisForm.Parent.Value
C.Parent.Text1.Value
D.This.Parent.Text1.Value
第6题:
假定一个表单里有一个文本框Text1和Command1、Command2两个命令按钮,如果要在Command1命令按钮的某个方法中访问文本框的Value属性值,下面______式子是正确的是
A.This.ThisForm.Text1.Value
B.ThisForm.Text1.Value
C.Parent.Parent.Text1.Value
D.This.parent.Text1.Value
第7题:
在表单中有命令按钮Commandl和文本框Text1,将文本框的InputMask属性值设置为$9,999.9,然后在命令按钮的Click事件中输入代码ThisForm.Text1.Value=123456.789,当运行表单时,单击命令按钮,此时文本框中显示的内容为______。
A.$123456.789
B.$23456.7
C.123456.7
D.**,***.*
第8题:
在窗体中有两个文本框分别为Textl和Text2,一个命令按钮Cmmand1,编写如下两个事件过程:
Private Sub Commandl_Click()
a=Textl.Value+Text2.Value
MsgBox a
End Sub
Private Sub Form_Load()
TextI.Value=“”
Text2.Value=””
End Sub
程序运行时,在文本框Textl中输入78,在文本框Text2中输入87,单击命令按钮,消息框中输出的结果为【 】。
第9题:
在窗体上添加两个文本框和一个按钮,在按钮的单击事件中添加如下代码:
Private sub command1_Click( )
Text1="vba test"
Text2=text1
Text1= "test end"
End sub
单击按钮后,text2中的文本内容是【 】。
第10题:
设有如右图所示的窗体和下列程序:
Private Sub Command1_Click() Text1.Text="Visual Basic" End Sub Private Sub Text1_LostFocus() If Text1.Text<>"BASIC" Then Text1.Text=" " Text1.SetFocus End If End Sub程序运行时,在Text1文本框中输入“Basic”(如图所示),然后单击Command1按钮,则产生的结果是( )。
A.文本框中无内容,焦点在文本框中
B.文本框中为"Basic",焦点在文本框中
C.文本框中为"Basic",焦点在按钮上
D.文本框中为"Visual Basic",焦点在按钮上