panel是轻量级组件,你在main里面应该先new 一个frame将这几个panel加进去,然后再显示frame.
还有你的panel监听器是在监听什么?你怎么也要有一个actionEvent发生才行呀,你可以在某一个panel显示时候new一个event。
还有你的你的每一个监听器实现了以后也没有去监听什么组件呀。
你还是看看书,找找例子看看在说
还有你的panel监听器是在监听什么?你怎么也要有一个actionEvent发生才行呀,你可以在某一个panel显示时候new一个event。
还有你的你的每一个监听器实现了以后也没有去监听什么组件呀。
你还是看看书,找找例子看看在说
解决方案 »
- 是谁把我的数组给更新了???
- jsp中 java.lang.NoClassDefFoundError问题处理
- 请教一下UML中的聚集和组成在java中怎么理解
- help! Windows中有哪个命令可以把一个文件中的特定字符串替换成另一个字符串
- 为什么Servlet要url-pattern映射,而jsp不要
- java中什么方法同c++中cin cout 是相同的作用呀
- "Hello".getBytes("UTF-16")=?
- 不干java了,郁闷....来散分...
- 继承的私有成员,究竟是如何回事呢?
- 为什么命令行窗口那么快就消失了啊?我根本没法看我的程序啊!55555~~~~~
- 我的Jbuilder9无法用鼠标拖动的方式改变控件的位置和它们的大小,是什么问题?
- 急救,关于从表单提取值时出错……头疼啊
事件处理方面我是会做的,只是在这里省略了,
我主要不会做的是frame和panel怎么处理,还有main的方法怎么处理这些问题。
事件处理是不是要通通放到frame里处理的?
import java.awt.*;
import java.awt.event.*;public class Cjgl
{
public static void main(String args[])
{
MainFrame mf=new MainFrame();
}
}class MainFrame extends Frame
{
int n; //记录学生的人数
int Mark[]=new int[n]; //定义分数数组
String Name[]=new String[n]; //定义名字数组
TextField[] tf = new TextField[n]; //定义文本框数组
Mypanel1 mp1; Mypanel2 mp2; Mypanel3 mp3;
MainFrame()
{
super("学生成绩登记表");
setLayout(new GridLayout(3,1));
add(mp1);add(mp2);add(mp3);
pack();show();setVisible(true);
}
class Mypanel1 extends Panel implements ActionListener
{
Label prompt1,prompt2;Choice choice1;TextField textfield1;Button button1;
public Mypanel1()
{
setLayout(new FlowLayout());
prompt1=new Label("考试课程:");
choice1=new Choice();
choice1.add("数学");choice1.add("语文");choice1.add("英语");
prompt2=new Label("考试人数:");
textfield1=new TextField(4);
button1=new Button("确定");
add(prompt1);add(choice1);add(prompt2);add(textfield1);add(button1);
pack();show();setVisible(true);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s1=textfield1.getText();
int value1=Integer.parseInt(s1);
if (e.getSource()==button1)
n=value1;
}
} class Mypanel2 extends Panel implements ActionListener
{
Label prompt1,prompt2,prompt3;
Button button1,button2,button3;
public Mypanel2()
{
setLayout(new GridLayout(n+2,3));
prompt1=new Label("学号");
prompt2=new Label("姓名");
prompt3=new Label("成绩");
button1=new Button("确认");
button2=new Button("修改");
button3=new Button("取消");
add(prompt1);add(prompt2);add(prompt3);
for(int i=1;i<=3*n;i++)
{tf[i-1]=new TextField(4);add(tf[i-1]);};
add(button1);add(button2);add(button3);
pack();show();setVisible(false);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
for(int i=1;i<=n;i++)
Name[i-1]=tf[3*i-2].getText();
for(int i=1;i<=n;i++)
Mark[i-1]=Integer.parseInt(tf[3*i-1].getText());
}
if(e.getSource()==button2)
{
tf[0].requestFocus();
}
if(e.getSource()==button3)
{
for(int i=1;i<=3*n;i++)
tf[i-1].setText("");
}
}
} class Mypanel3 extends Panel implements ActionListener
{
Label prompt1,prompt2;
Button button1,button2;
public Mypanel3()
{
setLayout(new FlowLayout());
prompt1=new Label("是否要统计?");
button1=new Button("确认");
button2=new Button("取消");
prompt2=new Label(" ");
add(prompt1);add(button1);add(button2);add(prompt2);
pack();show();setVisible(false);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String youxiu="",jige="",bujige="";
for(int i=0;i<=n-1;i++)
{
if(Mark[i]>84) youxiu=youxiu+Name[i];
else if(60>Mark[i]) bujige=bujige+Name[i];
else jige=jige+Name[i];
}
if(e.getSource()==button1)
{
prompt2.setText("优秀的有:"+youxiu+"及格的有:"+jige+"不及格的有:"+bujige);
}
if(e.getSource()==button2)
{System.exit(0);}
}
}
}
假如我在一个程序中有两个自定义的panel
开始显示一个,然后我点击了第一个panel里面的按钮,
第二个panel出现,这样的话大体应该怎么实现
我开始的时候把事件处理是定义在panel里面的
可是觉得好像有问题
因为我把事件处理定义在panel的话我就不能控制第二个panel是否可见
如果把所有的事件处理都放到panel外面的frame里的话我又觉得比较乱,因为frame里面会有好多个事件处理