我正在用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 :(得分:3)
您正在添加displayTimer和JScrollPane,它们也将GUI保存到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设置自己的大小。