我正在用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);
}
}
remove'bottomPanel.add(displayTimer);' 它已經包含在ScrollPane中。 – RamanSB