站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→帮忙看一下这道java题

帮忙看一下这道java题

发表新主题   回复此主题

第1楼 2007-07-01 12:21 深紫色yyt 写道:

帮忙看一下这道java题

题在附件里,麻烦打开看看
下面是我编的部分代码,还差主要功能:
import java.awt.*;
import java.awt.event.*;
public class TestPanel extends Frame
{
//设置关闭监听
public static void WindlowListener(Frame frm)
{
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)//实现windowListener中的方法
{
System.exit(0);
}
}
);
}
public static void main(String args[])
{
TestPanel frm=new TestPanel("信051贾航23");
Panel pnl=new Panel();
pnl.setLayout(null);//不使用布局管理器

//设置列表框
List lst=new List(5,true);
lst.add("1 王子 male 北京 东城区",0);
lst.add("2 公主 female 河北 石家庄",1);
lst.setBackground(Color.GRAY);

//设置文本框
TextField txt1=new TextField("1",1);
TextField txt2=new TextField("",15);

//设置复选框
CheckboxGroup chkbg=new CheckboxGroup();
Checkbox chkb1=new Checkbox("male",chkbg,false);
Checkbox chkb2=new Checkbox("female",chkbg,true);
chkbg.setSelectedCheckbox(chkb2);

//设置下拉列表框
Choice ch1=new Choice();
ch1.addItem("北京");
ch1.addItem("河北");
ch1.addItem("天津");
Choice ch2=new Choice();
ch2.addItem("东城区");
ch2.addItem("西城区");
ch2.addItem("石家庄");
ch2.addItem("保定");
ch2.addItem("天津港");
ch2.addItem("河东区");

//设置按钮
Button btn=new Button("Add");

//用add方法将组件添加到面板pnl
pnl.add(chkb1);
pnl.add(chkb2);
pnl.add(ch1);
pnl.add(ch2);
pnl.add(btn);
pnl.add(lst);
pnl.add(txt1);
pnl.add(txt2);
frm.add(pnl);

//设置框架及面板的属性
pnl.setSize(590,490);
pnl.setBackground(Color.gray);
frm.setSize(600,500);
frm.setBackground(Color.white);

//手动设置组件布局
frm.setLocation(220,120);
lst.setSize(290,470);
txt1.setBounds(300,10,280,50);
txt2.setBounds(300,75,280,50);
chkb1.setBounds(320,160,140,30);
chkb2.setBounds(460,160,140,30);
ch1.setBounds(300,220,280,30);
ch2.setBounds(300,280,280,30);
btn.setBounds(300,360,280,50);
//设置字体
Font l=new Font("Serif",Font.PLAIN,20);
lst.setFont(l);
Font c1=new Font("Serif",Font.PLAIN,20);
ch1.setFont(c1);
Font c2=new Font("Serif",Font.PLAIN,20);
ch2.setFont(c2);
Font t1=new Font("Serif",Font.PLAIN,20);
txt1.setFont(t1);
Font t2=new Font("Serif",Font.PLAIN,20);
txt1.setFont(t2);
Font ck1=new Font("Serif",Font.PLAIN,20);
chkb1.setFont(ck1);
Font ck2=new Font("Serif",Font.PLAIN,20);
chkb2.setFont(ck2);
Font b=new Font("Serif",Font.PLAIN,20);
btn.setFont(b);

WindlowListener(frm);
frm.setVisible( true);
frm.pack();


btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);

}
});
}

public TestPanel(String str)
{super(str);}
}

第2楼 2013-08-31 12:44 Robot :

帮忙看一下这道java题 相关


第3楼 2007-07-01 13:53 热学男儿 写道:

