2013-08-29 21 views
0

我期待嘗試加載一個JScrollPane與水平的JScrollBar此圖像中所有的方式設置爲正確的,即是一個正確的:爪哇 - 打開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); 
    } 

} 
+0

嘗試在將「JScrollPane」添加到對話框後設置「JScrollBar」的值。 – Pietu1998

+3

不要使用氯黴素,因爲它的使用與再生障礙性貧血有關。 –

+0

+1氣墊船充滿了鰻魚,但它只是一個隨機的藥物名稱進行測試。 Pietu1998,我試過了,沒有用。 –

回答

0

我解決了這個問題,顯然問題是我的組件的首選大小太小了。我夯實了首選尺寸,所有東西都自行排序。

1

從的Javadoc:。

的javax揮杆的JScrollBar
公共無效的setValue(int i)以
設置的當前值可調整的物體。如果提供的值小於最小值或大於最大值-visibleAmount,那麼視情況而定,這些值之一被替換。 調用此方法不會觸發AdjustmentEvent
參數:
V - 是當前值,最低之間的最大 - visibleAmount

而且我真的不知道爲什麼它似乎是最大值,使用getMaximum()回到100。
和可見金額值,使用getVisibleAmount()返回10.
因此,當您撥打setValue(9999)時的滾動值不會大於90.

而我的解決辦法是,設置一些高值的最大第一,然後設定值:

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

+0

試過了,但無濟於事。真的很困惑= S –

+0

對不起,我不知道爲什麼。但是,這對我很有用。 – topher

2

完整的代碼是在這裏:

您發佈的代碼無法編譯,因爲我們沒有訪問您所有的自定義類。當您有問題時發佈適當的SSCCE

horizontal.setValue(horizontal.getMaximum()); 

您需要在對話框可見後調用該代碼。

+0

好的,我會發佈一個SSCCE,將重新編寫我的代碼。感謝您的高舉。 –

+0

修改爲包含SSCCE。謝謝。 –

+0

@KeshavaMurthy:「SSCCE」==「short」中的第一個「S」。你的代碼應該只有足夠的代碼來編譯,運行和顯示問題,而不是其他的。您目前發佈的代碼中有許多與您的問題完全無關的代碼,因此會讓我們分心,使我們很難輕鬆地解決問題的本質。當然,這樣做對你而言意味着更多的工作,但是這對於你和我們來說都是很好的工作。 –

1

在回答你是否知道發生了什麼的問題時,我不得不說「僅部分」。

問題似乎是在UI完全實現之前滾動條的值已經設置好了(或者類似的東西 - 我不明白Swing的內部知識足以知道所有各種級別和術語是什麼)。

我把它插入下面的代碼工作:

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

只是configureFrame()方法在TempGraph年底前。

+0

乾杯,工作。 –

+0

嗨,事實證明,這隻適用於我的窗口小於一定大小。我的窗戶變得更大了,突然間酒吧才走到一半。我已經改變了這個問題,以包括一個正確的SSCCE。 –

+0

我沒有保留原始代碼與我設置的值,但我不記得圖中的對話框,我認爲它是在框架中。一旦你讓對話框可見,它就會停止執行諸如「invokeLater」之類的東西,它出現在「setVisible」之後。有一種方法可以通過對話來完成,但我不再確定你想要什麼,或者甚至你還在問一個問題。但我認爲「只走中途」的酒吧很可能是我們將價值設定爲1000,現在這個窗口比這個寬,我不願意嘗試調試當前的代碼而不知道更多需要什麼。 – arcy