Java-在JScrollBar设置在右侧的情况下打开JScrollPane [英] Java - Open JScrollPane with JScrollBar set to the right

查看:113
本文介绍了Java-在JScrollBar设置在右侧的情况下打开JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载一个JScrollPane,其中水平JScrollBar一直设置到最右边,即如下图所示:

I'm looking to try and load up a JScrollPane with the horizontal JScrollBar set all the way to the right, i.e. like in this image:

http://i.imgur.com/VeKYPa6.png

但是,无论我做什么,最终都会得到以下图像:

However, no matter what I do, I end up with this image:

http://i.imgur.com/c9KzRqZ.png

这是我的代码的相关代码段

This is the relevant snippet of my code

    JComponent graph = new TrendsForSuccLarge(wccScores, wccScoresTimes, graphStrings, maxScore, bgColour, iPadWidth);

    JScrollPane graphScrollPane = new JScrollPane(graph, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    JScrollBar horizontal = graphScrollPane.getHorizontalScrollBar();
    horizontal.setValue( horizontal.getMaximum() );

    this.getContentPane().add(graphScrollPane, BorderLayout.CENTER);

有什么问题的想法吗?

提前谢谢!

网格

SSCCE

import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

class ForStackOverflow  {

    private static JFrame main;
    //opens up a line chart on full screen

    public static void main(String args[]) {

        TempGraph tg = new TempGraph(main);
        tg.setVisible(true);

    }
}

class TempGraph extends JDialog {

    private static final long serialVersionUID = 1L;

    //opens up a line chart on full screen

    public TempGraph(JFrame parent)
    {
        // load the frame
        super(parent,true);
        configureFrame();
    }

    private void configureFrame()
    {

        int iPadHeight = 644;
        int iPadWidth = 981;

        this.getContentPane().setLayout(new BorderLayout());

        JComponent graph = new TrendsForSuccLarge2(iPadWidth);

        JScrollPane graphScrollPane = new JScrollPane(graph, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        this.getContentPane().add(graphScrollPane, BorderLayout.CENTER);

        JPanel buttons = new JPanel();
        buttons.setLayout(new BorderLayout());

        JButton ok = new JButton("Done");
        ok.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                next();
            }
        });
        buttons.add(ok,BorderLayout.CENTER);
        JButton exit = new JButton("Exit");
        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        buttons.add(exit,BorderLayout.WEST);

        this.getContentPane().add(buttons, BorderLayout.SOUTH);

        this.pack();        
        this.setSize(this.getWidth(), 750);
        this.setResizable(true);
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int widthFrame = iPadWidth;
        int heightFrame = iPadHeight;
        this.setSize(widthFrame, heightFrame);
        setBounds((screenSize.width - this.getWidth()) / 2,
                (screenSize.height - this.getHeight()) / 2, 
                getWidth(),
                getHeight());
    }

    private void next()
    {
        this.setVisible(false);
    }

}

class TrendsForSuccLarge2 extends JComponent {

    // basic parameters
    private static int PREF_W = 200;
    private static final int PREF_H = 200;
    private static final int BORDER_GAP = 10;
    private static final int GRAPH_GAP = BORDER_GAP + 20;

    private int graphWidth = 0;

    private static final long serialVersionUID = 1L;

    TrendsForSuccLarge2(int prefGraphWidth) {

        graphWidth = prefGraphWidth;

        paintComponent(null);
    }

    public void paint(Graphics g) { 

        int windowWidth = 2600;

        PREF_W = windowWidth;

        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.drawString("Hello", graphWidth/2, getHeight() - GRAPH_GAP);

        g2.drawString("Hello1", graphWidth - GRAPH_GAP, getHeight() - GRAPH_GAP);

        g2.drawString("Hello2", windowWidth - GRAPH_GAP - GRAPH_GAP, getHeight() - GRAPH_GAP);

        // create x and y axes 
        g2.setColor(Color.black);
        g2.drawLine(GRAPH_GAP, getHeight() - GRAPH_GAP, GRAPH_GAP, GRAPH_GAP);
        g2.drawLine(GRAPH_GAP, getHeight() - GRAPH_GAP, windowWidth - GRAPH_GAP, getHeight() - GRAPH_GAP);

    }

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

}

推荐答案

我解决了这个问题,显然问题是我的组件的首选大小太小了.我加大了首选尺寸,然后一切都自动排序了.

I solved this, apparently the issue was that my component had a preferred size that was too small. I rammed the preferred size up and everything sorted itself.

这篇关于Java-在JScrollBar设置在右侧的情况下打开JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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