JTextArea のスクロール ペインの表示に問題があり、GUI 出力に AS NEEDED を表示する垂直スクロール バーを取得できないのは何が問題なのか疑問に思っていました。このコードの 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("");
}
}
}