0
我正在嘗試創建一個計算循環算法的程序。邏輯工作正常。我的問題是用於繪製時間線的overriden JPanel。時間線繼續而且沒有明確的線條長度。我想將重寫面板添加到滾動窗格,以便它可以滾動。將JScrollPane添加到被覆蓋的JPanel
SampleGPane.class
import java.awt.*;
import javax.swing.*;
public class
SampleGPane
{
/* Timeline elements */
Container timelineContainer;
JFrame timelineFrame = new JFrame();
JPanel pnlDraw = new JPanel();
JScrollPane timelineScroll;
public void
launchFrame()
{
GPanel gpane = new GPanel();
timelineContainer = timelineFrame.getContentPane();
timelineScroll = new JScrollPane (gpane);
timelineContainer.add (timelineScroll);
timelineFrame.setSize (500, 250);
timelineFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
timelineFrame.setVisible (true);
}
private class
GPanel extends JPanel
{
@Override
public void
paintComponent (Graphics g)
{
super.paintComponent (g);
int runningLineX = 0;
int runningLineY = 0;
// g.drawLine (50, 50, orderCount * 5, 50);
runningLineX += 50;
runningLineY += 50;
for (int count = 0; count < 35; count++) {
g.drawString ("J" + (count + 1), runningLineX + 50, 25);
runningLineX += 50;
// runningLineY += 50;
g.drawLine (runningLineX, runningLineY, runningLineX + 50, runningLineY);
}
}
}
}
SampleGPane.class
由SampleLaunch.class
public class
SampleLaunch
{
public static void main (String args[]) {
SampleGPane sgp = new SampleGPane();
sgp.launchFrame();
}
}
的問題是所謂的,JScrollPane的將無法正常工作。它似乎沒有檢測到這條線。我該如何解決?
謝謝!這有助於我解決我的問題。所以關鍵在於計算花費的時間,將其傳遞給構造函數,構造函數將設置一個用於覆蓋'getPreferredSize()'方法的變量,然後啓動框架。 –