当前位置: 首页 > 知识库问答 >
问题:

将JScrollPane添加到JPanel?

王昆
2023-03-14

我目前有一个主JFrame包含几个JPanels,每个面板中都有一些文本。创建JPanels的代码在一个单独的类中(它“实现”JPanels)。如何仅向单个面板添加JScrollPane?

我已经花了一些时间尝试这样做,但什么也没有发生。顺便说一下,我的JPanel使用GridLayout

public class HoldingPanel extends JPanel {

private String code, title, fee, loanPeriod;
private LMSGUI view;
private LMSModel model;
private JPanel holder;                          //holds holding info with scrollPane

private JScrollPane scrollPaneBar;


   public HoldingPanel(LMSModel m, LMSGUI g,
                      int code, String title, int fee, int loanPeriod, String type)
   {
       this.view = view;
       this.code = Integer.toString(code);
       this.title = title;   
       this.fee = Integer.toString(fee);
       this.loanPeriod = Integer.toString(loanPeriod);  
       this.holder = new JPanel();

       scrollPaneBar = new JScrollPane(holder, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    
       holder.add(scrollPaneBar);
       this.add(holder);

       if(type.equals("Video"))                                                             //set panel border to red if video
           this.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.red));

       if(type.equals("Book"))                                                              //set panel border to blue if book
           this.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.blue));

       view = g;
      holder.setLayout(new GridLayout(4,1));
       holder.add(new JLabel(this.code));
       holder.add(new JLabel(this.title));
       holder.add(new JLabel(this.fee));
       holder.add(new JLabel(this.loanPeriod));


       holder.addMouseListener(view.getDeleteHoldingListener());
       holder.setBackground(Color.GRAY);

   }

上面的类是另一个面板类的实例变量,它是主面板“MenuHold”的实例变量。但使用“MenuHolder”的主要JFrame类如下所示:

package lms.view;

import javax.swing.*;

import java.awt.*;


import lms.controller.DeleteHoldingListener;
import lms.controller.WindowCloseListener;
import lms.model.Holding;
import lms.model.facade.*;


public class LMSGUI extends JFrame 
{
    LMSModel model;
    private MenuHolder menuHolder;                                      //holds panel container for JMenu and JButton options (NORTH)
    private StatusHolder statusHolder;                                  //status bar panel(SOUTH)

    private HoldingGrid holdingGridHolder;                          //displays grid of available holdings

    private AddCollectionWindow addCollectionWindow;
    private AddHoldingWindow addHoldingWindow;                              //adds books/video depending on paramaters received
    private AboutWindow aboutWindow;

    private DeleteHoldingListener deleteHoldingListener;

    public LMSGUI(LMSModel model)
    {
        this.model=model;
        deleteHoldingListener = new DeleteHoldingListener(model, this);

        Container cp = this.getContentPane();
        cp.setLayout(new BorderLayout(3,5));

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(0,1,2,2));

        //Menu Holder/--------------------------------------------------------------------------------------/
        menuHolder = new MenuHolder(model, this);
        cp.add(menuHolder, BorderLayout.NORTH);

        //holding display grid/--------------------------------------------------------------------------------------/
        holdingGridHolder = new HoldingGrid(model, this);
    //  holdingGridHolder.updateGrid(model.getAllHoldings());
        cp.add(holdingGridHolder, BorderLayout.CENTER);

        //Status bar Holder/--------------------------------------------------------------------------------------/
        statusHolder = new StatusHolder(model, this);
        cp.add(statusHolder, BorderLayout.SOUTH);


