以下是我写的一个关于线性表的程序,这是5个java源文件中的两个,在编译的时候报错,是关于两个类的对象的引用的问题,主要问题是这么几行(我用红笔标注了),我找了好久,但是始终没有找到问题的所在,请高手 帮我解答,在下感激不尽了
源文件1:
package sxm;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Myhomework extends JFrame implements ActionListener {
private JLabel xuehao, name, s;
private JTextField text1, text2, text3;
private JButton buttonse, buttonde, buttonor, buttonin;
static MarkList list;
public Myhomework() {
super("勇敢去做吧,明天是属于你的!");
Container container = getContentPane();
container.setLayout(new FlowLayout());
xuehao = new JLabel("学号");
name = new JLabel("姓名");
s = new JLabel("成绩");
text1 = new JTextField(20);
text2 = new JTextField(20);
text3 = new JTextField(20);
buttonse = new JButton("查询");
buttonde = new JButton("删除");
buttonin = new JButton("添加");
buttonor = new JButton("排序");
container.add(xuehao);
container.add(text1);
container.add(name);
container.add(text2);
container.add(s);
container.add(text3);
container.add(buttonse);
container.add(buttonde);
container.add(buttonor);
container.add(buttonin);
buttonse.addActionListener(this);
buttonde.addActionListener(this);
buttonor.addActionListener(this);
buttonin.addActionListener(this);
setVisible(true);
setSize(300, 200);
} public JTextField getText1() {
return text1;
}
public JTextField getText2() {
return text2;
}
public JTextField getText3() {
return text3;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonse) {
if (text1.getText().equals("")){
JOptionPane.showMessageDialog(null, "你好,请先输入要查询的同学的学号!");
return;
}
list.select();
}
else if (e.getSource() == buttonde) {
if (text1.getText().equals("")){
JOptionPane.showMessageDialog(null, "你好,请先输入要删除的同学的学号!");
return;
}
list.delete();
}
else if (e.getSource() == buttonin) {
if (text1.getText().equals("")
|| text2.getText().equals("")
|| text3.getText().equals("")){
JOptionPane.showMessageDialog(null, "你好,请输入完整的信息!");
return;
}
list.insert();
}
else if (e.getSource() == buttonor) {
boolean b=true;
String result = "";
if(list.length()<1){
JOptionPane.showMessageDialog(null,"对不起,你还没有添加数据呢!");
b=false;
}
list.order();
while(b){
for (int i = 0; i < list.length(); i++) {
result += ((Student) list.get(i + 1)).getName() + " ";
}
JOptionPane.showMessageDialog(null, "本次考试同学的名次依次为:"+result);
b=false;
}
}
} public static void main(String args[]) {
Myhomework sxm = new Myhomework();
[color=#FF0000list] = new MarkList(50);[/color]
sxm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}源文件2:
package sxm;
import javax.swing.JOptionPane;
public class MarkList extends PubList {
Myhomework sxm ;
Student ss = null;
static MarkList list = null;
public MarkList(int max)
{
super(max);
//sxm = new Myhomework();
ss = new Student();
}
public void insert(){ //添加对象
int num_1,num_2,i=1;
while(true){
try{
num_1 = Integer.parseInt(sxm.getText1().getText());
if (num_1==0) break;
ss.putNum(num_1);
num_2 = Integer.parseInt(sxm.getText3().getText());
ss.putMarks(num_2);
ss.putName(sxm.getText2().getText());
super.insert(i,ss);
JOptionPane.showMessageDialog(null,"添加成功!");
}
catch(NumberFormatException numberFormatException){
JOptionPane.showMessageDialog(null,"对不起,请输入整数,再试一次!");
continue;
}
i++;
}
} public void select(){ //查询对象
int i,num=0,tnum,swinum;
try{
num=Integer.parseInt(sxm.getText1().getText());
}
catch(NumberFormatException numberFormatException){
JOptionPane.showMessageDialog(null,"输入错误,请输入整数好吗?");
}
i=1;
while(i<=this.length()) {
tnum=((Student)this.get(i)).getNum();
if (tnum==num){
sxm.getText2().setText(((Student)this.get(i)).getName());
swinum =((Student)this.get(i)).getMarks();
sxm.getText3().setText(swinum+"");
}
i++;
}
JOptionPane.showMessageDialog(null,"对不起,你要找的人不存在,请核实再来吧!");
} public void delete(){ //删除对象
int num,i=1;
try{
num=Integer.parseInt(sxm.getText1().getText());
while(i<=this.length()&&((Student)this.get(i)).getNum()!=num)
i++;
if (i<this.length()){
super.delete(i);
JOptionPane.showMessageDialog(null,"删除成功!");
}
else
JOptionPane.showMessageDialog(null,"对不起,你要删除的人不存在,请核实再来吧!");
}
catch(NumberFormatException numberFormatException){
JOptionPane.showMessageDialog(null,"输入错误,请输入整数好吗?");
}
} public void order(){ //分数排序
Student temp1,temp2;
int i,j;
for(i=1;i<this.length();++i ){
for(j=1;j<=this.length()-i;++j){
temp1=(Student)get(j);
temp2=(Student)get(j+1);
if (temp1.getMarks()<temp2.getMarks()){
set(j,temp2);
set(j+1,temp1);
}
}
}
}
}
源文件1:
package sxm;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Myhomework extends JFrame implements ActionListener {
private JLabel xuehao, name, s;
private JTextField text1, text2, text3;
private JButton buttonse, buttonde, buttonor, buttonin;
static MarkList list;
public Myhomework() {
super("勇敢去做吧,明天是属于你的!");
Container container = getContentPane();
container.setLayout(new FlowLayout());
xuehao = new JLabel("学号");
name = new JLabel("姓名");
s = new JLabel("成绩");
text1 = new JTextField(20);
text2 = new JTextField(20);
text3 = new JTextField(20);
buttonse = new JButton("查询");
buttonde = new JButton("删除");
buttonin = new JButton("添加");
buttonor = new JButton("排序");
container.add(xuehao);
container.add(text1);
container.add(name);
container.add(text2);
container.add(s);
container.add(text3);
container.add(buttonse);
container.add(buttonde);
container.add(buttonor);
container.add(buttonin);
buttonse.addActionListener(this);
buttonde.addActionListener(this);
buttonor.addActionListener(this);
buttonin.addActionListener(this);
setVisible(true);
setSize(300, 200);
} public JTextField getText1() {
return text1;
}
public JTextField getText2() {
return text2;
}
public JTextField getText3() {
return text3;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonse) {
if (text1.getText().equals("")){
JOptionPane.showMessageDialog(null, "你好,请先输入要查询的同学的学号!");
return;
}
list.select();
}
else if (e.getSource() == buttonde) {
if (text1.getText().equals("")){
JOptionPane.showMessageDialog(null, "你好,请先输入要删除的同学的学号!");
return;
}
list.delete();
}
else if (e.getSource() == buttonin) {
if (text1.getText().equals("")
|| text2.getText().equals("")
|| text3.getText().equals("")){
JOptionPane.showMessageDialog(null, "你好,请输入完整的信息!");
return;
}
list.insert();
}
else if (e.getSource() == buttonor) {
boolean b=true;
String result = "";
if(list.length()<1){
JOptionPane.showMessageDialog(null,"对不起,你还没有添加数据呢!");
b=false;
}
list.order();
while(b){
for (int i = 0; i < list.length(); i++) {
result += ((Student) list.get(i + 1)).getName() + " ";
}
JOptionPane.showMessageDialog(null, "本次考试同学的名次依次为:"+result);
b=false;
}
}
} public static void main(String args[]) {
Myhomework sxm = new Myhomework();
[color=#FF0000list] = new MarkList(50);[/color]
sxm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}源文件2:
package sxm;
import javax.swing.JOptionPane;
public class MarkList extends PubList {
Myhomework sxm ;
Student ss = null;
static MarkList list = null;
public MarkList(int max)
{
super(max);
//sxm = new Myhomework();
ss = new Student();
}
public void insert(){ //添加对象
int num_1,num_2,i=1;
while(true){
try{
num_1 = Integer.parseInt(sxm.getText1().getText());
if (num_1==0) break;
ss.putNum(num_1);
num_2 = Integer.parseInt(sxm.getText3().getText());
ss.putMarks(num_2);
ss.putName(sxm.getText2().getText());
super.insert(i,ss);
JOptionPane.showMessageDialog(null,"添加成功!");
}
catch(NumberFormatException numberFormatException){
JOptionPane.showMessageDialog(null,"对不起,请输入整数,再试一次!");
continue;
}
i++;
}
} public void select(){ //查询对象
int i,num=0,tnum,swinum;
try{
num=Integer.parseInt(sxm.getText1().getText());
}
catch(NumberFormatException numberFormatException){
JOptionPane.showMessageDialog(null,"输入错误,请输入整数好吗?");
}
i=1;
while(i<=this.length()) {
tnum=((Student)this.get(i)).getNum();
if (tnum==num){
sxm.getText2().setText(((Student)this.get(i)).getName());
swinum =((Student)this.get(i)).getMarks();
sxm.getText3().setText(swinum+"");
}
i++;
}
JOptionPane.showMessageDialog(null,"对不起,你要找的人不存在,请核实再来吧!");
} public void delete(){ //删除对象
int num,i=1;
try{
num=Integer.parseInt(sxm.getText1().getText());
while(i<=this.length()&&((Student)this.get(i)).getNum()!=num)
i++;
if (i<this.length()){
super.delete(i);
JOptionPane.showMessageDialog(null,"删除成功!");
}
else
JOptionPane.showMessageDialog(null,"对不起,你要删除的人不存在,请核实再来吧!");
}
catch(NumberFormatException numberFormatException){
JOptionPane.showMessageDialog(null,"输入错误,请输入整数好吗?");
}
} public void order(){ //分数排序
Student temp1,temp2;
int i,j;
for(i=1;i<this.length();++i ){
for(j=1;j<=this.length()-i;++j){
temp1=(Student)get(j);
temp2=(Student)get(j+1);
if (temp1.getMarks()<temp2.getMarks()){
set(j,temp2);
set(j+1,temp1);
}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货