用 panel做,把你要加上的组件先加入panel里面。然后在把PANEL加入到顶级容器里
解决方案 »
- java底层
- 关于set的toArray方法
- 刚学完类这一章,感觉做这个题目好像知道思路,但写出来的东西就是不能运行,大家看下这个应该怎么着手编写思路啊
- 输出1到1000之间的所以完数
- 我是菜鸟,想学JAVA,不知道怎么开始,望高手指点谢谢
- 用 JInternalFrame创建得多文档应用程序,怎么响应鼠标事件?
- 怎么用UDP来发送一个对象,如何将对象跟字节流联系起来啊?
- 过路的大哥帮帮忙!请问java查询某个指定的文件夹是否存在指定的文件的api?
- kjava中资源有限,可是我要写很多的form如何减少对内存的耗费???
- 大家好,我想学JAVA,不知道学哪个公司的好?我是新手
- 用ASP.NET + C# + XSL + XML做了个东西,请大家帮忙测试,还有分拿!!
- 大家帮帮忙看看这个FTP程序中的列表操作为什么总是不正确?
小的布局也用panel做好,然后加入大的布局
[email protected]
import java.applet.*;
import java.applet.Applet;class Cal extends Frame{
TextField outField;
Panel p=new Panel();
public Cal(){
super("Little Calcalator");
setLayout(new GridLayout(3,1));
outField=new TextField();
outField.setEditable(false);
add(new DisplayPanel(outField));
p.setLayout(new FlowLayout(FlowLayout.RIGHT));
p.add(new ClearPanel(outField));
p.add(new OperationPanel(outField));
add(p);
add(new NumPanel(outField));
pack();
resize(200,250);
show();
}
public static void main(String args[]){
new Cal();
}
public Insets insets(){
return new Insets(30,20,10,20);
}
public boolean handleEvent(Event evt){
if(evt.id==Event.WINDOW_DESTROY){
System.exit(0);
return true;
}
return false;
}
}class DisplayPanel extends Panel{
TextField text;
public DisplayPanel(TextField text){
setLayout(new GridLayout(2,1));
add(text);
add(new Label("============================"));
}
}class NumPanel extends Panel{
TextField text;
static int value;
public NumPanel(TextField text){
int i;
this.text=text;
setLayout(new GridLayout(4,3,1,1));
for(i=1;i<10;i++)
add(new Button((new Integer(i)).toString() ));
add(new Label());
add(new Button("0"));
add(new Label());
}
public boolean action(Event evt,Object arg){
if(evt.target instanceof Button){
String Title=(String)arg;
value=value*10+new Integer(Title).intValue();
text.setText(new Integer(value).toString());
return true;
}
return false;
}
}class OperationPanel extends Panel{
TextField text;
String Title;
static boolean yFlag; int x;
int y;
public OperationPanel(TextField text){
this.text=text;
setLayout(new GridLayout(2,3,1,1));
add(new Button("+"));
add(new Button("-"));
add(new Button("*"));
add(new Button("/"));
add(new Button("="));
}
public boolean action(Event evt,Object arg){
if(evt.target instanceof Button){
x=(new Integer(text.getText())).intValue();
if(yFlag){
if(Title.equals("+")){
x=y+x;
}
else if(Title.equals("-")){
x=y-x;
}
else if(Title.equals("*")){
x=y*x;
}
else if(Title.equals("/")){
x=y/x;
}
}
yFlag=true;
y=x;
Title=(String)arg;
NumPanel.value=0;
text.setText((new Integer(x)).toString());
}
return false;
}
}class ClearPanel extends Panel{
TextField text;
public ClearPanel(TextField text){
this.text=text;
setLayout(new FlowLayout(FlowLayout.CENTER));
add(new Button("C"));
add(new Button("CA"));
}
public boolean action(Event evt,Object arg){
if(evt.target instanceof Button){
String Title=(String)arg;
if(Title.equals("C")){
NumPanel.value=0;
text.setText("0");
}
else if(Title.equals("CA")){
OperationPanel.yFlag=false;
NumPanel.value=0;
text.setText("0");
}
return true;
}
return false;
}
}
偶会的不多,不过这个例子是通的!