如何在JPanel中为长JComponents添加滚动条?如何使JComponent居中?

时间:2014-07-16 01:26:20

标签: java swing layout jcomponent

目前,我必须使用垂直框布局的JPanel中包含的JComponents。 这样,我可以让第一个组件居中,如下所示,并在下面有底部组件(很长)。但是,由于底部组件非常长,我想为该特定组件添加一个滑块。这样,用户可以看到所有底部组件,其中上部组件保持居中。但是,我的代码下面没有修复任何内容,滚动条甚至不起作用。您需要知道的有关GPComponent和GPinfinity的唯一信息是它们覆盖preferredSize,minimumSize,maximumSize和paintComponent方法(它们扩展JComponent)。 enter image description here

JFrame frame = new JFrame();
JPanel panel = new JPanel();

GPComponent gp = new GPComponent(n, k);
GPinfinityComponent gpi = new GPinfinityComponent(n, k);

Box box = new Box(BoxLayout.Y_AXIS);

panel.add(Box.createVerticalGlue());
panel.add(gp);
panel.add(Box.createVerticalGlue());
JScrollPane thePane = new JScrollPane(gpi, JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    panel.add(thePane);

frame.pack();6
frame.add(panel, BorderLayout.CENTER); // just to be clear
frame.setVisible(true);
final int FRAME_WIDTH = 600;
final int FRAME_HEIGHT = 600;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("GP("+n+", "+k+")");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

另外:两个组件的maximumSize = minimumSize = preferredSize 对于圆形尺寸,尺寸为(350,350),而另一个尺寸为(5000,150)。

1 个答案:

答案 0 :(得分:7)

你说:

  

......而对于另一个维度是(5000,150)。

如果这是应该显示滚动条的组件,那么Java会告诉你,它实际上比你想象的要短得多。我想知道你是否设置了尺寸而不是preferredSize。你实际上不应该设置任何一个,而应该覆盖getPreferredSize()并让它返回一个适合该组件的维度。

有关更详细的帮助,请考虑创建和发布minimal example program


修改
例如,我的MCVE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;

import javax.swing.*;

@SuppressWarnings("serial")
public class PreferredSizeEg extends JPanel {
   private static final int PREF_W = 800;
   private static final int PREF_H = 600;

   public PreferredSizeEg() {
      JPanel centerPanel = new JPanel(new GridBagLayout());
      centerPanel.add(new CenterImagePanel());

      JScrollPane scrollpane = new JScrollPane(new LongImagePanel(),
            JScrollPane.VERTICAL_SCROLLBAR_NEVER,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

      setLayout(new BorderLayout());
      add(centerPanel, BorderLayout.CENTER);
      add(scrollpane, BorderLayout.PAGE_END);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private class LongImagePanel extends JPanel {
      private static final int LI_PREF_W = 5000;
      private static final int LI_PREF_H = 150;

      @Override
      protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D) g;
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);

         int index = 0;
         int spriteWidth = 50;
         while ((index) * spriteWidth < getWidth()) {
            Color c = index % 2 == 0 ? Color.green : Color.red;
            g.setColor(c);
            int x = 2 + index * spriteWidth;
            int y = 2;
            int width = getHeight() - 4;
            int height = width;
            g.fillOval(x, y, width, height);
            index++;
         }
      }

      @Override
      public Dimension getPreferredSize() {
         return new Dimension(LI_PREF_W, LI_PREF_H);
      }
   }

   private class CenterImagePanel extends JPanel {
      private static final int CIP_PREF_W = 200;
      private static final int CIP_PREF_H = CIP_PREF_W;

      public CenterImagePanel() {
         setBorder(BorderFactory.createLineBorder(Color.BLACK));
      }

      @Override
      protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D) g;
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
         g.setColor(Color.green);
         int x = 5;
         int y = x;
         int width = getWidth() - 2 * x;
         int height = getHeight() - 2 * y;
         g.fillOval(x, y, width, height);
      }

      @Override
      public Dimension getPreferredSize() {
         return new Dimension(CIP_PREF_W, CIP_PREF_H);
      }
   }

   private static void createAndShowGui() {
      PreferredSizeEg mainPanel = new PreferredSizeEg();

      JFrame frame = new JFrame("PreferredSizeEg");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

显示为:

enter image description here