为什么JPanel没有显示在JScrollPane上?

时间:2014-03-12 12:06:21

标签: java swing jpanel jscrollpane jtabbedpane

为什么JPanel panGraphics中未显示JScrollPane JTabbedPane?但是,如果我将panGraphics直接放在tabbedPaneData.addTab("Chart View", panGraphics);上,则可以:JTabbedPane tabbedPaneData = new JTabbedPane(); JScrollPane paneScrollPane = new JScrollPane(); JPanel panGraphics = createGraphics(); paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); paneScrollPane.setPreferredSize(new Dimension(550, 320)); paneScrollPane.setMinimumSize(new Dimension(10, 10)); paneScrollPane.add(panGraphics); tabbedPaneData.addTab("Chart View", paneScrollPane); tabbedPaneData.addTab("Table View", new JPanel());

{{1}}

2 个答案:

答案 0 :(得分:3)

使用方法JScrollPane.setViewportView添加面板。因此,而不是:

paneScrollPane.add(panGraphics);

使用:

paneScrollPane.setViewportView(panGraphics);

答案 1 :(得分:0)

Here is My collection sample code:
-----------------------------------
package testing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestScroller extends JPanel
{
    JPanel panel;
    JScrollPane scrollPane;

    public TestScroller() {
        // TODO Auto-generated constructor stub

//this is in the constructor of the mother object, JPanel
    panel = new JPanel();
    panel.setBorder(BorderFactory.createEtchedBorder());
    //panel.setBounds(0,0,415,420);

    panel.setPreferredSize(new Dimension(415,420));
    panel.setBackground(Color.BLACK);

    scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(new Dimension(415,400));
    add(scrollPane);
  //  add(scrollPane, 0, 40, 415, 400);
    }
/*  //method add defined in the mother object
      public void add(Component c, int x, int y, int sX, int sY)
      {
          add(c);
          c.setBounds(x,y,sX,sY);
      }*/




    public static void main( String args[] )
    {
        JPanel baseView = new TestScroller();
        baseView.setPreferredSize(new Dimension(500,500));
        baseView.setOpaque(false);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(baseView);
        frame.setSize(500,500);

        frame.setVisible(true);
        frame.repaint();
    }
}