我这里有问题。我创建程序以将数据添加到表中并在按下按钮时对其进行排序。当我按一次排序按钮时,它没有错。但是当我再次按下它是错误的。所以为什么?请帮我。这是代码。
生班
public class Nama {
private String nama;
private int index;
public Nama(String n,int i){
nama=n;index=i;
}
void setData(String n,int i){
nama=n;index=i;
}
String nama(){
return nama;
}
int ind(){
return index;
}
public String toString(){
return(String.format("%s %d", nama,index));
}
}
合并排序类
import java.util.*;
public class MergeSortS{
public void merge_sort(int low,int high,Nama [] a){
int mid;
if(low<high) {
mid=(low+high)/2;
merge_sort(low,mid,a);
merge_sort(mid+1,high, a);
merge(low,mid,high,a);
}
}
public void merge(int low,int mid,int high,Nama [] a){
int h,i,j,k;
Nama b[]=new Nama[50];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high)){
if(a[h].nama().compareToIgnoreCase(a[j].nama())<0){
b[i]=a[h];
h++;
}
else{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid){
for(k=j;k<=high;k++){
b[i]=a[k];
i++;
}
}
else{
for(k=h;k<=mid;k++){
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
public MergeSortS() {
}
}
面板类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Kareem
*/
public class Panel extends JPanel implements ActionListener{
JTable table;
JTextField tf1,tf2,tf3,tf4;
JButton b1,b2,b3,b4,b5,b6,b7;
Vector rows,columns,temp;
DefaultTableModel tabModel;
String[]data = new String[3];
String [] column = {"Nama", "Nim", "IP"};
Nama[] n,nim,ip;
MergeSortS mS;
int c=0,index=0;
public Panel(){
this.setBounds(0,0,1280, 800);
this.setLayout(null);
inaTf();
inaTab();
inaBut();
n= new Nama[50];
nim= new Nama[50];
ip= new Nama[50];
mS= new MergeSortS();
this.setVisible(true);
}
public void inaTab(){
rows=new Vector();
columns= new Vector();
temp= new Vector();
addColumns(column);
tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);
table = new JTable(tabModel);
table.setPreferredScrollableViewportSize(new
Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setBounds(100,100,200,200);
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(50,50,400,400);
add(scroll);
}
public void inaBut(){
b1=new JButton("add Row");
b1.setBounds(50,600,90,25);
add(b1);
b1.addActionListener(this);
b2=new JButton("Delete Row");
b2.setBounds(170,600,90,25);
add(b2);
b2.addActionListener(this);
b3=new JButton("SortName");
b3.setBounds(290,600,120,25);
add(b3);
b3.addActionListener(this);
b5=new JButton("SortNim");
b5.setBounds(290,650,120,25);
add(b5);
b5.addActionListener(this);
b6=new JButton("SortIP");
b6.setBounds(290,700,120,25);
add(b6);
b6.addActionListener(this);
b4=new JButton("RESET");
b4.setBounds(170,650,90,25);
add(b4);
b4.addActionListener(this);
}
public void inaTf(){
tf1=new JTextField();
tf1.setBounds(640,50,90,25);
add(tf1);
JLabel l1= new JLabel("Nama \t: ");
l1.setBounds(530,50,90,25);
add(l1);
tf2=new JTextField();
tf2.setBounds(640,80,90,25);
add(tf2);
JLabel l2= new JLabel("Nim : ");
l2.setBounds(530,80,90,25);
add(l2);
tf3=new JTextField();
tf3.setBounds(640,110,90,25);
add(tf3);
JLabel l3= new JLabel("IPK : ");
l3.setBounds(530,110,90,25);
add(l3);
tf4=new JTextField();
tf4.setBounds(640,140,90,25);
add(tf4);
JLabel l4= new JLabel("Hapus Baris ke ");
l4.setBounds(530,140,120,25);
add(l4);
}
public void addRow()
{
Vector r;
r = createBlankElement();
rows.addElement(r);
table.addNotify();
}
public void addRow(String [] data)
{
Vector r=new Vector();
r = isi(data);
rows.addElement(r);
table.addNotify();
}
public Vector createBlankElement()
{
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}
public Vector isi(String[] data) {
Vector t = new Vector();
for(int j=0;j<3;j++){
t.addElement((String) data[j]);
}
return t;
}
public void addColumns(String[] colName) {
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}
void deleteRow(int index) {
if(index!=-1) {
rows.removeElementAt(index);
table.addNotify();
}
}
@Override
public void actionPerformed(ActionEvent e) {
try{
if(e.getSource()==b1){
data[0]=tf1.getText()+" "+index;
n[index]=new Nama(data[0],index);
data[1]=tf2.getText();
nim[index]=new Nama(data[1],index);
data[2]=tf3.getText()+rows.size();
ip[index]=new Nama (data[2],index);
c=c+1;
index=index+1;
addRow(data);
}
if(e.getSource()==b2){
int i;
i=Integer.parseInt(tf4.getText());
deleteRow(i);
// for(;i<rows.size();i++){
// n[i]=n[i+1];
// }
}
if(e.getSource()==b3){
mS.merge_sort(0, rows.size()-1, n);
temp.setSize(rows.size());
for(int i=0;i<index;i++){
temp.set(i, rows.get(n[i].ind()));
}
for(int i=0;i<index;i++){
rows.set(i, temp.get(i));
}
}
if(e.getSource()==b4){
rows.setSize(0);
temp.setSize(0);
for(int i=0;i<index;i++)n[i]=null;
index=0;
}
if(e.getSource()==b5){
mS.merge_sort(0, rows.size()-1, nim);
temp.setSize(rows.size());
for(int i=0;i<rows.size();i++){
temp.set(i, rows.get(nim[i].ind()));
}
for(int i=0;i<rows.size();i++){
rows.set(i, temp.get(i));
}
}
if(e.getSource()==b6){
mS.merge_sort(0, rows.size()-1, ip);
temp.setSize(rows.size());
for(int i=0;i<rows.size();i++){
temp.add(i, rows.get(ip[i].ind()));
}
for(int i=0;i<rows.size();i++){
rows.set(i, temp.get(i));
}
}
repaint();
}
catch (Throwable t){
JOptionPane.showMessageDialog(null, "AAAAAA");
}
}
}
框架类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Kareem
*/
public class Frame extends JFrame {
public Frame(){
super("Penghitung Gaji");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1280, 800);
this.getContentPane().add(new Panel());
this.setVisible(true);
}
public static void main(String[] args) {
Frame frame = new Frame();
}
}
谢谢。对不起我的英语不好。