java—当我向jtable添加jscrollpane时,jtable不可见

aelbi1ox  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(460)

当我只使用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);
    }
}

这是我们班的全部代码。我正在尝试一个带有jscrollpane的jtable,我在jtable和jscrollpane上也尝试了setvisible。我认为问题可能是我将jscrollpane添加到jpanel而不是jframe中,但是我希望面板上有那个score表。在我的jpanel(background)中,我为背景绘制了一个图像,仅此而已。
奇怪的想法?

pn9klfpd

pn9klfpd1#

您没有看到该表,因为您不正确地使用了空布局。
不要使用空布局!!!
不要使用设置边界(…)!!!
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 . 你也可以从那里下载演示代码来练习使用布局管理器。

相关问题