Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何强制JScrollPane中的组件显示_Java_Swing_Jpanel_Jscrollpane - Fatal编程技术网

Java 如何强制JScrollPane中的组件显示

Java 如何强制JScrollPane中的组件显示,java,swing,jpanel,jscrollpane,Java,Swing,Jpanel,Jscrollpane,我在显示放置在JScrollPane中的特定组件时遇到问题。我有一个带有GridLayout(1,0)的水平JScrollPane,它包含数量可变的JPanel,每个JPanel都包含图像。这就像是GIF图像中帧的预览。我使用按钮在这些JPanel之间移动(通过更改边框并保留所选内容的索引),但我不知道如何强制JScrollPane显示选中的JPanel(如果可能,将其居中) 所以我想要这个 强制执行此操作: 提前谢谢 编辑:几乎可以使用scrollRectToVisible()方法工作代码

我在显示放置在JScrollPane中的特定组件时遇到问题。我有一个带有GridLayout(1,0)的水平JScrollPane,它包含数量可变的JPanel,每个JPanel都包含图像。这就像是GIF图像中帧的预览。我使用按钮在这些JPanel之间移动(通过更改边框并保留所选内容的索引),但我不知道如何强制JScrollPane显示选中的JPanel(如果可能,将其居中)

所以我想要这个

强制执行此操作:

提前谢谢

编辑:几乎可以使用
scrollRectToVisible()方法工作代码

public class MiniatursPanel extends JPanel{


private int indexOfChosenFrame = 0;
private ArrayList<JPanel> frames;
private JScrollPane scrollPane;
private JPanel innerPanel;



public MiniatursPanel(){
    setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(),BorderFactory.createLoweredBevelBorder()));
    setPreferredSize(new Dimension(1200,170));
    setLayout(null);     
}

public void initialize(){
    int width = GifImageStats.getInstance().getWidth();
    int height = GifImageStats.getInstance().getHeight();
    int numberOfFrames = GifImageStats.getInstance().getNumberOfFrames();

    frames = new ArrayList(numberOfFrames);

    for (int i = 0; i < numberOfFrames; i++) {
        JPanel frameBox = new JPanel();
        frameBox.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton button = new JButton(String.valueOf(i+1));
        button.setPreferredSize(new Dimension(2*width,2*height));
        button.setBackground(Color.white);
        button.setFocusable(false);
        frameBox.add(button);
        frames.add(frameBox);
    }

    innerPanel = new JPanel();
    innerPanel.setLayout(new GridLayout(1,0,10,10));


    for (JPanel button : frames) {
        innerPanel.add(button);
    }

    scrollPane = new JScrollPane(innerPanel);
    scrollPane.setBounds(10, 10, 1180, 145);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

    highlightFrame(frames.get(0));

    add(scrollPane);
}


public void nextFrame(){
    if (indexOfChosenFrame == frames.size() - 1) {           
        unhighlightFrame(frames.get(indexOfChosenFrame));
        indexOfChosenFrame = 0;
        highlightFrame(frames.get(0));
    }else{
        unhighlightFrame(frames.get(indexOfChosenFrame));
        indexOfChosenFrame++;
        highlightFrame(frames.get(indexOfChosenFrame));
    }
}

public void previousFrame(){
    if (indexOfChosenFrame == 0) {           
        unhighlightFrame(frames.get(0));
        indexOfChosenFrame = frames.size()-1;
        highlightFrame(frames.get(indexOfChosenFrame));
    }else{
        unhighlightFrame(frames.get(indexOfChosenFrame));
        indexOfChosenFrame--;
        highlightFrame(frames.get(indexOfChosenFrame));
    }
}

private void highlightFrame(JPanel frame){
    Rectangle rect = frame.getBounds();
    rect.setBounds(frame.getX()-550, frame.getY(), frame.getWidth()+1050, frame.getHeight());
    innerPanel.scrollRectToVisible(rect);     
    frame.setBorder(BorderFactory.createLineBorder(Color.red,2));
}          

