pack();show();setVisible(true);
button1.addActionListener(this);
问题可能出在这几句,show()和setVisible|()只用一个就行了。
还有addActionListener()方法你用错了,查查资料好好看看吧。应该如下改改看
......
ActionListener action=new ActionListener(){
public void actionPerformed(ActionEvent e){
......}};
button1.addActionListener(action);
......
button1.addActionListener(this);
问题可能出在这几句,show()和setVisible|()只用一个就行了。
还有addActionListener()方法你用错了,查查资料好好看看吧。应该如下改改看
......
ActionListener action=new ActionListener(){
public void actionPerformed(ActionEvent e){
......}};
button1.addActionListener(action);
......
{tf[i-1]=new TextField(4);add(tf[i-1]);};//你这样挨着加挨着加是什么意思?你知道加到哪去了吗
panel1是简单的,就一个提交;
panel2可能复杂点,其实是一个表格样子的形式,三个字段分别是学号,姓名,成绩,因为我还没学过
表格,所以暂时用一个文本框的数组来做,希望高手理解我;
panel3是一个统计,统计出及格的人,不及格的人,优秀的人,我用了三个字符串这里的 n Mark[] Name[] tf数组 这4个差不多都是全局变量的意思
可能我的水平比较低,所以错误比较低级,谢谢高手再帮我看看
this.tf = tf;
}
}
给构造方法添加上参数,你可以传递多个参数进来这样,就不用内部类实现了。而且完成了数据的传递。
使用时:myPanel1 = new MyPanel1(tf);//tf是主窗口中得到的数据。
//Cjgl.java 本地测试已通过
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//
public class Cjgl{
public static void main(String args[])
{
MainFrame mf=new MainFrame();
mf.setSize(new Dimension(800,600));
mf.setVisible(true);
}
}class MainFrame extends JFrame
{
int n; //记录学生的人数
int[] Mark; //定义分数数组
String[] Name; //定义名字数组
String[] Numb; //记录学号
JTextField[] tf; //定义文本框数组
// private Mypanel1 mp1;
// private Mypanel2 mp2;
// private Mypanel3 mp3;
Container con=(Container)this.getContentPane();
MainFrame()
{
super("学生成绩登记表");
// create the component
Mypanel1 mp1=new Mypanel1();
// mp2=new Mypanel2();
// mp3=new Mypanel3();
// Add the components to the frame
con.add(mp1, BorderLayout.NORTH);
// con.add(mp2, BorderLayout.CENTER);
// con.add(mp3, BorderLayout.SOUTH);
// setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}});
}
class Mypanel1 extends JPanel implements ActionListener
{
JLabel prompt1,prompt2;Choice choice1;JTextField textfield1;JButton button1;
public Mypanel1()
{
setLayout(new FlowLayout());
prompt1=new JLabel("考试课程:");
choice1=new Choice();
choice1.add("数学");choice1.add("语文");choice1.add("英语");
prompt2=new JLabel("考试人数:");
textfield1=new JTextField(4);
button1=new JButton("确定");
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();
n=Integer.parseInt(s1);
// if (e.getSource()==button1)
// n=value1;
Mark=new int[n]; //定义分数数组
Name=new String[n]; //定义名字数组
Numb=new String[n];
tf=new JTextField[3*n];
textfield1.setEnabled(false);
choice1.setEnabled(false);
button1.setEnabled(false);
Mypanel2 mp2=new Mypanel2();
con.add(mp2, BorderLayout.CENTER);
revalidate();
}
} class Mypanel2 extends JPanel implements ActionListener
{
JLabel prompt1,prompt2,prompt3;
JButton button1,button2,button3;
public Mypanel2()
{
setLayout(new GridLayout(n+2,3));
prompt1=new JLabel("学号");
prompt2=new JLabel("姓名");
prompt3=new JLabel("成绩");
button1=new JButton("确认");
button2=new JButton("修改");
button3=new JButton("取消");
add(prompt1);add(prompt2);add(prompt3);
for(int i=1;i<=3*n;i++)
{tf[i-1]=new JTextField(4);tf[i-1].setText("");add(tf[i-1]);};
add(button1);add(button2);add(button3);
// pack();show();setVisible(true);
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++){
if(!tf[3*i-1].getText().equals("")) //若有的没有输入成绩运行时会出错
Mark[i-1]=Integer.parseInt((tf[3*i-1].getText()));
else Mark[i-1]=-1; }
for(int i=0;i<n;i++)
Numb[i]=tf[3*i].getText();
for(int i=1;i<=3*n;i++)
tf[i-1].setEnabled(false);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
Mypanel3 mp3=new Mypanel3();
con.add(mp3,BorderLayout.SOUTH);
revalidate();
}
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 JPanel implements ActionListener
{
JLabel prompt1,prompt2;
JButton button1,button2;
public Mypanel3()
{
setLayout(new FlowLayout());
prompt1=new JLabel("统计吗?");
button1=new JButton("确认");
button2=new JButton("取消");
prompt2=new JLabel("统计结果显示区域");
add(prompt1);add(button1);add(button2);add(prompt2);
// pack();show();setVisible(true);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String youxiu="",jige="",bujige="";
for(int i=0;i<n;i++)
{
if(Mark[i]>=85) youxiu=youxiu+Name[i]+" ";
else if(60>Mark[i]&&Mark[i]>=0) bujige=bujige+Name[i]+" ";
else if(85>Mark[i]&&Mark[i]>=60) jige=jige+Name[i]+" ";
}
if(e.getSource()==button1)
{ //这里如果prompt2显示不下就没有任何显示信息,所以需要改进,测试时尽量让人数不要过大
prompt2.setText("优秀:"+youxiu+"及格:"+jige+"不及格:"+bujige);
}
if(e.getSource()==button2)
{System.exit(0);}
}
}
}