如何在 JPanel 中为长 JComponent 添加滚动条?如何将 JComponent 居中? [英] How to add a scrollbar for long JComponents in a JPanel? How to center a JComponent?

查看:34
本文介绍了如何在 JPanel 中为长 JComponent 添加滚动条?如何将 JComponent 居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我必须将 JComponents 包含在具有垂直框布局的 JPanel 中.这样,我可以让第一个组件居中,如下所示,底部组件(很长)在下面.但是,由于底部组件很长,我想为该特定组件添加一个滑块.这样,用户可以看到所有底部组件,而上部组件保持居中.但是,我下面的代码没有解决任何问题,滚动条甚至根本不起作用.您需要了解的有关 GPComponent 和 GPinfinity 的唯一信息是它们覆盖了 preferredSize、minimumSize、maximumSize 和 paintComponent 方法(它们扩展了 JComponent).

Currently, I have to JComponents contained in a JPanel with a vertical box layout. This way, I can have the first component centered, as shown below, and have the bottom component (which is quite long) below. However, since the bottom component is very long I wanted to add a slider just for that specific component. This way, the user can see all of the bottom component with the upper component remaining centered. However, my code below doesn't fix anything and the scrollbar never even works. The only information about GPComponent and GPinfinity you need to know is they override the preferredSize, minimumSize, maximumSize, and paintComponent methods (they extend JComponent).

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).

Also: the maximumSize=minimumSize=preferredSize for both components For the circular one the dimensions are (350, 350) and for the other the dimensions are (5000, 150).

推荐答案

您声明:

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

...and for the other the dimensions are (5000, 150).

如果这是应该显示滚动条的组件,Java 会告诉您其他情况,它实际上比您想象的要短得多.我想知道您是否正在设置大小而不是首选大小.您实际上不应该设置,而应该覆盖 getPreferredSize() 并让它返回适合组件的尺寸.

If this is the component that is supposed to show the scrollbars, the Java is telling you otherwise, that it is in fact much shorter than you suppose it to be. I wonder if you're setting size instead of preferredSize. You actually should not be setting either but rather should override getPreferredSize() and have it return a dimension appropriate for the component.

要获得更详细的帮助,请考虑创建并发布最小示例程序.

For more detailed help, consider creating and posting a minimal example program.

编辑
例如,我的 MCVE:

Edit
For example, my 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();
         }
      });
   }
}

显示为:

这篇关于如何在 JPanel 中为长 JComponent 添加滚动条?如何将 JComponent 居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