1

我正在尝试制作一个应用程序,它可以让您创建和玩 Jeopardy!木板。在创建 GUI 时,我似乎遇到了困难。一个 JPanel (scrollPanel1) 似乎没有出现在我的 JScrollPane (scrollPanel1) 中。我很确定 JScrollPane 在那里,因为当我将滚动条策略设置为始终显示滚动条时,滚动条是可见的。但我不认为 JPanel 出现在里面。我会很感激一些帮助。

这是代码:

import java.awt.*;
import javax.swing.*;

public class CreateNewBoardGUI {
    private JFrame mainFrame;
    private JPanel mainPanel;
    private JPanel boardInfo;
    private JLabel boardNameLabel;
    private JTextField boardNameField;
    private JTabbedPane catTab;
    private JPanel tabPanel1;
    private JPanel catNamePanel1;
    private JLabel catNameLabel1;
    private JTextField catNameField1;
    private JScrollPane scrollPane1;
    private JPanel scrollPanel1;
    private JPanel qPanel1;
    private JLabel qLabel1;
    private JTextField qField1;

    public void CreateNewBoardGUI(){
        mainFrame = new JFrame("Create new Jeopardy! board");
        mainPanel = new JPanel();
        boardInfo = new JPanel();
        boardNameLabel = new JLabel("What would you like this board to be called?");
        boardNameField = new JTextField();
        catTab = new JTabbedPane();
        tabPanel1 = new JPanel();
        catNamePanel1 = new JPanel();
        catNameLabel1 = new JLabel("What would you like the category to be called?");
        catNameField1 = new JTextField();
        scrollPane1 = new JScrollPane();
        scrollPanel1 = new JPanel();
        qPanel1 = new JPanel();
        qLabel1 = new JLabel("200 Point Question");
        qField1 = new JTextField();

        mainPanel.setLayout(new BorderLayout());
        boardInfo.setLayout(new BorderLayout());
        tabPanel1.setLayout(new BorderLayout());
        catNamePanel1.setLayout(new BorderLayout());
        scrollPanel1.setLayout(new BorderLayout());
        mainFrame.setSize(700, 900);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setResizable(false);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);

        qPanel1.add(qLabel1);
        qPanel1.add(qField1);
        scrollPanel1.add(qPanel1, BorderLayout.NORTH);
        scrollPane1.add(scrollPanel1);
        tabPanel1.add(scrollPane1, BorderLayout.CENTER);
        catNamePanel1.add(catNameField1, BorderLayout.CENTER);
        catNamePanel1.add(catNameLabel1, BorderLayout.NORTH);
        tabPanel1.add(catNamePanel1, BorderLayout.NORTH);
        mainPanel.add(catTab, BorderLayout.CENTER);
        boardInfo.add(boardNameField, BorderLayout.CENTER);
        boardInfo.add(boardNameLabel, BorderLayout.NORTH);
        mainPanel.add(boardInfo, BorderLayout.NORTH);
        mainFrame.add(mainPanel);

        catTab.addTab("Category 1", tabPanel1);





    }
}

提前致谢。

4

2 回答 2

4

去除那个scrollPane1 = new JScrollPane();

并添加它(在创建 scrollPanel1 实例之后):

scrollPane1 = new JScrollPane(scrollPanel1);

这将强制 scrollPane1 将 scrollPanel1 作为其“视口”。

要查看情况如何,您应该设置背景颜色: scrollPanel1.setBackground(Color.red);
向面板添加不同的颜色以研究它们的行为。

将滚动条从选项卡面板中拉伸出来后,它应该会立即添加它。

注意:查看“空布局”。它应该更容易处理。

让我知道这是否有帮助,或者您是否仍有任何问题。

于 2012-08-10T23:19:08.593 回答
0
package com.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.border.LineBorder;

public class MainGrid {

    public static JPanel addPanel(int count) {
        JPanel newPanel = new JPanel();
        newPanel.setLayout(new GridLayout(1, 5));
        JButton jb1 = new JButton("Button 1" + count);
        jb1.setSize(200, 10);
        newPanel.add(jb1);
        JButton jb2 = new JButton("Button 2" + count);
        jb2.setSize(200, 10);
        newPanel.add(jb2);
        JButton jb3 = new JButton("Button 3" + count);
        jb3.setSize(200, 10);
        newPanel.add(jb3);
        JButton jb4 = new JButton("Button 4" + count);
        jb4.setSize(200, 10);
        newPanel.add(jb4);
        JButton jb5 = new JButton("Button 5" + count);
        jb5.setSize(200, 10);
        newPanel.add(jb5);
        newPanel.setBorder(new LineBorder(Color.black, 2));
        newPanel.setVisible(true);
        return newPanel;
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("BoxLayout Test");
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollPane = new JScrollPane(mainPanel);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setLayout(new ScrollPaneLayout());
        mainPanel.add(addPanel(1));
        mainPanel.add(addPanel(2));
        mainPanel.add(addPanel(3));
        mainPanel.add(addPanel(4));
        mainPanel.add(addPanel(5));
        mainPanel.add(addPanel(6));
        mainPanel.add(addPanel(7));
        mainPanel.add(addPanel(8));
        mainPanel.add(addPanel(9));
        mainPanel.add(addPanel(10));
        mainPanel.add(addPanel(11));
        mainPanel.add(addPanel(12));
        mainPanel.add(addPanel(13));
        mainPanel.add(addPanel(14));
        mainPanel.add(addPanel(15));
        mainPanel.add(addPanel(1));
        mainPanel.add(addPanel(2));
        mainPanel.add(addPanel(3));
        mainPanel.add(addPanel(4));
        mainPanel.add(addPanel(5));
        mainPanel.add(addPanel(6));
        mainPanel.add(addPanel(7));
        mainPanel.add(addPanel(8));
        mainPanel.add(addPanel(9));
        mainPanel.add(addPanel(10));
        mainPanel.add(addPanel(11));
        mainPanel.add(addPanel(12));
        mainPanel.add(addPanel(13));
        mainPanel.add(addPanel(14));
        mainPanel.add(addPanel(15));
        scrollPane.setViewportBorder(
                BorderFactory.createLineBorder(Color.red));
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }
}
于 2017-03-07T03:21:15.857 回答