        this.setSize(800,550);
        this.setLocation(300,200);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowCloseListener(model, this));                                       //show confirmation dialogue



    }


    public void initializeAddHoldingWindow(String type)
    {
        addHoldingWindow = new AddHoldingWindow(type,model,this);
        addHoldingWindow.setVisible(true);
    }

    public void initializeAddCollectionWindow() {
        addCollectionWindow = new AddCollectionWindow(model, this);
        addCollectionWindow.setVisible(true);

    }

    public void initializeAboutWindow() {
        aboutWindow = new AboutWindow();
        aboutWindow.setVisible(true);

    }

    public AddHoldingWindow getAddHoldingWindowDetails(){
        return addHoldingWindow;
    }

    public AddCollectionWindow getAddCollectionWindowDetails(){
        return addCollectionWindow;
    }

    public void updateHoldingGrid(Holding[] holdings){
        holdingGridHolder.updateGrid(holdings);
    }

    public void resetHoldingGrid(){
        holdingGridHolder.resetGrid();
    }

    public DeleteHoldingListener getDeleteHoldingListener(){
        return deleteHoldingListener;
    }

    public void updateSatusBar(Holding[] holdings){
        statusHolder.updateStatusBar(holdings);
    }


}

例外情况

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding container's parent to itself
    at java.awt.Container.checkAddToSelf(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at lms.view.HoldingPanel.<init>(HoldingPanel.java:38)
    at lms.view.HoldingGrid.updateGrid(HoldingGrid.java:80)
    at lms.view.LMSGUI.updateHoldingGrid(LMSGUI.java:90)
    at lms.controller.AddBookListener.actionPerformed(AddBookListener.java:63)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

共有2个答案

文凯康
2023-03-14
JScrollPane scroll = new JScrollPane(panelInstance);
frame.add(scroll);
阎晋
2023-03-14

JScrollPane的构造函数中的第一个参数是它将显示的组件(例如JTable),而不是包含它的组件。因此,这两条线构成一个无限循环(有点):

   scrollPaneBar = new JScrollPane(holder, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    
   holder.add(scrollPaneBar);

JScrollPane要显示的组件替换holder(在第一行中)。

 类似资料:
  • 问题内容: 我有一个gui,它的Panel包含一系列标签和TextField,并使用spring布局(这是mainPanel),而另一个Panel仅包含button(buttonPanel)。我正在尝试使我的mainPanel也具有垂直滚动条。我想实现我的GUI,以便在JFrame中有2个面板。mainPanel出现在框架的顶部,而buttonPanel出现在mainPanel的下方。 我的问题是

  • 问题内容: 我有一个JList,我需要将其放置在滚动窗格中,因为我是从数据库中获取JList的,其值可能会大大增加。我需要能够向下滚动它们,所以我写道: 因为看不到任何ScrollPane,我在做什么错? 问题答案: 该列表已经包含在滚动窗格中,因此您不能将列表添加到主面板。仅滚动窗格。 您做错的另一件事是不使用布局管理器,而是设置组件的边界和大小。不要那样做 让布局管理器为您定位和调整组件的大小

  • 我有一个关于向Java框架添加组件的问题。 我有一个带有两个按钮的JGroup和一个添加了JTable的JScrollPane。我想将这两个都添加到JFrame中。 我可以将JPanel添加到JFrame,或者将JScrollPane添加到JFrame,并且它们可以正确显示。我很难将它们都添加到JFrame并显示它们。 是否有与JFrames相关的东西不允许这样做?任何帮助都将不胜感激。 编辑 问

  • 我试图将JTable添加到JScrollPane中。但是做完这个我就看不到桌子了。 我怎么了?

  • 请帮助我,我正在创建像sublime text editor这样的软件。我想显示可用的字符集列表,下面是主要问题的图片。在这张图片中,我看到的菜单弹出窗口至少有200多个字符集,但只有显示。。。某物但是要显示所有字符集。没有为该弹出菜单添加滚动条的选项 我的软件图片有问题,请看那个红色圆圈。 请任何人帮助我如何添加滚动条到那个

  • 因此,我的问题是面板被插入到JScrollPane中,这些面板的大小正在减小,但没有滚动条。 这是我的代码,我已经播种很远: