有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JTable显示为空白

我将只发布重要的部分

我创建了一个JFrame,其中包含一个JPanel,其中包含一些JTextFields、JTextAreas和一个JList。我知道我想添加一个JTable来显示一些结果,但它将显示为空白。我试着查看一些帖子,但没能修复

我手动输入了两行,但它们不会出现。列名也不会。请帮忙

import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class GUI_Automata_Ex_1 extends JFrame {

    public static int ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static int ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

    public static int WindowWidth = ScreenWidth*3/4;
    public static int WindowHeight = WindowWidth*9/16;

    public static int unit = WindowWidth/160;

    // tables used
    static String[] columnNames = {"word", "length", "result"};
    static Object[][] data = {{"abbaa", new Integer (5), "belongs"}, {"baabbb", new Integer (6), "does not belong"}};
    public static JTable table_saved_words = new JTable(data, columnNames);
    public static DefaultTableModel dtm_saved_words = new DefaultTableModel();
    public static JScrollPane sp_saved_words;

    public GUI_Automata_Ex_1 () {

        // this will only run on ultrawide screens (e.g. 21:9 or 32:9) because the window is 16:9 optimized
        if (ScreenWidth/2 > ScreenHeight) {
            WindowHeight = ScreenHeight*3/4;
            WindowWidth = WindowHeight*16/9;
            unit = WindowWidth/160;
        }

        this.setTitle("Automata Theory 1st Excercise");
        this.setBounds(ScreenWidth/2-WindowWidth/2,ScreenHeight/2-WindowHeight/2,WindowWidth,WindowHeight);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(colorBG);

        Board board = new Board();
        this.setContentPane(board);
        this.setLayout(null);

        // TABLES

        // settings for table_saved_words
        table_saved_words.setBackground(Color.white);
        table_saved_words.setFont(fontM);
        table_saved_words.setModel(dtm_saved_words);
        table_saved_words.setPreferredScrollableViewportSize(new Dimension(unit*86, unit*50));
        table_saved_words.setFillsViewportHeight(true);
        sp_saved_words = new JScrollPane(table_saved_words);
        board.add(sp_saved_words);
        sp_saved_words.setBounds(unit*68, unit*32, unit*86, unit*50);
        sp_saved_words.setWheelScrollingEnabled(true);
        sp_saved_words.setViewportView(table_saved_words);
        sp_saved_words.setVisible(false);

        dtm_saved_words.addRow(new Object[]{"aabbbbaa", 5, "belongs"});

    }

    public class Board extends JPanel {
        public void paintComponent (Graphics g) {

        }
    }

}

}

以下是一个屏幕截图:

https://i.stack.imgur.com/AHtLf.png

JTable位于右下角。我没有包括其他J组件的代码部分

在获得要显示的列表后,我想做的只是在窗口运行时添加一些行(用户输入的单词,或者属于字典,或者不属于字典,以及它们的长度),但我一直坚持的部分是让JTable显示列和数据


共 (1) 个答案

  1. # 1 楼答案

    您的表模型没有列,因此它从不显示添加到其中的数据

    documentation for the zero-argument DefaultTableModel constructor

    Constructs a default DefaultTableModel which is a table of zero columns and zero rows.

    最初,您的表具有自动创建的有效表模型:

    JTable table_saved_words = new JTable(data, columnNames);
    

    然后创建一个没有列的新模型:

    DefaultTableModel dtm_saved_words = new DefaultTableModel();