代码如下
import java.awt.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.*;
public class ResultDisplayTable
{
protected MainFrame mainFrame;
private int rowNum;
private int i,j;
private int colNum=0;
private int[][] cellData;
private String[] columnNames;
public ResultDisplayTable(MainFrame mainFrame,int rowNum)
{
this.mainFrame=mainFrame;
this.rowNum=rowNum;
this.colNum=this.rowNum+1;
this.cellData = new int[this.rowNum][this.colNum];
this.columnNames = new String[this.colNum];
this.Table();
}
public ResultDisplayTable(MainFrame mainFrame,int rowNum,int[][] cellData)
{
this.mainFrame=mainFrame;
this.rowNum=rowNum;
//this.colNum=this.rowNum+1;
this.colNum = cellData[0].length;
this.cellData = new int[this.rowNum][this.colNum];
this.columnNames = new String[this.colNum];
this.cellData = cellData;
this.Table();
}
private JTable mainTable;
private JTable fixedTable;
private void Table()
{
//for(i=0;i<this.rowNum;i++)
//{
// for(j=1;j<colNum;j++){System.out.println(this.cellData[i][j]);}
// System.out.println();
//}
for(i=0;i<this.rowNum;i++)
{
this.cellData[i][0]=i+1;
}
this.columnNames[0]="#";
for (j = 1; j < this.columnNames.length; j++)
{
this.columnNames[j] = String.valueOf(j);
}
TableModel fixedColumnModel = new DefaultTableModel()
{
public int getColumnCount()
{
return 1;
}
public String getColumnName(int column)
{
return columnNames[column];
}
public int getRowCount()
{
return cellData.length;
}
public Object getValueAt(int row, int column)
{
return cellData[row][column];
}
};
TableModel mainModel = new DefaultTableModel()
{
public int getColumnCount()
{
return columnNames.length - 1;
}
public String getColumnName(int column)
{
return columnNames[column + 1];
}
public int getRowCount()
{
return cellData.length;
}
public Object getValueAt(int row, int column)
{
return cellData[row][column + 1];
}
};
fixedTable = new JTable(fixedColumnModel);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setRowHeight(30);
fixedTable.getColumnModel().getColumn(0).setPreferredWidth(30);
DefaultTableCellRenderer dtc=new DefaultTableCellRenderer();
dtc.setBackground(new Color(207,245,254));
fixedTable.getColumnModel().getColumn(0).setCellRenderer(dtc);
mainTable = new JTable(mainModel);
mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
ListSelectionModel model = fixedTable.getSelectionModel();
mainTable.setSelectionModel(model);
mainTable.setRowHeight(30);
mainTable.getTableHeader().setFont(new Font("Arial",Font.PLAIN,18));
mainTable.getTableHeader().setBackground(new Color(207,245,254));
for(j = 0; j < this.columnNames.length-1; j++)
{
mainTable.getColumnModel().getColumn(j).setPreferredWidth(35);
}
for(i=0;i<this.rowNum;i++)
{
mainTable. getCellEditor(i,0).getTableCellEditorComponent(fixedTable,i+1,false,i,0).setFont(new Font("Arial",Font.PLAIN,18));
}
JScrollPane scrollPane = new JScrollPane(mainTable);
Dimension fixedSize = fixedTable.getPreferredSize();
JViewport viewport = new JViewport();
viewport.setView(fixedTable);
viewport.setPreferredSize(fixedSize);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
scrollPane.setRowHeaderView(viewport);
fixedTable.repaint();
fixedTable.validate();
mainTable.repaint();
mainTable.validate();
this.mainFrame.add(scrollPane);
this.mainFrame.validate();
}
}
初始会生成一个50*51的表格,然后在主窗口中点“打开”之后,打开一个100*101大小的矩阵,并显示在表中。现在的问题是矩阵能显示,表也可以成矩阵大小。但是当我拖动那个滚动条之后,显示的表格就会从100*101变成50*51,即初始大小。而且显示的数据也没有了。哪位达人知道哪里有错,谢谢了
7 个解决方案
#1
那个帖子80分还没有结,解决的话我一起结帖
#2
在用到這個類的時候 不會new出了多個對象吧?
#3
不会的,另一个是在一个事件里才会生成
#4
这种程序真的想让别人帮忙
麻烦写个main函数
麻烦写个main函数
#5
这个类是用来调用的,所以就没写main函数
我把那个主类简化一下发这,达人帮我看看,用到的两个类,加上上一个,一用三个
这个是主类MainFrame.java
import java.lang.*;
import java.util.*;
public class MainFrame extends JFrame
{
public static void main(String args[])
{
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
mainFrame.setResizable(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public int i,j;
public int rowNum=30;
int colNum=rowNum+1;
int[][] cellData = new int[rowNum][colNum];
String[] columnNames = new String[colNum];
private TableModel mainModel;
private JTable mainTable;
private JTable fixedTable;
public MainFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int SW=screenSize.width;
int SH=screenSize.height;
setTitle("Main Frame");
setBounds(SW/8,SH/20,3*SW/4,9*SH/10);
JMenu fileMenu=new JMenu("File");
JMenuItem openItem=new JMenuItem("Open");
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openFile(e);
}
});
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitFrame(e);
}
});
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
fileMenu.addSeparator();
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum);
}
void openFile(ActionEvent e)
{
JFileChooser chooser = new JFileChooser(".");
FileFilter type = new ExtensionFilter("Text File", new String[] { ".txt", ".doc" });
chooser.addChoosableFileFilter(type);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type);
int status = chooser.showOpenDialog(MainFrame.this);
File f_1;
String fileName="";
if (status == JFileChooser.APPROVE_OPTION)
{
f_1 = chooser.getSelectedFile();
fileName=f_1.getAbsolutePath();
}
try
{
ProcessFile pf=new ProcessFile(this,fileName);
rowNum=pf.getRowNum();
colNum=rowNum+1;
cellData = new int[rowNum][colNum];
System.arraycopy(pf.getCellData(),0, cellData,0,pf.getCellData().length);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum,cellData);
}catch(IOException ee) {
ee.printStackTrace();
}
}
void exitFrame(ActionEvent e)
{
System.exit(0);
}
public class ExtensionFilter extends FileFilter
{
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension)
{
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[])
{
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file)
{
if (file.isDirectory())
{
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++)
{
String ext = extensions[i];
if (path.endsWith(ext) && (path.charAt(path.length() - ext.length()) == '.'))
{
return true;
}
}
return false;
}
public String getDescription()
{
return (description == null ? extensions[0] : description);
}
}
}
[/code]
我把那个主类简化一下发这,达人帮我看看,用到的两个类,加上上一个,一用三个
这个是主类MainFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.*;
import javax.swing.ListSelectionModel;
import java.io.IOException;
import java.io.File;[code=Java]
import java.lang.*;
import java.util.*;
public class MainFrame extends JFrame
{
public static void main(String args[])
{
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
mainFrame.setResizable(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public int i,j;
public int rowNum=30;
int colNum=rowNum+1;
int[][] cellData = new int[rowNum][colNum];
String[] columnNames = new String[colNum];
private TableModel mainModel;
private JTable mainTable;
private JTable fixedTable;
public MainFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int SW=screenSize.width;
int SH=screenSize.height;
setTitle("Main Frame");
setBounds(SW/8,SH/20,3*SW/4,9*SH/10);
JMenu fileMenu=new JMenu("File");
JMenuItem openItem=new JMenuItem("Open");
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openFile(e);
}
});
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitFrame(e);
}
});
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
fileMenu.addSeparator();
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum);
}
void openFile(ActionEvent e)
{
JFileChooser chooser = new JFileChooser(".");
FileFilter type = new ExtensionFilter("Text File", new String[] { ".txt", ".doc" });
chooser.addChoosableFileFilter(type);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type);
int status = chooser.showOpenDialog(MainFrame.this);
File f_1;
String fileName="";
if (status == JFileChooser.APPROVE_OPTION)
{
f_1 = chooser.getSelectedFile();
fileName=f_1.getAbsolutePath();
}
try
{
ProcessFile pf=new ProcessFile(this,fileName);
rowNum=pf.getRowNum();
colNum=rowNum+1;
cellData = new int[rowNum][colNum];
System.arraycopy(pf.getCellData(),0, cellData,0,pf.getCellData().length);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum,cellData);
}catch(IOException ee) {
ee.printStackTrace();
}
}
void exitFrame(ActionEvent e)
{
System.exit(0);
}
public class ExtensionFilter extends FileFilter
{
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension)
{
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[])
{
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file)
{
if (file.isDirectory())
{
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++)
{
String ext = extensions[i];
if (path.endsWith(ext) && (path.charAt(path.length() - ext.length()) == '.'))
{
return true;
}
}
return false;
}
public String getDescription()
{
return (description == null ? extensions[0] : description);
}
}
}
[/code]
#6
这个是打开文件的一个类ProcessFile.java
import java.io.*;
import java.util.*;
public class ProcessFile
{
protected MainFrame mainFrame;
private String fileName;
public int count;
public int[][] cellData;
public ProcessFile(MainFrame mainFrame,String fileName) throws IOException
{
this.mainFrame=mainFrame;
this.fileName=fileName;
this.process();
}
public void process() throws IOException
{
try
{
File f=new File(this.fileName);
FileReader fr=new FileReader(f);
long fileLength=f.length();
LineNumberReader lnr=new LineNumberReader(fr);
if(lnr != null)
{
lnr.skip(fileLength);
}
count=lnr.getLineNumber()+1;
String line;
int p=0;
int k;
cellData = new int[count][count+1];
FileReader fr_1=new FileReader(f);
BufferedReader br = new BufferedReader(fr_1);
while((line = br.readLine())!=null && p<count)
{
String[] str = line.split(" ");
for(k=1;k<=str.length;k++)
{
cellData[p][k]=Integer.parseInt(str[k-1]);
}
p++;
}
br.close();
}catch (IOException e) {
e.printStackTrace();
}
}
public int getRowNum()
{
return count;
}
public int[][] getCellData()
{
return cellData;
}
}
#7
那个存储矩阵的文本文件1.txt
这个文件存储复制粘贴存储,达人帮我看看,谢谢了,搞了半个月不知道什么问题
21 92 28 93 04 73 40 46 61 92 31 42 38 84 59 05 69 57 14 18 63 71 84 05 35 69 10 79 69 25 71 61 48 77 29 66 07 37 23 73 20 68 77 97 36 67 73 56 84 72
25 42 23 97 48 86 76 31 59 50 16 14 64 26 47 49 58 13 68 55 03 57 83 96 73 87 70 83 74 28 10 45 12 09 17 11 51 74 26 52 51 74 30 76 49 05 77 12 31 67
90 17 79 39 75 56 03 67 78 35 89 26 26 70 35 55 06 26 85 17 06 48 91 05 90 07 93 43 20 07 51 38 17 91 98 28 11 35 34 62 74 77 71 39 28 48 23 33 71 53
85 49 49 69 28 12 65 32 59 85 23 64 33 99 55 77 96 53 92 91 96 63 27 76 99 86 85 57 09 17 57 76 86 58 08 77 48 91 00 82 26 43 73 82 44 07 22 89 79 26
34 38 81 54 61 00 13 95 20 44 90 33 22 42 13 60 96 38 83 02 32 92 30 88 83 33 77 46 17 64 18 49 14 52 74 62 89 32 61 09 18 37 04 16 31 85 88 17 50 49
56 31 34 28 30 63 16 90 47 98 20 91 73 24 49 13 01 29 60 09 88 08 56 93 45 90 59 66 70 63 18 85 41 75 77 38 05 25 27 82 08 28 12 74 39 08 96 57 06 11
40 85 33 63 60 59 91 69 28 46 12 67 69 71 53 56 25 28 64 07 33 73 00 11 19 05 26 79 73 72 53 06 16 18 63 18 86 67 63 97 65 81 51 16 35 78 63 58 44 63
95 09 06 66 22 69 92 81 49 34 13 96 55 97 24 86 28 85 27 46 09 95 89 40 16 23 69 50 37 88 51 93 29 15 92 31 25 44 95 28 61 10 37 63 23 36 00 23 37 77
42 71 11 44 19 47 66 61 35 49 99 61 73 03 32 31 54 60 29 84 34 65 92 31 57 57 41 31 60 84 69 60 15 23 18 94 07 58 50 22 22 27 22 14 41 89 24 48 43 46
76 70 00 81 29 78 97 98 73 82 03 32 95 26 38 81 06 88 02 47 92 99 78 25 41 44 04 59 19 39 89 86 95 51 42 42 14 85 46 54 86 23 41 53 41 01 10 44 56 26
27 33 04 98 76 97 91 66 73 62 97 64 23 13 98 18 63 28 63 62 32 75 01 98 48 93 60 69 77 65 66 37 98 21 23 70 01 56 74 04 83 31 89 18 18 45 02 93 46 87
22 39 22 56 72 62 43 20 43 25 83 23 83 90 48 70 68 66 58 46 48 99 84 68 01 92 59 68 75 49 96 15 32 37 49 16 86 03 88 18 45 55 54 12 62 24 97 92 29 29
18 43 36 50 60 40 52 65 39 39 02 53 13 68 34 69 59 16 92 93 22 94 42 34 02 59 45 01 28 68 79 94 72 73 73 69 66 05 43 17 72 14 85 34 92 89 90 54 87 87
48 62 09 19 09 57 57 64 73 89 00 95 07 27 26 64 62 78 97 64 51 70 44 04 63 75 42 70 33 69 60 96 04 95 21 92 27 90 25 67 18 67 84 54 79 82 08 19 72 11
76 35 42 53 77 52 54 34 33 30 36 17 19 29 97 62 36 28 09 47 69 40 39 55 20 04 29 86 07 07 44 56 71 57 17 92 25 38 30 81 71 26 17 23 68 99 83 61 72 71
60 93 13 03 67 70 01 06 64 45 39 97 36 16 34 14 78 24 92 06 07 75 91 80 58 59 53 86 86 96 35 31 53 74 77 66 13 07 86 62 04 80 30 94 81 95 71 86 19 38
74 23 73 48 81 43 57 86 60 94 76 74 00 52 32 64 28 94 97 42 29 87 41 85 31 20 85 12 28 23 46 39 33 54 25 30 30 70 97 26 41 35 69 32 14 93 50 46 68 02
13 67 45 06 25 11 56 09 32 33 10 51 18 41 86 64 44 87 97 31 48 60 60 45 55 84 23 50 14 15 35 67 05 43 90 69 76 74 42 28 30 38 18 93 12 76 26 53 91 94
12 45 91 48 75 12 48 98 28 87 70 04 37 73 80 78 69 98 22 88 40 28 72 07 20 21 20 39 65 06 26 23 93 32 62 75 33 41 66 92 60 04 09 36 08 74 36 39 77 59
55 28 25 15 23 28 37 33 62 52 03 72 53 64 97 98 31 23 97 38 29 59 84 23 92 03 88 92 25 77 53 53 20 07 34 48 40 38 60 55 30 52 23 70 90 68 92 45 99 65
95 20 89 20 29 01 09 67 32 24 32 84 64 36 28 89 32 61 06 82 95 62 57 08 09 15 66 16 50 96 18 90 96 52 76 93 40 73 57 88 38 93 87 75 21 65 87 23 92 12
97 03 20 34 06 59 88 76 43 07 74 59 45 47 11 03 84 96 55 08 45 77 07 08 91 92 03 68 68 00 17 55 71 51 84 31 88 83 02 51 57 83 40 04 10 14 16 37 62 70
11 04 23 73 20 58 83 35 28 37 89 36 70 39 79 19 18 91 08 81 02 75 89 67 74 76 20 98 02 17 50 28 70 97 40 45 49 29 14 18 29 48 25 84 41 29 08 09 79 58
26 95 62 44 66 34 45 20 79 55 55 46 24 31 72 02 19 20 85 43 62 50 66 08 45 64 32 54 44 15 11 32 56 51 29 75 75 03 90 43 36 35 47 24 75 84 15 75 65 35
96 29 62 39 63 21 73 25 71 80 61 36 41 49 77 18 87 78 11 53 56 97 63 54 71 33 14 65 58 03 95 15 07 08 61 23 63 83 75 24 50 10 35 13 91 73 80 01 54 68
46 71 80 80 46 91 84 51 91 70 48 13 90 76 08 17 07 27 16 06 69 40 87 04 81 93 21 32 33 57 00 88 29 15 78 74 18 08 85 81 54 89 73 17 78 50 84 44 52 41
88 58 60 05 12 25 22 10 93 62 74 34 89 63 01 09 73 03 99 95 15 18 23 06 43 71 36 39 73 00 88 30 92 34 04 64 83 17 75 02 64 83 29 33 52 84 42 64 36 07
75 47 82 29 89 23 88 63 22 95 44 03 02 58 54 51 90 55 20 01 66 47 98 07 51 04 75 08 42 14 64 59 93 51 11 65 47 08 93 28 30 16 07 06 60 50 90 68 56 23
71 93 66 23 09 53 91 55 25 86 97 93 74 90 88 14 17 98 40 71 45 89 67 82 44 25 19 68 22 29 19 49 96 58 63 96 30 09 91 95 86 21 29 20 25 50 28 10 11 79
42 63 15 13 59 98 62 16 51 20 08 35 43 14 84 81 41 33 97 98 96 99 34 44 31 22 63 88 90 44 16 25 56 99 61 06 84 81 78 63 12 89 03 36 18 76 85 62 02 35
69 86 01 98 28 20 36 75 00 62 58 57 28 26 41 08 55 26 21 53 23 93 13 85 45 52 76 84 77 42 04 02 69 05 17 91 30 73 15 74 01 67 47 39 80 85 67 09 34 16
34 76 76 19 39 34 62 20 26 11 55 49 20 22 46 03 63 89 49 47 98 23 14 95 48 82 51 74 86 08 84 73 77 02 17 06 75 75 07 58 14 08 36 73 33 76 71 22 82 78
07 77 14 55 15 16 08 47 86 23 39 23 40 34 20 47 10 63 65 97 20 65 09 95 06 59 48 81 49 97 88 08 37 00 68 77 77 87 23 62 49 26 80 35 11 90 09 01 10 59
97 88 07 38 14 63 74 35 31 49 60 85 26 11 99 78 34 34 69 81 62 43 95 80 27 58 40 24 40 46 94 67 10 24 56 15 05 15 68 58 72 73 27 12 71 37 26 58 72 03
79 46 20 39 73 45 41 45 76 75 69 17 16 25 24 66 68 51 88 14 39 84 77 44 04 49 41 22 88 45 80 14 61 88 88 05 50 24 26 05 83 24 38 74 91 33 03 97 16 04
85 79 06 70 80 80 92 17 43 77 95 12 58 91 04 77 49 95 61 82 56 57 62 11 32 06 98 66 36 73 30 62 51 84 13 94 83 93 83 10 35 03 05 22 07 19 32 77 07 89
82 74 26 04 09 71 59 61 14 17 41 07 08 83 01 98 23 04 19 76 22 45 24 29 76 90 49 40 25 08 35 33 37 08 31 31 92 98 53 92 69 65 04 73 49 67 79 38 22 90
43 12 98 40 79 80 32 56 73 67 64 23 42 14 13 31 64 28 51 33 04 43 28 62 33 36 12 49 39 59 64 83 46 13 64 13 75 66 56 89 02 31 09 21 81 55 20 37 47 44
33 79 15 45 97 55 42 10 01 21 59 57 31 76 92 81 71 10 63 96 10 22 92 48 88 72 63 35 58 05 78 57 39 38 79 43 55 22 60 34 77 75 76 08 18 72 83 94 39 54
55 20 77 00 53 09 24 81 14 38 22 57 12 73 05 72 86 56 09 45 62 84 88 70 46 20 51 58 51 20 46 21 50 89 35 59 89 04 81 63 44 22 27 44 60 55 03 97 51 45
77 94 27 96 32 33 30 25 21 03 68 53 44 43 69 51 43 34 76 58 36 58 45 16 55 57 15 68 74 91 70 21 92 50 23 29 37 73 09 79 45 78 88 72 97 31 27 64 67 88
51 06 76 58 17 40 33 62 08 53 76 37 00 73 38 42 13 41 79 28 15 03 59 08 10 32 52 28 24 34 20 59 36 81 18 64 15 53 31 58 78 32 65 82 62 15 99 18 89 94
06 35 93 38 25 40 67 73 26 83 61 80 31 48 81 34 60 70 22 23 89 66 81 23 97 28 74 71 48 30 27 89 06 22 90 28 73 44 41 60 22 55 84 28 41 18 37 77 49 72
51 25 66 36 10 00 58 45 00 71 48 57 76 18 85 33 64 32 60 35 01 40 70 35 49 21 19 22 50 52 80 94 64 59 15 85 50 58 15 09 37 82 86 43 60 44 83 65 19 54
60 21 69 32 94 99 18 62 09 62 82 21 18 52 23 98 85 55 57 18 95 65 16 58 29 66 14 08 02 44 69 04 76 75 07 40 01 85 72 77 96 63 53 89 44 31 63 61 42 99
55 48 91 18 31 21 04 25 39 63 70 19 19 82 77 33 32 85 99 90 56 08 00 79 19 40 28 94 90 04 23 91 63 16 83 42 94 60 05 62 90 21 23 76 82 84 62 17 57 73
34 01 48 23 99 23 59 14 26 16 07 48 10 64 51 61 01 21 18 59 26 98 79 43 16 90 13 40 44 83 51 77 59 41 46 62 66 19 01 92 00 38 58 20 28 65 13 94 94 13
19 52 16 05 09 37 57 91 66 68 70 92 32 52 24 49 89 95 44 84 66 19 67 51 08 26 76 44 70 20 40 37 44 97 58 94 36 95 66 57 86 60 48 12 81 67 91 78 98 20
37 26 35 44 73 02 34 48 68 65 94 92 72 87 31 81 81 33 45 42 59 38 16 91 21 34 78 74 73 21 46 70 29 82 52 91 83 28 54 61 16 38 66 80 27 69 84 47 86 85
62 22 60 96 36 54 44 27 69 89 06 65 46 59 27 62 35 49 10 11 73 88 27 81 44 75 33 69 80 43 04 81 89 01 32 77 22 13 06 07 54 06 33 64 72 43 32 37 31 95
这个文件存储复制粘贴存储,达人帮我看看,谢谢了,搞了半个月不知道什么问题
#1
那个帖子80分还没有结,解决的话我一起结帖
#2
在用到這個類的時候 不會new出了多個對象吧?
#3
不会的,另一个是在一个事件里才会生成
#4
这种程序真的想让别人帮忙
麻烦写个main函数
麻烦写个main函数
#5
这个类是用来调用的,所以就没写main函数
我把那个主类简化一下发这,达人帮我看看,用到的两个类,加上上一个,一用三个
这个是主类MainFrame.java
import java.lang.*;
import java.util.*;
public class MainFrame extends JFrame
{
public static void main(String args[])
{
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
mainFrame.setResizable(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public int i,j;
public int rowNum=30;
int colNum=rowNum+1;
int[][] cellData = new int[rowNum][colNum];
String[] columnNames = new String[colNum];
private TableModel mainModel;
private JTable mainTable;
private JTable fixedTable;
public MainFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int SW=screenSize.width;
int SH=screenSize.height;
setTitle("Main Frame");
setBounds(SW/8,SH/20,3*SW/4,9*SH/10);
JMenu fileMenu=new JMenu("File");
JMenuItem openItem=new JMenuItem("Open");
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openFile(e);
}
});
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitFrame(e);
}
});
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
fileMenu.addSeparator();
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum);
}
void openFile(ActionEvent e)
{
JFileChooser chooser = new JFileChooser(".");
FileFilter type = new ExtensionFilter("Text File", new String[] { ".txt", ".doc" });
chooser.addChoosableFileFilter(type);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type);
int status = chooser.showOpenDialog(MainFrame.this);
File f_1;
String fileName="";
if (status == JFileChooser.APPROVE_OPTION)
{
f_1 = chooser.getSelectedFile();
fileName=f_1.getAbsolutePath();
}
try
{
ProcessFile pf=new ProcessFile(this,fileName);
rowNum=pf.getRowNum();
colNum=rowNum+1;
cellData = new int[rowNum][colNum];
System.arraycopy(pf.getCellData(),0, cellData,0,pf.getCellData().length);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum,cellData);
}catch(IOException ee) {
ee.printStackTrace();
}
}
void exitFrame(ActionEvent e)
{
System.exit(0);
}
public class ExtensionFilter extends FileFilter
{
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension)
{
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[])
{
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file)
{
if (file.isDirectory())
{
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++)
{
String ext = extensions[i];
if (path.endsWith(ext) && (path.charAt(path.length() - ext.length()) == '.'))
{
return true;
}
}
return false;
}
public String getDescription()
{
return (description == null ? extensions[0] : description);
}
}
}
[/code]
我把那个主类简化一下发这,达人帮我看看,用到的两个类,加上上一个,一用三个
这个是主类MainFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.*;
import javax.swing.ListSelectionModel;
import java.io.IOException;
import java.io.File;[code=Java]
import java.lang.*;
import java.util.*;
public class MainFrame extends JFrame
{
public static void main(String args[])
{
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
mainFrame.setResizable(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public int i,j;
public int rowNum=30;
int colNum=rowNum+1;
int[][] cellData = new int[rowNum][colNum];
String[] columnNames = new String[colNum];
private TableModel mainModel;
private JTable mainTable;
private JTable fixedTable;
public MainFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int SW=screenSize.width;
int SH=screenSize.height;
setTitle("Main Frame");
setBounds(SW/8,SH/20,3*SW/4,9*SH/10);
JMenu fileMenu=new JMenu("File");
JMenuItem openItem=new JMenuItem("Open");
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openFile(e);
}
});
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitFrame(e);
}
});
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
fileMenu.addSeparator();
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum);
}
void openFile(ActionEvent e)
{
JFileChooser chooser = new JFileChooser(".");
FileFilter type = new ExtensionFilter("Text File", new String[] { ".txt", ".doc" });
chooser.addChoosableFileFilter(type);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type);
int status = chooser.showOpenDialog(MainFrame.this);
File f_1;
String fileName="";
if (status == JFileChooser.APPROVE_OPTION)
{
f_1 = chooser.getSelectedFile();
fileName=f_1.getAbsolutePath();
}
try
{
ProcessFile pf=new ProcessFile(this,fileName);
rowNum=pf.getRowNum();
colNum=rowNum+1;
cellData = new int[rowNum][colNum];
System.arraycopy(pf.getCellData(),0, cellData,0,pf.getCellData().length);
ResultDisplayTable rt=new ResultDisplayTable(this,rowNum,cellData);
}catch(IOException ee) {
ee.printStackTrace();
}
}
void exitFrame(ActionEvent e)
{
System.exit(0);
}
public class ExtensionFilter extends FileFilter
{
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension)
{
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[])
{
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file)
{
if (file.isDirectory())
{
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++)
{
String ext = extensions[i];
if (path.endsWith(ext) && (path.charAt(path.length() - ext.length()) == '.'))
{
return true;
}
}
return false;
}
public String getDescription()
{
return (description == null ? extensions[0] : description);
}
}
}
[/code]
#6
这个是打开文件的一个类ProcessFile.java
import java.io.*;
import java.util.*;
public class ProcessFile
{
protected MainFrame mainFrame;
private String fileName;
public int count;
public int[][] cellData;
public ProcessFile(MainFrame mainFrame,String fileName) throws IOException
{
this.mainFrame=mainFrame;
this.fileName=fileName;
this.process();
}
public void process() throws IOException
{
try
{
File f=new File(this.fileName);
FileReader fr=new FileReader(f);
long fileLength=f.length();
LineNumberReader lnr=new LineNumberReader(fr);
if(lnr != null)
{
lnr.skip(fileLength);
}
count=lnr.getLineNumber()+1;
String line;
int p=0;
int k;
cellData = new int[count][count+1];
FileReader fr_1=new FileReader(f);
BufferedReader br = new BufferedReader(fr_1);
while((line = br.readLine())!=null && p<count)
{
String[] str = line.split(" ");
for(k=1;k<=str.length;k++)
{
cellData[p][k]=Integer.parseInt(str[k-1]);
}
p++;
}
br.close();
}catch (IOException e) {
e.printStackTrace();
}
}
public int getRowNum()
{
return count;
}
public int[][] getCellData()
{
return cellData;
}
}
#7
那个存储矩阵的文本文件1.txt
这个文件存储复制粘贴存储,达人帮我看看,谢谢了,搞了半个月不知道什么问题
21 92 28 93 04 73 40 46 61 92 31 42 38 84 59 05 69 57 14 18 63 71 84 05 35 69 10 79 69 25 71 61 48 77 29 66 07 37 23 73 20 68 77 97 36 67 73 56 84 72
25 42 23 97 48 86 76 31 59 50 16 14 64 26 47 49 58 13 68 55 03 57 83 96 73 87 70 83 74 28 10 45 12 09 17 11 51 74 26 52 51 74 30 76 49 05 77 12 31 67
90 17 79 39 75 56 03 67 78 35 89 26 26 70 35 55 06 26 85 17 06 48 91 05 90 07 93 43 20 07 51 38 17 91 98 28 11 35 34 62 74 77 71 39 28 48 23 33 71 53
85 49 49 69 28 12 65 32 59 85 23 64 33 99 55 77 96 53 92 91 96 63 27 76 99 86 85 57 09 17 57 76 86 58 08 77 48 91 00 82 26 43 73 82 44 07 22 89 79 26
34 38 81 54 61 00 13 95 20 44 90 33 22 42 13 60 96 38 83 02 32 92 30 88 83 33 77 46 17 64 18 49 14 52 74 62 89 32 61 09 18 37 04 16 31 85 88 17 50 49
56 31 34 28 30 63 16 90 47 98 20 91 73 24 49 13 01 29 60 09 88 08 56 93 45 90 59 66 70 63 18 85 41 75 77 38 05 25 27 82 08 28 12 74 39 08 96 57 06 11
40 85 33 63 60 59 91 69 28 46 12 67 69 71 53 56 25 28 64 07 33 73 00 11 19 05 26 79 73 72 53 06 16 18 63 18 86 67 63 97 65 81 51 16 35 78 63 58 44 63
95 09 06 66 22 69 92 81 49 34 13 96 55 97 24 86 28 85 27 46 09 95 89 40 16 23 69 50 37 88 51 93 29 15 92 31 25 44 95 28 61 10 37 63 23 36 00 23 37 77
42 71 11 44 19 47 66 61 35 49 99 61 73 03 32 31 54 60 29 84 34 65 92 31 57 57 41 31 60 84 69 60 15 23 18 94 07 58 50 22 22 27 22 14 41 89 24 48 43 46
76 70 00 81 29 78 97 98 73 82 03 32 95 26 38 81 06 88 02 47 92 99 78 25 41 44 04 59 19 39 89 86 95 51 42 42 14 85 46 54 86 23 41 53 41 01 10 44 56 26
27 33 04 98 76 97 91 66 73 62 97 64 23 13 98 18 63 28 63 62 32 75 01 98 48 93 60 69 77 65 66 37 98 21 23 70 01 56 74 04 83 31 89 18 18 45 02 93 46 87
22 39 22 56 72 62 43 20 43 25 83 23 83 90 48 70 68 66 58 46 48 99 84 68 01 92 59 68 75 49 96 15 32 37 49 16 86 03 88 18 45 55 54 12 62 24 97 92 29 29
18 43 36 50 60 40 52 65 39 39 02 53 13 68 34 69 59 16 92 93 22 94 42 34 02 59 45 01 28 68 79 94 72 73 73 69 66 05 43 17 72 14 85 34 92 89 90 54 87 87
48 62 09 19 09 57 57 64 73 89 00 95 07 27 26 64 62 78 97 64 51 70 44 04 63 75 42 70 33 69 60 96 04 95 21 92 27 90 25 67 18 67 84 54 79 82 08 19 72 11
76 35 42 53 77 52 54 34 33 30 36 17 19 29 97 62 36 28 09 47 69 40 39 55 20 04 29 86 07 07 44 56 71 57 17 92 25 38 30 81 71 26 17 23 68 99 83 61 72 71
60 93 13 03 67 70 01 06 64 45 39 97 36 16 34 14 78 24 92 06 07 75 91 80 58 59 53 86 86 96 35 31 53 74 77 66 13 07 86 62 04 80 30 94 81 95 71 86 19 38
74 23 73 48 81 43 57 86 60 94 76 74 00 52 32 64 28 94 97 42 29 87 41 85 31 20 85 12 28 23 46 39 33 54 25 30 30 70 97 26 41 35 69 32 14 93 50 46 68 02
13 67 45 06 25 11 56 09 32 33 10 51 18 41 86 64 44 87 97 31 48 60 60 45 55 84 23 50 14 15 35 67 05 43 90 69 76 74 42 28 30 38 18 93 12 76 26 53 91 94
12 45 91 48 75 12 48 98 28 87 70 04 37 73 80 78 69 98 22 88 40 28 72 07 20 21 20 39 65 06 26 23 93 32 62 75 33 41 66 92 60 04 09 36 08 74 36 39 77 59
55 28 25 15 23 28 37 33 62 52 03 72 53 64 97 98 31 23 97 38 29 59 84 23 92 03 88 92 25 77 53 53 20 07 34 48 40 38 60 55 30 52 23 70 90 68 92 45 99 65
95 20 89 20 29 01 09 67 32 24 32 84 64 36 28 89 32 61 06 82 95 62 57 08 09 15 66 16 50 96 18 90 96 52 76 93 40 73 57 88 38 93 87 75 21 65 87 23 92 12
97 03 20 34 06 59 88 76 43 07 74 59 45 47 11 03 84 96 55 08 45 77 07 08 91 92 03 68 68 00 17 55 71 51 84 31 88 83 02 51 57 83 40 04 10 14 16 37 62 70
11 04 23 73 20 58 83 35 28 37 89 36 70 39 79 19 18 91 08 81 02 75 89 67 74 76 20 98 02 17 50 28 70 97 40 45 49 29 14 18 29 48 25 84 41 29 08 09 79 58
26 95 62 44 66 34 45 20 79 55 55 46 24 31 72 02 19 20 85 43 62 50 66 08 45 64 32 54 44 15 11 32 56 51 29 75 75 03 90 43 36 35 47 24 75 84 15 75 65 35
96 29 62 39 63 21 73 25 71 80 61 36 41 49 77 18 87 78 11 53 56 97 63 54 71 33 14 65 58 03 95 15 07 08 61 23 63 83 75 24 50 10 35 13 91 73 80 01 54 68
46 71 80 80 46 91 84 51 91 70 48 13 90 76 08 17 07 27 16 06 69 40 87 04 81 93 21 32 33 57 00 88 29 15 78 74 18 08 85 81 54 89 73 17 78 50 84 44 52 41
88 58 60 05 12 25 22 10 93 62 74 34 89 63 01 09 73 03 99 95 15 18 23 06 43 71 36 39 73 00 88 30 92 34 04 64 83 17 75 02 64 83 29 33 52 84 42 64 36 07
75 47 82 29 89 23 88 63 22 95 44 03 02 58 54 51 90 55 20 01 66 47 98 07 51 04 75 08 42 14 64 59 93 51 11 65 47 08 93 28 30 16 07 06 60 50 90 68 56 23
71 93 66 23 09 53 91 55 25 86 97 93 74 90 88 14 17 98 40 71 45 89 67 82 44 25 19 68 22 29 19 49 96 58 63 96 30 09 91 95 86 21 29 20 25 50 28 10 11 79
42 63 15 13 59 98 62 16 51 20 08 35 43 14 84 81 41 33 97 98 96 99 34 44 31 22 63 88 90 44 16 25 56 99 61 06 84 81 78 63 12 89 03 36 18 76 85 62 02 35
69 86 01 98 28 20 36 75 00 62 58 57 28 26 41 08 55 26 21 53 23 93 13 85 45 52 76 84 77 42 04 02 69 05 17 91 30 73 15 74 01 67 47 39 80 85 67 09 34 16
34 76 76 19 39 34 62 20 26 11 55 49 20 22 46 03 63 89 49 47 98 23 14 95 48 82 51 74 86 08 84 73 77 02 17 06 75 75 07 58 14 08 36 73 33 76 71 22 82 78
07 77 14 55 15 16 08 47 86 23 39 23 40 34 20 47 10 63 65 97 20 65 09 95 06 59 48 81 49 97 88 08 37 00 68 77 77 87 23 62 49 26 80 35 11 90 09 01 10 59
97 88 07 38 14 63 74 35 31 49 60 85 26 11 99 78 34 34 69 81 62 43 95 80 27 58 40 24 40 46 94 67 10 24 56 15 05 15 68 58 72 73 27 12 71 37 26 58 72 03
79 46 20 39 73 45 41 45 76 75 69 17 16 25 24 66 68 51 88 14 39 84 77 44 04 49 41 22 88 45 80 14 61 88 88 05 50 24 26 05 83 24 38 74 91 33 03 97 16 04
85 79 06 70 80 80 92 17 43 77 95 12 58 91 04 77 49 95 61 82 56 57 62 11 32 06 98 66 36 73 30 62 51 84 13 94 83 93 83 10 35 03 05 22 07 19 32 77 07 89
82 74 26 04 09 71 59 61 14 17 41 07 08 83 01 98 23 04 19 76 22 45 24 29 76 90 49 40 25 08 35 33 37 08 31 31 92 98 53 92 69 65 04 73 49 67 79 38 22 90
43 12 98 40 79 80 32 56 73 67 64 23 42 14 13 31 64 28 51 33 04 43 28 62 33 36 12 49 39 59 64 83 46 13 64 13 75 66 56 89 02 31 09 21 81 55 20 37 47 44
33 79 15 45 97 55 42 10 01 21 59 57 31 76 92 81 71 10 63 96 10 22 92 48 88 72 63 35 58 05 78 57 39 38 79 43 55 22 60 34 77 75 76 08 18 72 83 94 39 54
55 20 77 00 53 09 24 81 14 38 22 57 12 73 05 72 86 56 09 45 62 84 88 70 46 20 51 58 51 20 46 21 50 89 35 59 89 04 81 63 44 22 27 44 60 55 03 97 51 45
77 94 27 96 32 33 30 25 21 03 68 53 44 43 69 51 43 34 76 58 36 58 45 16 55 57 15 68 74 91 70 21 92 50 23 29 37 73 09 79 45 78 88 72 97 31 27 64 67 88
51 06 76 58 17 40 33 62 08 53 76 37 00 73 38 42 13 41 79 28 15 03 59 08 10 32 52 28 24 34 20 59 36 81 18 64 15 53 31 58 78 32 65 82 62 15 99 18 89 94
06 35 93 38 25 40 67 73 26 83 61 80 31 48 81 34 60 70 22 23 89 66 81 23 97 28 74 71 48 30 27 89 06 22 90 28 73 44 41 60 22 55 84 28 41 18 37 77 49 72
51 25 66 36 10 00 58 45 00 71 48 57 76 18 85 33 64 32 60 35 01 40 70 35 49 21 19 22 50 52 80 94 64 59 15 85 50 58 15 09 37 82 86 43 60 44 83 65 19 54
60 21 69 32 94 99 18 62 09 62 82 21 18 52 23 98 85 55 57 18 95 65 16 58 29 66 14 08 02 44 69 04 76 75 07 40 01 85 72 77 96 63 53 89 44 31 63 61 42 99
55 48 91 18 31 21 04 25 39 63 70 19 19 82 77 33 32 85 99 90 56 08 00 79 19 40 28 94 90 04 23 91 63 16 83 42 94 60 05 62 90 21 23 76 82 84 62 17 57 73
34 01 48 23 99 23 59 14 26 16 07 48 10 64 51 61 01 21 18 59 26 98 79 43 16 90 13 40 44 83 51 77 59 41 46 62 66 19 01 92 00 38 58 20 28 65 13 94 94 13
19 52 16 05 09 37 57 91 66 68 70 92 32 52 24 49 89 95 44 84 66 19 67 51 08 26 76 44 70 20 40 37 44 97 58 94 36 95 66 57 86 60 48 12 81 67 91 78 98 20
37 26 35 44 73 02 34 48 68 65 94 92 72 87 31 81 81 33 45 42 59 38 16 91 21 34 78 74 73 21 46 70 29 82 52 91 83 28 54 61 16 38 66 80 27 69 84 47 86 85
62 22 60 96 36 54 44 27 69 89 06 65 46 59 27 62 35 49 10 11 73 88 27 81 44 75 33 69 80 43 04 81 89 01 32 77 22 13 06 07 54 06 33 64 72 43 32 37 31 95
这个文件存储复制粘贴存储,达人帮我看看,谢谢了,搞了半个月不知道什么问题