我正在制作一个随机应用,我想让它为电子表格制作表格。到目前为止我制作了一些代码,但我的JTable似乎没有显示出来。我做错了什么?这是我的代码:
import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;
public class Screen {
private static JFrame frame;
public JFileChooser choicer = new JFileChooser();
public Font f = new Font("Candara", Font.PLAIN, 16);
public TableMethods tm = new TableMethods();
String dir;
JTable Table;
String[] columns;
String[][] data;
JScrollPane jsp;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Screen window = new Screen();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Screen() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("Spreadr");
frame.setBounds(100, 100, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu FileMenu = new JMenu("File");
FileMenu.setFont(f);
menuBar.add(FileMenu);
JMenuItem New_Doc = new JMenuItem("New Document");
New_Doc.setFont(f);
New_Doc.addActionListener(e -> {
choicer.setCurrentDirectory(new java.io.File("."));
choicer.setDialogTitle("New Document");
choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
choicer.setAcceptAllFileFilterUsed(false);
if (choicer.showOpenDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
dir = String.valueOf(choicer.getCurrentDirectory());
}
});
FileMenu.add(New_Doc);
JMenuItem Save_Doc = new JMenuItem("Save Document");
Save_Doc.setFont(f);
FileMenu.add(Save_Doc);
JMenuItem Open_Doc = new JMenuItem("Open Document");
Open_Doc.setFont(f);
FileMenu.add(Open_Doc);
JMenu EditMenu = new JMenu("Edit");
EditMenu.setFont(f);
menuBar.add(EditMenu);
JMenuItem Copy = new JMenuItem("Copy");
Copy.setFont(f);
EditMenu.add(Copy);
JMenuItem Cut = new JMenuItem("Cut");
Cut.setFont(f);
EditMenu.add(Cut);
JMenuItem Paste = new JMenuItem("Paste");
Paste.setFont(f);
EditMenu.add(Paste);
EditMenu.addSeparator();
JMenuItem EditRows = new JMenuItem("Edit Rows...");
EditRows.setFont(f);
JMenuItem EditColumns = new JMenuItem("Edit Columns...");
EditColumns.setFont(f);
EditMenu.add(EditColumns);
EditMenu.add(EditRows);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
Table = new JTable();
Table = tm.newTable(Table);
jsp = new JScrollPane(Table);
frame.add(jsp);
}
class TableMethods {
public JTable newTable(JTable table) {
int i = 1;
columns = new String[10];
while (i <= 10) {
columns[i - 1] = String.valueOf(i);
}
i = 0;
int j = 0;
while (i < 10) {
while (j < 2) {
data[i][j] = String.valueOf(i + j);
j++;
}
i++;
}
table = new JTable(data, columns);
table.setFillsViewportHeight(false);
table.setDragEnabled(false);
frame.getContentPane().add(table);
return table;
}
}
}
我觉得我的布局有问题。我根据@ClarkKent的要求进行了一些更改,但现在应用程序完全是白色的。
答案 0 :(得分:2)
发现了一些问题。屏幕不会显示,因为您从未增加i,也从未在newTable方法中初始化数据。这会产生无限循环,导致屏幕无法显示。修复无限循环后,您将获得空指针异常。
import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;
public class Screen {
private JFrame frame;
public JFileChooser choicer = new JFileChooser();
public Font f = new Font("Candara", Font.PLAIN, 16);
public TableMethods tm = new TableMethods();
String dir;
JTable Table;
String[] columns;
String[][] data;
JScrollPane jsp;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Screen window = new Screen();
//window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Screen() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("Spreadr");
frame.setBounds(100, 100, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu FileMenu = new JMenu("File");
FileMenu.setFont(f);
menuBar.add(FileMenu);
JMenuItem New_Doc = new JMenuItem("New Document");
New_Doc.setFont(f);
New_Doc.addActionListener(e -> {
choicer.setCurrentDirectory(new java.io.File("."));
choicer.setDialogTitle("New Document");
choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
choicer.setAcceptAllFileFilterUsed(false);
if (choicer.showOpenDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
dir = String.valueOf(choicer.getCurrentDirectory());
}
});
FileMenu.add(New_Doc);
JMenuItem Save_Doc = new JMenuItem("Save Document");
Save_Doc.setFont(f);
FileMenu.add(Save_Doc);
JMenuItem Open_Doc = new JMenuItem("Open Document");
Open_Doc.setFont(f);
FileMenu.add(Open_Doc);
JMenu EditMenu = new JMenu("Edit");
EditMenu.setFont(f);
menuBar.add(EditMenu);
JMenuItem Copy = new JMenuItem("Copy");
Copy.setFont(f);
EditMenu.add(Copy);
JMenuItem Cut = new JMenuItem("Cut");
Cut.setFont(f);
EditMenu.add(Cut);
JMenuItem Paste = new JMenuItem("Paste");
Paste.setFont(f);
EditMenu.add(Paste);
EditMenu.addSeparator();
JMenuItem EditRows = new JMenuItem("Edit Rows...");
EditRows.setFont(f);
JMenuItem EditColumns = new JMenuItem("Edit Columns...");
EditColumns.setFont(f);
EditMenu.add(EditColumns);
EditMenu.add(EditRows);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
Table = new JTable();
Table = tm.newTable(Table);
jsp = new JScrollPane(Table);
frame.add(jsp);
frame.pack();
frame.setVisible(true);
}
class TableMethods {
public JTable newTable(JTable table) {
int i = 1;
columns = new String[10];
while (i <= 10) {
columns[i++ - 1] = String.valueOf(i); //Increment i
}
data = new String[10][10]; //Initialize data.
i = 0;
int j = 0;
while (i < 10) {
j=0; //Set j to 0.
while (j < 2) {
data[i][j] = String.valueOf(i + j);
j++;
}
i++;
}
table = new JTable(data, columns);
table.setFillsViewportHeight(false);
table.setDragEnabled(false);
frame.getContentPane().add(table);
return table;
}
}
}