[英]Java - Open JScrollPane with JScrollBar set to the right
我正在嘗試嘗試使用水平JScrollBar一直設置到右側的方式加載JScrollPane,即如下圖所示:
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);
}
}
完整的代碼在這里:
您發布的代碼無法編譯,因為我們無權訪問所有自定義類。 SSCCE
,請張貼適當的SSCCE
。
horizontal.setValue( horizontal.getMaximum() );
對話框可見后,您需要調用該代碼。
從Javadoc:
使用javax。擺動。JScrollBar中
公共無效setValue(int i)
設置可調對象的當前值。 如果提供的值小於最小值或大於最大值 -visibleAmount ,那么將酌情替換這些值之一。 調用此方法不會觸發AdjustmentEvent 。
參數:
v-當前值,介於最小值和最大值之間-visibleAmount
而且我真的不知道為什么使用getMaximum()
返回的最大值似乎為100。
可見量值(使用getVisibleAmount()
返回10)。
因此,調用setValue(9999)
時滾動的值不會大於90。
我的解決方案是,首先將最大值設置為某個較高的值,然后再設置該值:
myScrollPane.getHorizontalScrollBar()setMaximum(1500)。
myScrollPane.getHorizontalScrollBar()的setValue(1500)。
為了回答您是否知道發生了什么的問題,我不得不說“僅部分”。
問題似乎是在完全實現UI之前已經設置了滾動條值(或類似的東西-我對Swing的內部知識了解不足,不足以知道所有各種級別和術語是什么)。
我通過插入以下代碼使其工作:
SwingUtilities.invokeLater
(
new Runnable()
{
public void run()
{
graphScrollPane.getHorizontalScrollBar().setValue(1000);
}
}
);
就在TempGraph
configureFrame()
方法結束之前。
我解決了這個問題,顯然問題是我的組件的首選大小太小了。 我加大了首選大小,然后一切都整理好了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.