为什么我的表在JScrollPane中不可见? [英] Why is my table not visible when it's in a JScrollPane?

查看:95
本文介绍了为什么我的表在JScrollPane中不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试几个小时,不同的事情,并在各处搜索解决方案,但我无法在 addScoreCardUpper()中显示我的表格。我得到一个水平滚动条,但没有内容,只有2个像素的边框。

I've been trying for hours, different things and searching everywhere for a solution, but I can not get my table in addScoreCardUpper() to show up. I get an horizontal scrollbar, but no content, just 2 pixels worth of border.

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

public class YahtzeeGUI extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 3255683022699487295L;
    private JFrame frame;
    private JPanel panel;
    private JPanel dicePanel;
    private JScrollPane scrollPane;
    private JButton btnRoll;
    private JButton[] btnDice = new JButton[5];
    private JTable table;

    private Yahtzee y = new Yahtzee();

    public YahtzeeGUI(){
        createWindow();
        addButtonRoll();
        addButtonDice();
        addScoreCardUpper();
        //addScoreCardLower();


        frame.add(panel);
        frame.setVisible(true);
    }

    public void createWindow(){
        frame = new JFrame();
        frame.setTitle("Yahtzee");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000,700);
        frame.setVisible(true);
        panel = new JPanel(new BorderLayout());
        dicePanel = new JPanel();
        scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    }

    public void addButtonRoll(){
        btnRoll = new JButton ("Roll the Dice");
        btnRoll.addActionListener(new RollHandler());

        dicePanel.add (btnRoll);
        panel.add(dicePanel, BorderLayout.SOUTH);
    }

    public void addButtonDice(){

        for (int i = 0; i < btnDice.length; i++){
            btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
            btnDice[i].addActionListener(new HoldHandler());
            dicePanel.add (btnDice[i]);
        }
        panel.add(dicePanel, BorderLayout.SOUTH);
    }

    public void addScoreCardUpper(){
        String tableHeader[] = {"Upper Section", "How to score", "Score" };
        String tableValues[][] = {
                {"ONES", "Total of all Ones",""},
                {"TWOS", "Total of all Twos",""},
                {"THREES", "Total of all Threes",""},
                {"FOURS", "Total of all Fours",""},
                {"FIVES", "Total of all Fives",""},
                {"SIXES", "Total of all Sixes",""},
                {"TOTAL SCORE", "",""},
                {"BONUS", "",""},
                {"TOTAL + BONUS", "",""}
        };

        table = new JTable(tableValues, tableHeader);
        table.setEnabled(false);
        setColumnWidths();
        scrollPane.add(table); // Here is the offender
        panel.add(scrollPane, BorderLayout.EAST);
    }

    /*public void addScoreCardLower(){
        String tableHeader[] = {"Lower S", "How to score", "Score" };
        String tableValues[][] = {
                {"ONES", "Total of all Ones",""},
                {"TWOS", "Total of all Twos",""},
                {"THREES", "Total of all Threes",""},
                {"FOURS", "Total of all Fours",""},
                {"FIVES", "Total of all Fives",""},
                {"SIXES", "Total of all Sixes",""},
                {"TOTAL SCORE", "",""},
                {"BONUS", "",""},
                {"TOTAL + BONUS", "",""}
        };

        table = new JTable(tableValues, tableHeader);
        table.setEnabled(false);
        setColumnWidths();
        scoreSubPanel.add(table);
        panel.add(scoreSubPanel, BorderLayout.WEST);
    }*/

    public void setColumnWidths(){

        TableColumn a = table.getColumnModel().getColumn(0);
        TableColumn b = table.getColumnModel().getColumn(1);
        a.setPreferredWidth(100);
        b.setPreferredWidth(100);

    }

    class RollHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            for(int i = 0; i < y.dice.length; i++){
                if (y.dice[i].getHoldState() != true){
                    y.dice[i].roll();
                    btnDice[i].setText(String.valueOf(y.dice[i].getFaceValue()));
                }
            }
        }
    }

    class HoldHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            for (int i = 0; i < btnDice.length; i++){
                if (event.getSource()== btnDice[i]){ // Picks the pressed button after running through them all.
                    if (y.dice[i].getHoldState() == false){
                        y.dice[i].setHoldState(true);
                    } else if (y.dice[i].getHoldState() == true){
                        y.dice[i].setHoldState(false);
                    }
                }
            }
        }
    }

}


推荐答案

这是你的问题:

scrollPane.add(table);

这不会将JTable添加到JScrollPane的视口视图中,这是您想要的视图,而是完全用JTable替换JScrollPane的视口,使JScrollPane完全不起作用。而是将表设置为JScrollPane的viewportview:

This does not add the JTable to the JScrollPane's viewport's view which is where you want it, but rather completely replaces the JScrollPane's viewport with the JTable, making the JScrollPane completely nonfunctional. Instead set the table as the JScrollPane's viewportview:

scrollPane.setViewportView(table);

执行此操作或将表传递给JScrollPane的构造函数,该构造函数几乎完全相同:

Do either this or pass the table into the JScrollPane's constructor which does pretty much the same thing:

JScrollPane scrollPane = new JScrollPane(table,
          ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
          ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

这一点在 JScrollPane API ,您将需要查看重要的详细信息。

This is all well explained in the JScrollPane API, and you will want to give it a look for the important details.

编辑

您的代码也调用 setVisible(true)<在添加可能导致问题的所有组件之前,在JFrame上添加/ code>。你会想避免这样做。

Edit
Your code also calls setVisible(true) on the JFrame before adding all components which can lead to trouble. You'll want to avoid doing this.

这篇关于为什么我的表在JScrollPane中不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