private void unhighlightFrame(JPanel frame){
    frame.setBorder(null);
}
public-class-minimaturespanel扩展了JPanel{
私有int indexOfChosenFrame=0;
私有数组列表框架;
私有JScrollPane滚动窗格;
私人JPanel内部面板;
公共迷你模型{
setBordOrder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(),BorderFactory.createLowerdBevelborder());
设置首选尺寸(新尺寸(1200170));
setLayout(空);
}
公共无效初始化(){
int width=GifImageStats.getInstance().getWidth();
int height=GifImageStats.getInstance().getHeight();
int numberOfFrames=GifImageStats.getInstance().getNumberOfFrames();
frames=新的ArrayList(numberOfFrames);
for(int i=0;i
此处的相关方法是
JComponent#scrollRectToVisible(矩形)
。必须在滚动窗格视口中的组件上调用它。(在您的情况下,这是具有网格布局的面板,其中包含其他子面板)

传递给此方法的矩形可以是一个子面板的边界。在这种情况下,scoll窗格将执行“最小值”滚动是使给定矩形可见所必需的。如果要确保相应的子面板位于中心,则可以增加此矩形的大小-即,以所需子面板位于中心的方式定义矩形

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class ScrollToVisible
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        int n = 20;
        final JPanel panel = new JPanel(new GridLayout(1,0));
        final List<JComponent> components = new ArrayList<JComponent>();
        for (int i=0; i<n; i++)
        {
            JComponent component = new JLabel(String.valueOf(i), SwingConstants.CENTER);
            component.setPreferredSize(new Dimension(100,100));
            component.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            components.add(component);
            panel.add(component);
        }
        final JScrollPane scrollPane = new JScrollPane(panel);

        final JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, n-1, 1));
        spinner.addChangeListener(new ChangeListener()
        {
            JComponent selectedComponent = components.get(0);

            @Override
            public void stateChanged(ChangeEvent e)
            {
                selectedComponent.setBorder(BorderFactory.createLineBorder(Color.BLACK));

                int index = (Integer)spinner.getValue();
                JComponent component = components.get(index);
                Rectangle bounds = component.getBounds();

                // This would make the component "just" visible:
                //panel.scrollRectToVisible(bounds);

                // This will center the component:
                int cx = bounds.x + bounds.width / 2;
                int w = scrollPane.getViewport().getWidth();
                Rectangle r = new Rectangle(cx-w/2, bounds.y, w, bounds.height);
                panel.scrollRectToVisible(r);


                selectedComponent = component;
                selectedComponent.setBorder(BorderFactory.createLineBorder(Color.RED));

            }
        });

        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(scrollPane, BorderLayout.CENTER);
        f.getContentPane().add(spinner, BorderLayout.NORTH);


        f.setSize(800, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

最重要的是通过设置与“突出显示”相同大小的空边框,确保组件的大小正确边框。

谢谢!它工作得几乎完美。但最后两个组件不会显示,我也不知道为什么。我还尝试手动更改矩形边框,使其更宽,并将其向右移动,但没有成功。滚动条在结尾前停止一个部分。我还将在稍后向原始帖子添加代码,这可能会有所帮助。@user3437263添加了一个编辑。非常感谢您的帮助和您的时间。它现在运行得非常好。问题确实出在我设置为null的borders中。:)
private void highlightFrame(JPanel frame){
    Rectangle rect = frame.getBounds();

    int c = rect.x + rect.width / 2; 
    int w = scrollPane.getViewport().getWidth();
    int x = c-w/2;
    rect.setBounds(x, rect.y, w, rect.height);

    innerPanel.scrollRectToVisible(rect);     
    frame.setBorder(BorderFactory.createLineBorder(Color.red,2));
}          

private void unhighlightFrame(JPanel frame){
    frame.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}