地暖管壁厚国家标准:java链表用application怎么做

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/02 08:45:53
怎么样的图形界面更直观 方便

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class UseLinkList extends Applet implements TextListener,ActionListener
{
Label prompt1;
Label prompt2;
Label prompt3;
TextField input1;
TextField input2;
Button insert;
Button delete,b,c;
Integer data;
int index;
String msg,m;
Panel t;
LinkList mylist;
boolean a;

public void init()
{
data=null;
index=0;
mylist=new LinkList();

prompt1=new Label("输入数据");
prompt2=new Label("输入数据所在的位置(默认为头结点)");
prompt3=new Label("链表中数据的逻辑结构:");
input2=new TextField(5);
input1=new TextField(5);
insert=new Button("尾插数据");
delete=new Button("删除数据");
b=new Button("全部删除");
c=new Button("头插建表");
t=new Panel();

GridBagLayout gbLayout=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gbLayout);

gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;
gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbLayout.setConstraints(prompt1,gbc);
add(prompt1);

gbc.gridx=1;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;
gbc.weighty=0;
gbc.insets=new Insets(2,5,2,5);
gbLayout.setConstraints(input1,gbc);
add(input1);

gbc.gridx=2;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=0;
gbc.weighty=0;
gbc.insets=new Insets(5,5,5,5);
gbLayout.setConstraints(c,gbc);
add(c);

gbc.gridx=1;
gbc.gridy=3;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;
gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbLayout.setConstraints(prompt2,gbc);
add(prompt2);

gbc.gridx=1;
gbc.gridy=4;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;
gbc.weighty=0;
gbc.insets=new Insets(2,5,2,5);
gbLayout.setConstraints(input2,gbc);
add(input2);

gbc.gridx=1;
gbc.gridy=5;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=0;
gbc.weighty=0;
gbc.insets=new Insets(5,5,5,5);
gbLayout.setConstraints(insert,gbc);
add(insert);

gbc.gridx=2;
gbc.gridy=5;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=0;
gbc.weighty=0;
gbc.insets=new Insets(5,5,5,5);
gbLayout.setConstraints(delete,gbc);
add(delete);

gbc.gridx=1;
gbc.gridy=6;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;
gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbLayout.setConstraints(prompt3,gbc);
add(prompt3);

gbc.gridx=3;
gbc.gridy=5;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.SOUTHWEST;
gbc.weightx=0;
gbc.weighty=0;
gbc.insets=new Insets(5,5,5,5);
gbLayout.setConstraints(b,gbc);
add(b);

gbc.gridx=4;
gbc.gridy=8;
gbc.gridwidth=4;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.BOTH;
gbc.anchor=GridBagConstraints.CENTER;
gbc.weightx=1;
gbc.weighty=1;
gbc.insets=new Insets(2,5,5,5);
gbLayout.setConstraints(t,gbc);
add(t);

input1.addTextListener(this);
input2.addTextListener(this);
insert.addActionListener(this);
delete.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);

}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==input1)
{
try
{
data = Integer.valueOf(input1.getText());
a=true;
}
catch(NumberFormatException ex)
{
msg="在第一个文本框输入数字";
repaint();
}
}
if(e.getSource()==input2)
{
try
{
index = Integer.parseInt(input2.getText());
a=true;
}
catch(NumberFormatException ex)
{
msg="在第二个文本框输入数字";
repaint();
}
}

}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==insert)
{
if(a)
{

mylist.insertAfterId(data, index);
msg=mylist.visitAllNode();
repaint();
a=false;
}

}
if(e.getSource()==delete)
{
if(!a)
{
mylist.removeAtId(index);
msg=mylist.visitAllNode();
repaint();
}
}
if(e.getSource()==b)
{
if(a)
{
mylist.removeAll();
msg=mylist.visitAllNode();
repaint();
a=false;
}
}
if(e.getSource()==c)
{

mylist.insertAtBegin(data);
msg=mylist.visitAllNode();
repaint();

}

}

public void paint(Graphics g)
{
g.drawString(msg, 5, 300);
}
}

class LinkList
{
private Node first;
public LinkList()
{
first = null;
}
public LinkList(Object data)
{
first = new Node(data);
}
public String visitAllNode()
{
Node node = first;
String str="";
while(node != null)
{
str+=((node.getData()).toString()+" ");
node=node.getNext();
}
return str;
}
public void insertAtBegin(Object data)
{
if(first==null)
{
first = new Node(data);
return;
}
first = new Node(data, first);
}
public void insertAfterId(Object data, int index)
{
Node node = first;
if(first == null)
{
first = new Node(data);
return ;
}
int ind = 0;
while(node.getNext() != null && ind < index)
{
node = node.getNext();
ind++;
}
Node temp = new Node(data);
temp.setNext(node.getNext());
node.setNext(temp);
return ;
}
public Object removeAtId(int index)
{
if(first == null)
return null;
Node temp;
if(index == 0 )
{
temp = first;
first = first.getNext();
return temp.getData();
}
Node node = first;
int ind = 1;
while(node.getNext() != null && ind <= index)
{
if(ind == index)
{
temp = node.getNext();
node.setNext(temp.getNext());
return temp.getData();
}
node = node.getNext();
ind++;
}
return null;
}
public boolean isEmpty()
{
return first==null;
}
public void removeAll()
{
while(!isEmpty())
{
removeAtId(0);
}
}

}

class Node
{
private Object item;
private Node nextNode;
private void in(Object data, Node next)
{
item = data;
nextNode = next;
}
public Node(Object data)
{
in(data, null);
}
public Node(Object data, Node next)
{
in(data, next);
}
public void setData(Object data)
{
item = data;
}
public Object getData()
{
return item;
}
public void setNext(Node next)
{
nextNode = next;
}
public Node getNext()
{
return nextNode;
}
}