1 编写 Application 程序,在 Frame 中加
入 80 个 按 钮 , 分 20 行 4 列 , 用
GridLayout 布局方式,按钮背景为黄色
(Color.yellow) , 按 钮 文 字 颜 色 为 红 色
(Color.red)。
import java.awt.event.*;
import java.awt.*;
public class sy75 extends Frame
implements ActionListener
{
Button []But;
sy75()
{
setLayout(new
GridLayout(20,4,10,10));
But=new Button[80];
for(int i=0;i<80;i++)
{
setBackground(Color.yellow);
But[i]=new Button(" 按
钮");
setForeground(Color.red);
But[i].addActionListener(this);
add(But[i]);
}
addWindowListener(new
koWindowListener());
setVisible(true);
}
public void
actionPerformed(ActionEvent e){}
public static void main(String[] args)
{sy75 but=new sy75();}
}
class koWindowListener extends
WindowAdapter
{
public void
windowClosing(WindowEvent e)
{e.getWindow().dispose();
System.exit(0);}}
2.设计一个简单计算器,如下图所示。在
“操作数”标签右侧的两个文本框输入操作数
当单击操作符+,-,×,÷按钮时,对两
个操作数进行运算并将结果填入到“结果”标
签右侧的文本框中。
import java.awt.event.*;
import java.awt.*;
public class sy76 extends Frame
implements
ActionListener,TextListener
{
TextField tf1,tf2,tf3;
Label lab1,lab2,lab3;
Button But1,But2,But3,But4;
int optr1,optr2;
?oat result;
String s;
sy76()
{
super("简单计算器");
lab1=new Label("操作数");
tf1=new TextField("",20);
lab2=new Label("操作数");
tf2=new TextField("",20);
lab3=new Label("结果");
tf3=new TextField("",20);
But1=new Button(" + ");
But2=new Button(" - ");
But3=new Button(" * ");
But4=new Button(" / ");
GridBagLayout gbL=new
GridBagLayout();
GridBagConstraints gbc=new
GridBagConstraints();
setLayout(gbL);
gbc.gridx=0;/*指定包含组件的
显示区域开始边的单元格,其中行的第一
个单元格为 gridx=0。*/
gbc.gridy=0;/*指定位于组件显
示区域的顶部的单元格,其中最上边的单
元格为 gridy=0*/
gbc.gridwidth=1;/*指定组件显
示区域的某一行中的单元格数*/
评论5
最新资源