Java 我的列名没有';我的JTable中没有显示
当我运行这个程序时,我的列名不会显示。 我试图在谷歌上搜索这个错误,但似乎没有人有这个错误。 我已经为数据和列名创建了一个成功的数组,但它仍然不起作用Java 我的列名没有';我的JTable中没有显示,java,swing,jtable,jscrollpane,Java,Swing,Jtable,Jscrollpane,当我运行这个程序时,我的列名不会显示。 我试图在谷歌上搜索这个错误,但似乎没有人有这个错误。 我已经为数据和列名创建了一个成功的数组,但它仍然不起作用 public class graphProgram extends JFrame { private JPanel inputPanel, graphPanel, buttonPane; private JTextField equationInput; private JLabel equationLabel;
public class graphProgram extends JFrame {
private JPanel inputPanel, graphPanel, buttonPane;
private JTextField equationInput;
private JLabel equationLabel;
private JTable x_yValues;
private JButton calculateButton;
public graphProgram() {
super("Graph Calculator");
setSize(500, 500);
setLayout(new BorderLayout());
addComponents();
setVisible(true);
}
public void addComponents() {
String[] columnNames = {"X Values", "Y Values"};
Object[][] data = {{new Integer(-2), ""},
{new Integer(-1), ""},
{new Integer(0), ""},
{new Integer(1), ""},
{new Integer(2), ""},
{new Integer(3 ), ""}};
equationInput = new JTextField();
calculateButton = new JButton("Calculate");
equationLabel = new JLabel("Equation: ");
x_yValues = new JTable(data, columnNames);
inputPanel = new JPanel(new GridLayout(0, 2));
graphPanel = new JPanel(new GridLayout(0, 2, 5, 0));
buttonPane = new JPanel(new GridLayout(0, 1));
add(inputPanel, BorderLayout.NORTH);
add(graphPanel, BorderLayout.CENTER);
add(buttonPane, BorderLayout.SOUTH);
inputPanel.add(equationLabel);
inputPanel.add(equationInput);
graphPanel.add(x_yValues);
buttonPane.add(calculateButton);
}
}
您必须将
JTable
放入滚动窗格中
:
x_yValues = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(x_yValues);
尝试使用
JScrollPane
作为表的容器
JScrollPane scrollPane = new JScrollPane(x_yValues);
graphPanel.add(scrollPane); // replaces graphPanel.add(x_yValues);
您以后必须将滚动窗格添加到图形面板,而不是JTable。@ShaheedulIslam,是的,这是正确的解决方案(1+)。实现它的更简单的方法是替换:
graphPanel.add(x_y值)使用graphPanel.add(新的JScrollPane(x_y值))代码>。如果它不起作用,那么您的代码中还有一些其他问题没有发布。如果你需要更多的帮助,那么发布一个适当的说明问题的帖子。