请大家帮我看下这个java程序的错误,非常感谢!
请高手指点:http://hi.baidu.com/%B3%A4%D3%C2%5Fzone/blog/item/98220412bdd92754f919b879.html...
请高手指点:
http://hi.baidu.com/%B3%A4%D3%C2%5Fzone/blog/item/98220412bdd92754f919b879.html
我这个程序在eclipse上能正常运行,但是到了linux下,运行起来就只有一个frame其他什么也没有,还有就是关闭不了。能找出的请大家帮忙找下,给分的时候我会考虑的。 展开
http://hi.baidu.com/%B3%A4%D3%C2%5Fzone/blog/item/98220412bdd92754f919b879.html
我这个程序在eclipse上能正常运行,但是到了linux下,运行起来就只有一个frame其他什么也没有,还有就是关闭不了。能找出的请大家帮忙找下,给分的时候我会考虑的。 展开
3个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exercise_5 {
public static void main(String[] args) {
new Linklist("Exercise-5");
}
}
class Planet {
double distance;
String planetname;
Planet forward;
public Planet(double dis, String pname) {
distance = dis;
planetname = pname;
forward = null;
}
public String toString() {
return distance + "--" + planetname;
}
}
class LinkedList {
public LinkedList() {
head = null;
nodecounter = 0;
pre = null;
current = null;
}
Planet head, pre, current;
int nodecounter;
public void addElement(double dis, String name) {
this.addElement(new Planet(dis, name));
}
public void addElement(Planet p) {
if (head != null) {
if (p.distance < head.distance) {
p.forward = head;
head = p;
} else {
current = head;
while (current != null) {
if (p.distance < current.distance) {
pre.forward = p;
p.forward = current;
break;
} else
pre = current;
current = current.forward;
}
pre.forward = p;
}
} else {
head = p;
}
nodecounter++;
}
public String listInfo() throws NullPointerException {
Planet tmp = head;
String lsinfo = "";
while (tmp != null) {
lsinfo = lsinfo + tmp + "\n";
tmp = tmp.forward;
}
return lsinfo;
}
}
class Linklist extends JFrame implements ActionListener {
private static final long serialVersionUID = -1505225068154256928L;
public Linklist(String t) {
setTitle(t);
c = getContentPane();
c.setLayout(new FlowLayout(0, 5, 15));
ini_comps();
setSize(340, 300);
setResizable(false);
ls = new LinkedList();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
JLabel dis_lb, pname_lb;
JTextField dis_tf, pname_tf;
JButton add;
JTextArea lst_info;
LinkedList ls;
Container c;
private void ini_comps() {
dis_lb = new JLabel("Distance(AU) :");
pname_lb = new JLabel("Planet Name :");
dis_tf = new JTextField(20);
pname_tf = new JTextField(20);
add = new JButton("Add");
add.addActionListener(this);
lst_info = new JTextArea("There are 0 items.", 8, 29);
lst_info.setEditable(false);
c.add(dis_lb);
c.add(dis_tf);
c.add(pname_lb);
c.add(pname_tf);
c.add(add);
c.add(lst_info);
}
public void actionPerformed(ActionEvent e) {
String pname = pname_tf.getText().trim();
double distance = toDouble(dis_tf.getText().trim());
if (e.getSource().equals(add) && !pname.equals("") && distance != -1) {
ls.addElement(distance, pname);
lst_info.setText(ls.listInfo());
} else {
dis_tf.setText("");
}
}
private double toDouble(String s) {
double tmp = -1;
try {
tmp = Double.parseDouble(s);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid number!", "Warning", 2);
}
return tmp;
}
}
import java.awt.*;
import java.awt.event.*;
public class Exercise_5 {
public static void main(String[] args) {
new Linklist("Exercise-5");
}
}
class Planet {
double distance;
String planetname;
Planet forward;
public Planet(double dis, String pname) {
distance = dis;
planetname = pname;
forward = null;
}
public String toString() {
return distance + "--" + planetname;
}
}
class LinkedList {
public LinkedList() {
head = null;
nodecounter = 0;
pre = null;
current = null;
}
Planet head, pre, current;
int nodecounter;
public void addElement(double dis, String name) {
this.addElement(new Planet(dis, name));
}
public void addElement(Planet p) {
if (head != null) {
if (p.distance < head.distance) {
p.forward = head;
head = p;
} else {
current = head;
while (current != null) {
if (p.distance < current.distance) {
pre.forward = p;
p.forward = current;
break;
} else
pre = current;
current = current.forward;
}
pre.forward = p;
}
} else {
head = p;
}
nodecounter++;
}
public String listInfo() throws NullPointerException {
Planet tmp = head;
String lsinfo = "";
while (tmp != null) {
lsinfo = lsinfo + tmp + "\n";
tmp = tmp.forward;
}
return lsinfo;
}
}
class Linklist extends JFrame implements ActionListener {
private static final long serialVersionUID = -1505225068154256928L;
public Linklist(String t) {
setTitle(t);
c = getContentPane();
c.setLayout(new FlowLayout(0, 5, 15));
ini_comps();
setSize(340, 300);
setResizable(false);
ls = new LinkedList();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
JLabel dis_lb, pname_lb;
JTextField dis_tf, pname_tf;
JButton add;
JTextArea lst_info;
LinkedList ls;
Container c;
private void ini_comps() {
dis_lb = new JLabel("Distance(AU) :");
pname_lb = new JLabel("Planet Name :");
dis_tf = new JTextField(20);
pname_tf = new JTextField(20);
add = new JButton("Add");
add.addActionListener(this);
lst_info = new JTextArea("There are 0 items.", 8, 29);
lst_info.setEditable(false);
c.add(dis_lb);
c.add(dis_tf);
c.add(pname_lb);
c.add(pname_tf);
c.add(add);
c.add(lst_info);
}
public void actionPerformed(ActionEvent e) {
String pname = pname_tf.getText().trim();
double distance = toDouble(dis_tf.getText().trim());
if (e.getSource().equals(add) && !pname.equals("") && distance != -1) {
ls.addElement(distance, pname);
lst_info.setText(ls.listInfo());
} else {
dis_tf.setText("");
}
}
private double toDouble(String s) {
double tmp = -1;
try {
tmp = Double.parseDouble(s);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid number!", "Warning", 2);
}
return tmp;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询