为什么我的表在JScrollPane中不可见?

编程入门 行业动态 更新时间:2024-10-27 19:24:35
本文介绍了为什么我的表在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)。你会想避免这样做。

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中不可见?

本文发布于:2023-11-15 05:05:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   JScrollPane

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!