📄 w.java
字号:
charname=table.getValueAt(i,j).toString();
bout.write(charname);
bout.write(" ");
}
bout.newLine();
}
bout.close();
out.close();
}catch(NullPointerException e) {
i++;
JOptionPane.showMessageDialog(table," 第 "+i+" 行 有空值 !","非法操作",JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}catch(IOException e){ System.err.println(e);}
return true;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
dt.addRow(data);
}
if(e.getSource()==b2){
if(selectedRow==-1) selectedRow++;
dt.insertRow(selectedRow,data);
}
if(e.getSource()==b3){
final JFileChooser fa = new JFileChooser();
fa.setLocation(500,450);
fa.setDialogTitle("存表");
int returnVal = fa.showSaveDialog(this);
File file=fa.getSelectedFile();
filename=file.getPath();
savefile(filename);
if(ss){
setVisible(false);}
}
if(e.getSource()==b4)
{
if ( selectedRow == -1 )
selectedRow++;
dt.removeRow ( selectedRow ) ;
}
if(e.getSource()==b5)
{
setVisible(false);
}
}
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
}
////////////////////////////////////////////////////////////
////读数据类
class openfile extends JFrame implements ActionListener{
JTable table;
Object []dat;
Object [][]p;
String[] data;
JButton b1,b2,b3,b4,b5;
FileReader read;
BufferedReader bout;
JPanel p1,p2;
int i=1,j=0,t=0;
DefaultTableModel dt;
String s1="";
String a[];
String fname=null;
int selectedRow;
LinkedList ls1;
JPopupMenu popup;
boolean aa=false;
String filename=null;
int lenght;
openfile(String filename)
{
super("展示表格内容");
fname=filename;
p1=new JPanel();
p2=new JPanel();
b1=new JButton("返回");
b1.addActionListener(this);
b2=new JButton("添加");
b2.addActionListener(this);
b3=new JButton("插入");
b3.addActionListener(this);
b4=new JButton("删除");
b4.addActionListener(this);
b5=new JButton("保存");
b5.addActionListener(this);
try{
read=new FileReader(filename);
bout=new BufferedReader(read);
while((s1=bout.readLine())!=null)
{
StringTokenizer fenxi=new StringTokenizer(s1," ");
int n=fenxi.countTokens();
a=new String[n];
for(j=0;j<n;j++)
{
String temp=fenxi.nextToken();
a[j]=temp;
}
if(i==1)
{
dt=new DefaultTableModel(a,0);
table = new JTable(dt);
}
else
{
dat=a;
dt.insertRow(i-2,dat);
}
i++;
}
bout.close();
read.close();
}
catch(IOException e)
{
System.out.println(e);
}
//
JScrollPane scrollPane = new JScrollPane(table);
p1.add(scrollPane);
p2.setLayout(new FlowLayout());
p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
table.setPreferredScrollableViewportSize(new Dimension(370, 180));
setBounds(250,200,400,300);
table.setSelectionMode ( ListSelectionModel.SINGLE_SELECTION );
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener ( new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if ( e.getValueIsAdjusting() ) return;
ListSelectionModel lsm = ( ListSelectionModel ) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
}
});
//
int i1 = 0,len1;
String name1;
ls1 = new LinkedList () ;
int rowNum1 = table.getColumnCount();
try{
for ( i1 = 0 ; i1 < rowNum1 ; i1++ )
{
name1 = table.getColumnName(i1) . toString () ;
ls1.addLast ( name1 ) ;
}
}
catch ( NullPointerException e )
{
i1++;
JOptionPane.showMessageDialog
( create.c," 第 "+i1+" 行 有空值 !","ererr",
JOptionPane.ERROR_MESSAGE ) ;
}
}
public boolean savefile(String filename)
{
int i=0,j=0;
String s=null;
try{
FileWriter out=new FileWriter(filename);
BufferedWriter bout=new BufferedWriter(out);
String charname;
int rowNum=table.getRowCount();
try{
for(j=0;j<ls1.size();j++)
{
s=ls1.get(j).toString();
bout.write(s);
bout.write(" ");
}
bout.newLine();
for(i=0;i<rowNum;i++){
for(j=0;j<ls1.size();j++)
{
charname=table.getValueAt(i,j).toString();
bout.write(charname);
bout.write(" ");
}
bout.newLine();
}
bout.close();
out.close();
}catch(NullPointerException e) {
i++;
JOptionPane.showMessageDialog(table," 第 "+i+" 行 有空值 !","非法操作",JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}catch(IOException e){ System.err.println(e);}
return true;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1){setVisible(false);}
if(e.getSource()== b2){dt.addRow(data);}
if( e.getSource()== b3 )
{
if ( selectedRow == -1 )
selectedRow++;
dt.insertRow ( selectedRow,data ) ;
}
if(e.getSource()== b4)
{
if ( selectedRow == -1 )
selectedRow++;
dt.removeRow ( selectedRow ) ;
}
if( e.getSource()== b5 )
{
if(fname!=null)
savefile(fname);
if(aa)
{
setVisible(false);
}
}
}
class PopupListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e)
{
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e)
{
if(e.isPopupTrigger())
{
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
}
class suoyin extends JFrame implements ActionListener
{
JTable table;
JLabel l1;
Object []data,data1[];
Object [][]p;
JButton b1,b2,b3;
JTextField t1;
TextArea ta;
JPanel p1,p2,p3,p4;
FileReader read;
BufferedReader bout;
int i=1,j=0,t=0,number=0,colnumber=-1;
DefaultTableModel dt,dt1,sdt;
String s="123456";
String a[],b[];
openfile file;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1){}
if(e.getSource()==b2){setVisible(false);}
}
}
///////////////////////////////////////////////////////////////////////////
///获取内容
class field implements Serializable{
private String name; //字段名字
private String type; //字段类型
private int len; //字段长度
public field ( String name,String type,int len ) {
this.name = name;
this.type = type;
this.len = len;
}
public String getName () {
return name;
}
public String getType () {
return type;
}
public int getLen () {
return len;
}
}
class w
{
static JieMian fr;
public static void main(String args[])
{
fr=new JieMian();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -