0

我正在尝试加载一个 JScrollPane 并将水平 JScrollBar 设置到右侧,即像这张图片中一样:

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

但是,无论我做什么,我都会得到这个图像:

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

这是我的代码的相关片段

    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);
    }

}
4

4 回答 4

2

完整的代码在这里:

您发布的代码无法编译,因为我们无权访问您的所有自定义类。SSCCE有问题时发表适当的。

horizontal.setValue( horizontal.getMaximum() ); 

您需要在对话框可见后调用该代码。

于 2013-08-29T15:06:37.517 回答
1

从 Javadoc :

javax.​swing.​JScrollBar
public void setValue(int i)
设置可调整对象的当前值。如果提供的值小于minimum或大于maximum - visibleAmount,则酌情替换这些值之一。调用此方法不会触发AdjustmentEvent
参数:
v - 当前值,介于最小值最大值之间- visibleAmount

而且我真的不知道为什么看起来最大值,使用getMaximum()返回100。
可见金额值,使用getVisibleAmount()返回10。
所以,当你调用时滚动的值setValue(9999)不会大于90。

我的解决方案是,先设置一些高值的最大值,然后设置值:

myScrollPane.getHorizo​​ntalScrollBar().setMaximum(1500);
myScrollPane.getHorizo​​ntalScrollBar().setValue(1500);

于 2013-08-29T14:43:40.990 回答
1

在回答您关于我是否知道发生了什么的问题时,我不得不说“仅部分”。

问题似乎是在完全实现 UI 之前设置了滚动条值(或类似的东西——我对 Swing 内部的了解不足以知道所有不同的级别和术语是什么)。

我通过插入以下代码使其工作:

SwingUtilities.invokeLater
(
  new Runnable()
  {
    public void run()
    {
      graphScrollPane.getHorizontalScrollBar().setValue(1000);
    }
  }
);

就在configureFrame()方法结束之前TempGraph

于 2013-08-29T18:44:28.433 回答
0

我解决了这个问题,显然问题是我的组件的首选尺寸太小了。我将首选尺寸推高,一切都自行排序。

于 2013-09-02T13:41:01.087 回答