2015-08-31 115 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:不客气! –