是這樣的一個資料 {13,12,12} 怎麼取??? 我想把13取出來 進行排序然後在吧資料儲存到JTable 裡面!!!!
這是我的代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class JTableApp {
JTable table;
int i;
JFrame f;
private String xuehao;
private String Name;
private String chengji;
JTextField jt_1;
JTextField jt_2;
JTextField jt_3;
JDialog jd;
Vector rowData;
public JTableApp(){
f = new JFrame();
Vector columnNames = new Vector();//建立各項名稱
columnNames.add("學号");
columnNames.add("姓名");
columnNames.add("成績");
rowData = new Vector();//學生資訊資料主體
///讀取資料/
try{
FileInputStream f=new FileInputStream("data.txt");
ObjectInputStream s=new ObjectInputStream(f);
rowData=(Vector)s.readObject();
s.close();
}catch(IOException e){}
catch(ClassNotFoundException e){}
JPanel pane=new JPanel(new BorderLayout());//定義頂層容器
JPanel pane0=new JPanel(new BorderLayout());
JPanel pane1=new JPanel(new FlowLayout());
//"添加"按鈕
JButton a=new JButton("添加");
final JButton n=new JButton("排序");
a.setToolTipText("單擊進行添加");
a.setFont(new Font("",Font.PLAIN,15));
a.setBackground(new Color(175,215,136));
//a.updateUI();
//編寫添加監聽器
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jd=new JDialog(f,"輸入",false,null);//頂層面闆
JPanel pane_0=new JPanel(new GridLayout(0,1));
//裝載JTabel和TextField的面闆
JPanel pane_1=new JPanel(new FlowLayout());
JPanel pane_2=new JPanel(new FlowLayout());
JPanel pane_3=new JPanel(new FlowLayout());
JPanel pane_4=new JPanel(new FlowLayout());
JPanel pane_5=new JPanel(new FlowLayout());
JPanel pane_6=new JPanel(new FlowLayout());
JPanel pane_7=new JPanel(new FlowLayout());
//設定面闆L&F
final JButton b1=new JButton("确定");
final JButton b2=new JButton("取消");
//設定按鈕的L&F
b1.setFont(new Font("",Font.BOLD,15));
b2.setFont(new Font("",Font.BOLD,15));
b1.setBackground(new Color(175,215,136));
b2.setBackground(new Color(175,215,136));
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
xuehao=jt_1.getText();//擷取學号
Name=jt_2.getText();//擷取姓名
chengji=jt_3.getText();//擷取成績
String ss[]=new String[3];
ss[0]=xuehao;
ss[1]=Name;
ss[2]=chengji;
int h;
boolean bb=false;
for(h=0;h
System.out.println(ss[h]);
if(ss[h].equals("")){
bb=true;
break;
}else{bb=false;}
}
if(bb){
JOptionPane.showMessageDialog(b1,"輸入不能有為空!","Error",JOptionPane.ERROR_MESSAGE);
}
else{
int s4 = rowData.size();
System.out.println("s4="+s4);
Vector rowData1 = new Vector();
rowData1.add(xuehao);
rowData1.add(Name);
rowData1.add(chengji);;
if(s4!=0){
rowData.add(i+1,rowData1);}
else{
rowData.add(rowData1);
}
table.updateUI();
JOptionPane.showMessageDialog(b1,"添加成功!","Information",JOptionPane.INFORMATION_MESSAGE);
//System.out.println(num);
jd.removeNotify();
}
//存取資料
try{
ObjectOutputStream fout1=new ObjectOutputStream(new FileOutputStream
("data.txt"));
fout1.writeObject(rowData);
fout1.close();
}catch(IOException e){}
///存取結束///
}
});
///按鈕b2的監聽器結束
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
jd.removeNotify();//關閉JDialog
}
});
JLabel jl_1=new JLabel("學号");
JLabel jl_2=new JLabel("姓名");
JLabel jl_3=new JLabel("成績");
jl_1.setFont(new Font("",Font.BOLD,15));
jl_2.setFont(new Font("",Font.BOLD,15));
jl_3.setFont(new Font("",Font.BOLD,15));
jt_1=new JTextField(20);
jt_2=new JTextField(20);
jt_3=new JTextField(20);
//排序
n.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
//裝載JTabel和JTextField
pane_2.add(jl_1);
pane_2.add(jt_1);
pane_3.add(jl_2);
pane_3.add(jt_2);
pane_4.add(jl_3);
pane_4.add(jt_3);
pane_6.add(b1);
pane_6.add(b2);
//裝載JTabel和JTextField結束
/将裝載了JTabelJ和TextField的面闆裝載到頂層面闆
pane_0.add(pane_7);
pane_0.add(pane_1);
pane_0.add(pane_2);
pane_0.add(pane_3);
pane_0.add(pane_4);
pane_0.add(pane_5);
pane_0.add(pane_6);
///裝載結束/
jd.getContentPane().add(pane_0);
//jd.setVisible(true);方此位置不能顯示
jd.setSize(420,500);
jd.setLocation(160,400);
jd.setVisible(true);
jd.pack();
}
});
///對的table進行系列設定//
table=new JTable(rowData,columnNames);
//table.CellEditor(rowData);
table.setPreferredScrollableViewportSize(new Dimension(600,200));//設定此表視口的首選大小
table.setRowHeight(30);
table.setFont(new Font("colunmNames",Font.PLAIN,15));
table.setForeground(new Color(54,117,23));//前端顯示
table.setGridColor(new Color(238,124,107));
table.setSelectionBackground(new Color(175,215,136));
table.setSelectionForeground(new Color(54,117,23));
//傳回滑鼠單擊表格所在的行數
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
i=table.getSelectedRow();
table.getSelectedColumn();
table.updateUI();
存儲修改後得結果
try{
ObjectOutputStream fout1=new ObjectOutputStream(new FileOutputStream
("data.txt"));
fout1.writeObject(rowData);
fout1.close();
}catch(IOException e3){}
System.out.println(i);
}
});
///設定結束
JScrollPane scrollPane=new JScrollPane(table);//添加表格
//添加按鈕
pane1.add(a);
pane1.add(n);
pane0.add(scrollPane,BorderLayout.CENTER);
pane0.add(pane1,BorderLayout.SOUTH);
pane.add(pane0,BorderLayout.CENTER);
f.getContentPane().add(pane,BorderLayout.CENTER);
f.setTitle("通訊錄管理程式");
f.pack();
f.setVisible(true);
f.setLocation(300,300);
//setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new JTableApp();
}
}