0

我在为我的 JTextArea 显示滚动窗格时遇到问题,我想知道我无法让垂直滚动条在我的 gui 输出中按需要显示是什么问题。在此代码中的 orderPanel() 下,我定义了 JTextArea 和 JScrollPane 以与它一起使用。

public class CustomInfo extends JFrame {
private JPanel mainPanel, headerPanel, customerPanel, orderPanel, buttonPanel;
private JLabel headerLabel, nameLabel, phoneLabel, addressLabel, emailLabel, 
        probLabel, scriptLabel;  // message label for customer information
private JTextField nameTF, phoneTF, addressTF, emailTF;  // text field
private JTextArea description;
private JScrollPane vert_scroll;
private JButton submitButton;  // button

// set up for GUI
public CustomInfo() {
    // title bar text
    super("Customer Information");
    // corner exit button action
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // create main panel
    mainPanel = new JPanel();
    // create header panel
    headerPanel = new JPanel();
    // create input panel
    customerPanel = new JPanel();
    // create order panel
    orderPanel = new JPanel();
    // create button panel
    buttonPanel = new JPanel();
    // panel build manager
    headerPanel();
    customerPanel();
    orderPanel();
    buttonPanel();
    // add GridLayout manager to main panel
    mainPanel.setLayout(new GridLayout(4, 1));
    // add panel to gui
    this.add(mainPanel);
    mainPanel.add(headerPanel);
    mainPanel.add(customerPanel);
    mainPanel.add(orderPanel);
    mainPanel.add(buttonPanel);
    // resize GUI to fit text
    this.pack();
    // display window
    setVisible(true);
}
// main method
public static void main(String[] args) {
    CustomInfo customInfo = new CustomInfo();
}
// build header panel
private void headerPanel() {
    headerPanel.setLayout(new GridLayout(1, 1));
    // change background color
    headerPanel.setBackground(Color.BLACK);
    // initialize variable
    headerLabel = new JLabel("PLEASE ENTER THE FOLLOWING INFORMATION");
    // set color of headerLabel
    headerLabel.setForeground(Color.white);
    // add component to panel
    headerPanel.add(headerLabel);
}
// build input panel
private void customerPanel() {
    customerPanel.setLayout(new GridLayout(4, 2));
    // change background color
    customerPanel.setBackground(Color.BLACK);
    // initialize variable
    nameLabel = new JLabel("NAME:");
    // set color of nameLabel
    nameLabel.setForeground(Color.white);
    // 
    nameTF = new JTextField(5);
    // initialize variable
    phoneLabel = new JLabel("PHONE #:");
    // set color of phoneLabel
    phoneLabel.setForeground(Color.white);
    // 
    phoneTF = new JTextField(5);
    // initialize variable
    addressLabel = new JLabel("Address:");
    // set color of addressLabel
    addressLabel.setForeground(Color.white);
    // 
    addressTF = new JTextField(5);
    // initialize variable
    emailLabel = new JLabel("EMAIL:");
    // set color of emailLabel
    emailLabel.setForeground(Color.white);
    // 
    emailTF = new JTextField(5);
    // add components to panel
    customerPanel.add(nameLabel);
    customerPanel.add(nameTF);
    customerPanel.add(phoneLabel);
    customerPanel.add(phoneTF);
    customerPanel.add(addressLabel);
    customerPanel.add(addressTF);
    customerPanel.add(emailLabel);
    customerPanel.add(emailTF);
}

private void orderPanel() {
    orderPanel.setLayout(new GridLayout(2, 2));
    // change background color
    orderPanel.setBackground(Color.BLACK);
    // initialize variable
    probLabel = new JLabel("Order:");
    // set color of probLabel
    probLabel.setForeground(Color.white);
    // initialize variable

    // initialize variable
    scriptLabel = new JLabel("Description:");
    // set color of scriptLabel
    scriptLabel.setForeground(Color.white);

    /*Something here*/

    // initialize variable
    description = new JTextArea(3, 20);
    // allow word wrap
    description.setLineWrap(true);
    // initialize scroll pane variable
    vert_scroll = new JScrollPane(description);
    // specify scroll pane function
    vert_scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    // add components to panel
    orderPanel.add(probLabel);
    orderPanel.add(scriptLabel);
    orderPanel.add(description);

}
// build button panel
private void buttonPanel() {
    // change background color
    buttonPanel.setBackground(Color.BLACK);
    // initialize variable
    submitButton = new JButton("Submit Order");
    // add ActionListener
    submitButton.addActionListener(new SubmitButtonListener());
    // add components to panel
    buttonPanel.add(submitButton);
}
// build action listener for button panel
private class SubmitButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) {
        nameTF.setText("");
        phoneTF.setText("");
        emailTF.setText("");
        description.setText("");
    }
}
}
4

1 回答 1

1

而不是像现在这样添加文本区域

orderPanel.add(description);

您应该添加滚动窗格,如下所示:

orderPanel.add(vert_scroll);
于 2013-11-13T02:13:27.817 回答