0

私は現在、各パネルにいくつかのテキストを持つ複数の JPanel を含むメインの JFrame を持っています。JPanel を作成するためのコードは、別のクラス (JPanel を「実装」する) にあります。個々のパネルのみに 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);

   }

上記のクラスは、別のパネル クラスのインスタンス変数であり、メイン パネル「MenuHolder」のインスタンス変数です。ただし、「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)
4

2 に答える 2

2

JScrollPaneのコンストラクターの最初のパラメーターは、それを含むコンポーネントではJTableなく、表示するコンポーネント (たとえば a ) です。したがって、これらの 2 行は無限ループ (一種) を作成します。

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

holder(最初の行で)表示するコンポーネントに置き換えJScrollPaneます。

于 2013-06-03T12:19:48.760 に答える
0
JScrollPane scroll = new JScrollPane(panelInstance);
frame.add(scroll);
于 2013-06-03T12:02:39.300 に答える