我目前有一个主 JFrame,其中包含几个 JPanel,每个面板中都有一些文本。创建 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)