级别:学弟
7月1日 10:51 我看了你的题目,给你改了改程序,在添加的按钮监听里面写程序,先用getSoure()方法获得按钮的监听源,表示点键钮一次,然后后的你上面写的文本框,单选按钮,下拉框中的字符串,将这些字符串添加到旁边的列表里就好了,这些程序全部都是在按钮的监听里写的,你的题目要求中已经给出了一些方法里面的getText()是用来获得文本框中的字符串的,getSlectItem()是用来获得下拉框中的字符串的,其他要用到但上面没有给出来的方法,你到API文档里查查就好了,找到了方法,你会觉得很容易写出来的,但是按钮监听里面的这一句要去掉,System.exit(0),下面是给你改好的代码,同时按照你的题目要求把你程序中的txt1改成了只读形式,因为要在按钮监听的内部类里访问你设的各个组建的变量,所以我把这些变量都给你改为了全局变量,并声明为了静态变量,自己看看吧。最后祝你好运。
import java.awt.*;
import java.awt.event.*;
public class TestPanel extends Frame
{
static List lst;
static TextField txt1;
static TextField txt2;
static Checkbox chkb1;
static Checkbox chkb2;
static Choice ch1;
static Choice ch2;
static Button btn;
//设置关闭监听
public static void WindlowListener(Frame frm)
{
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)//实现windowListener中的方法
{
System.exit(0);
}
}
);
}
public static void main(String args[])
{
TestPanel frm=new TestPanel("信051贾航23");
Panel pnl=new Panel();
pnl.setLayout(null);//不使用布局管理器

//设置列表框
lst=new List(10,true);
lst.add("1 王子 male 北京 东城区",0);
lst.add("2 公主 female 河北 石家庄",1);
lst.setBackground(Color.GRAY);

//设置文本框
txt1=new TextField("3",1);
txt2=new TextField("",15);

//设置复选框
CheckboxGroup chkbg=new CheckboxGroup();
chkb1=new Checkbox("male",chkbg,false);
chkb2=new Checkbox("female",chkbg,true);
chkbg.setSelectedCheckbox(chkb2);

//设置下拉列表框
ch1=new Choice();
ch1.addItem("北京");
ch1.addItem("河北");
ch1.addItem("天津");
ch2=new Choice();
ch2.addItem("东城区");
ch2.addItem("西城区");
ch2.addItem("石家庄");
ch2.addItem("保定");
ch2.addItem("天津港");
ch2.addItem("河东区");

//设置按钮
btn=new Button("Add");

//用add方法将组件添加到面板pnl
pnl.add(chkb1);
pnl.add(chkb2);
pnl.add(ch1);
pnl.add(ch2);
pnl.add(btn);
pnl.add(lst);
pnl.add(txt1);
pnl.add(txt2);
frm.add(pnl);

//设置框架及面板的属性
pnl.setSize(590,490);
pnl.setBackground(Color.gray);
frm.setSize(600,500);
frm.setBackground(Color.white);

//手动设置组件布局
frm.setLocation(220,120);
lst.setSize(290,470);
txt1.setBounds(300,10,280,50);
txt1.setEditable(false);
txt2.setBounds(300,75,280,50);
chkb1.setBounds(320,160,140,30);
chkb2.setBounds(460,160,140,30);
ch1.setBounds(300,220,280,30);
ch2.setBounds(300,280,280,30);
btn.setBounds(300,360,280,50);
//设置字体
Font l=new Font("Serif",Font.PLAIN,20);
lst.setFont(l);
Font c1=new Font("Serif",Font.PLAIN,20);
ch1.setFont(c1);
Font c2=new Font("Serif",Font.PLAIN,20);
ch2.setFont(c2);
Font t1=new Font("Serif",Font.PLAIN,20);
txt1.setFont(t1);
Font t2=new Font("Serif",Font.PLAIN,20);
txt1.setFont(t2);
Font ck1=new Font("Serif",Font.PLAIN,20);
chkb1.setFont(ck1);
Font ck2=new Font("Serif",Font.PLAIN,20);
chkb2.setFont(ck2);
Font b=new Font("Serif",Font.PLAIN,20);
btn.setFont(b);

WindlowListener(frm);
frm.setVisible( true);
frm.pack();

//String a1,a2,a3,a4,a5;
//int i=2;
btn.addActionListener(new ActionListener()
{ String a1,a2,a3,a4,a5;
int i=2;
int j=4;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn){ //当点击按钮时获得按钮的监听源
a1=txt1.getText(); //获得txt1的字符串
a2=txt2.getText(); //获得txt2的字符串
if(chkb1.getState()){ //判断单选框chkb1是否选中
a3=chkb1.getLabel(); //获得chkb1的标签
}
else if(chkb2.getState()){ //判断单选框chkb2是否选中
a3=chkb2.getLabel(); //获得chkb2的标签
}
a4=ch1.getSelectedItem(); //获得ch1的当前选中字符串形式
a5=ch2.getSelectedItem(); //获得ch2的当前选中字符串形式
String m=Integer.toString(j);
txt1.setText(m); //txt1中的只读数字,每点击一次按钮,增长一次;
lst.add(a1+" "+a2+" "+a3+" "+a4+" "+a5,i); //将获得东西全部写入列表lst中
i++;
j++;
}
}
});
}

public TestPanel(String str)
{super(str);}
}

该回答在7月1日 19:15由回答者修改过

发表新主题   回复此主题