簡體   English   中英

當我將 JScrollPane 添加到我的 JTable 時,JTable 不可見

[英]When i add a JScrollPane to my JTable, the JTable is not visible

當我只使用 JTable 時,它是可見的,只有 JTable但當我添加 JScrollPane 時,它是不可見的。 使用 JScrollPane

    package Menu;
import Objects.Background;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class HighScore extends JFrame {
    public HighScore() {
        setLayout(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(250, 100, 1050, 650);
        this.setLayout(null);
        Background background = new Background();
        background.setBounds(0, 0, 1050, 650);
        add(background);
        background.setLayout(null);
        JButton back = new JButton("BACK");
        back.setBounds(800, 550, 100, 30);
        background.add(back);

        String[][] info = new String[100][2];
        String[] nevek=new String[100];
        int[] pontok = new int[100];
        int i=0;
        // read the highscores
        BufferedReader objReader = null;
        try {
            String strCurrentLine;

            objReader = new BufferedReader(new FileReader("highscores.txt"));

            while ((strCurrentLine = objReader.readLine()) != null) {
                String[] parts = strCurrentLine.split("\\,");
                String nev = parts[0];
                String pont = parts[1];
                nevek[i]=nev;
                pontok[i]= Integer.parseInt(pont);
                i++;
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {
                if (objReader != null)
                    objReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }//---bubble sort for the highscores
        for (int j=0; j<i; j++)
        {
            for (int k=0; k<i; k++)
                if(pontok[j]>pontok[k]) {
                    String seged2=nevek[j];
                    int seged=pontok[j];
                    pontok[j]=pontok[k];
                    nevek[j]=nevek[k];
                    pontok[k]=seged;
                    nevek[k]=seged2;
                }
        }
        //--matrix for the JTable
        for (int j=0; j<i; j++)
        {
            info[j][0]=nevek[j];
            info[j][1]= String.valueOf(pontok[j]);
        }
        //initialize Jtable and JScrollPane
        String[] columnNames={"Name","Score"};
        JTable scores = new JTable(info,columnNames);
        scores.setBounds(325,100,325,190);
        JScrollPane jScrollPane = new JScrollPane(scores);
        background.add(jScrollPane);

        back.addActionListener(e -> {
            new MainMenu();
            dispose();
        });
        setVisible(true);
    }
}

那是我的 class 的完整代碼。我正在嘗試使用 JTable 和 JScrollPane,我也嘗試在 JTable 和 JScrollPane 設置可見。 我認為問題可能是我將 JScrollPane 添加到 JPanel 而不是添加到 JFrame,但我想要我的面板上的分數表。 在我的 JPanel(背景)中,我為背景繪制了一個圖像,僅此而已。

有什么想法嗎?

您沒有看到表格,因為您錯誤地使用了 null 布局。

不要使用 null 布局!!!

不要使用 setBounds(...)!!!

Swing 旨在與布局管理器一起使用。 了解如何使用布局管理器。

你的基本代碼應該是:

Background background = new Background().
background.setLayout( new BorderLayout() );

JButton button = new JButton(...);
background.add(button, BorderLayout.PAGE_START);

JTable table = new JTable(...);
background.add(new JScrollPane( table ), BorderLayout.CENTER);

add(background);

現在按鈕和滾動窗格將添加到背景面板,背景面板將添加到框架。

閱讀 Swing 教程中有關布局管理器的部分,了解更多信息和布局管理器的工作示例。

本教程還有一個關於How to Use Tables的部分。 您也可以從那里下載工作演示代碼,以練習使用布局管理器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM