2015-08-31 113 views
0

我正在用Java寫一個程序,但由於某種原因,最底層組件中的滾動條不起作用。我不知道爲什麼。它應該在底部文本區域應用垂直滾動條。我究竟做錯了什麼?滾動條在Java中不工作

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.Timer; 
import java.text.*; 
import java.lang.*; 
import java.util.*; 



public class TimerGui extends JFrame 
{ 
private static final int FRAME_HEIGHT = 300; 
private static final int FRAME_WIDTH = 600; 

private JPanel topPanel; 
private JPanel leftPanel; 
private JPanel bottomPanel; 

private JLabel topLabel; 
private JLabel leftLabel; 

private JTextField setTimer; 
private JTextArea displayTimer; 

private JScrollPane scrollPane; 

private JButton startButton; 

//no-arg constructor 
public TimerGui() 
{ 
    createTopPanel(); 
    createLeftPanel(); 
    createBottomPanel(); 
    setSize(FRAME_WIDTH, FRAME_HEIGHT); 
} 

//creates the top panel, including the label and text field 
private void createTopPanel() 
{ 
    topPanel = new JPanel(); 
    topLabel = new JLabel("TIMER LAB:"); 

    final int FIELD_WIDTH = 10; 
    setTimer = new JTextField(FIELD_WIDTH); 

    topPanel.add(topLabel); 
    topPanel.add(setTimer); 

    add(topPanel, BorderLayout.NORTH); 
} 

//creates the left panel, which is just a single text label 
private void createLeftPanel() 
{ 
    leftPanel = new JPanel(); 
    leftLabel = new JLabel("(1000 milliseconds = 1 second)"); 

    add(leftPanel, BorderLayout.WEST); 

    leftPanel.add(leftLabel); 
} 


//the listener for the start button 
class ClickListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     int speed = Integer.parseInt(setTimer.getText()); 

     MyListner listener = new MyListner(); 
     Timer timer = new Timer(speed, listener); 
     timer.start(); 
    } 
} 

class MyListner implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    {   
     SimpleDateFormat simpleTime = new SimpleDateFormat("HH:mm:ss.SSS"); 
     Date now = new Date(); 
     String time = simpleTime.format(now); 
     displayTimer.append(time + String.format("%n")); 
    } 
} 

//creates the button, with action listener attached 
private void createStartButton() 
{ 
    startButton = new JButton("Start"); 

    ActionListener listener = new ClickListener(); 
    startButton.addActionListener(listener); 
} 

//creates the bottom panel, with the start button and the display text field 
private void createBottomPanel() 
{ 
    final int FIELD_WIDTH = 10; 

    bottomPanel = new JPanel(); 
    displayTimer = new JTextArea(10, 15); 
    displayTimer.setEditable(false); 
    scrollPane = new JScrollPane(displayTimer); 
     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

    add(bottomPanel, BorderLayout.SOUTH); 

    createStartButton(); 
    bottomPanel.add(startButton); 
    bottomPanel.add(displayTimer); 
    bottomPanel.add(scrollPane); 
} 

} 
+0

remove'bottomPanel.add(displayTimer);' 它已經包含在ScrollPane中。 – RamanSB

回答

3

您正在添加displayTimer和JScrollPane,它們也將其保存到GUI。不要這樣做。只需添加JScrollPane,因爲您只能將一個組件添加到GUI中,因此您最終將沒有JScrollPane的JTextArea與空的JScrollPane一起添加(仔細查看JTextArea的右側)。因此,改變這種:

createStartButton(); 
    bottomPanel.add(startButton); 
    bottomPanel.add(displayTimer); 
    bottomPanel.add(scrollPane); 

這樣:

createStartButton(); 
    bottomPanel.add(startButton); 
    // !! bottomPanel.add(displayTimer); 
    bottomPanel.add(scrollPane); 

其他問題:

  • 避免設置大小。
  • 改爲調用pack()並讓GUI設置其自己的大小。
+0

謝謝!這完美地解決了我的問題! –

+0

@BenjaminCardwell:不客氣! –