2016-09-18 4 views
0

스크롤 막대를 JTextArea과 함께 추가하려하지만 작동하지 않고 내용이 textArea에서 벗어납니다. 여러 기사를 읽었지만 JPanel 예제를 찾지 못했습니다. 모든 예제는 JFrame과 함께 제공됩니다. 현재 우리는 JFrame을 사용하도록 제한되어 있으므로 사용하지 않고이 작업을 수행해야합니다. teatArea를 사용하여 스크롤을 구현하는 방법을 알려주는 코드를 찾으십시오. 도움을 받으실 수 있습니다.JScrollPane이 JPanel과 동작하지 않습니다.

GUI of below code

코드

public class CreatePanel extends JPanel 
{ 
    private Vector athleteList; 
    private CountPanel cPanel; 

    private JTextField txt_firstName; 
    private JTextField txt_lastName; 
    private JTextField txt_sport; 

    private JButton btnNewButton; 

    private JLabel lbl_error; 
    private JLabel lblNewLabel; 
    private JLabel lblNewLabel_1; 
    private JLabel lblNewLabel_2; 
    private JList list; 
    private JTextArea textArea; 
//Constructor initializes components and organize them using certain layouts 
public CreatePanel(Vector athleteList, CountPanel cPanel) 
    { 
    this.athleteList = athleteList; 
    this.cPanel = cPanel; 
    ButtonListener buttonListener = new ButtonListener(); 
    //organize components here 

    txt_firstName = new JTextField(); 
    txt_firstName.setColumns(10); 

    txt_lastName = new JTextField(); 
    txt_lastName.setColumns(10); 

    txt_sport = new JTextField(); 
    txt_sport.setColumns(10); 

    lblNewLabel = new JLabel("First Name"); 
    lblNewLabel_1 = new JLabel("Last Name"); 
    lblNewLabel_2 = new JLabel("Sport"); 

    lbl_error = new JLabel("error"); 
    lbl_error.setForeground(Color.RED); 
    lbl_error.setVisible(false); 

    list = new JList<Athlete>(); 

    btnNewButton = new JButton("Create an Athlete"); 
    btnNewButton.addActionListener(new ButtonListener()); 


    textArea = new JTextArea(16,58); 
    JScrollPane scroll = new JScrollPane (textArea); 
    scroll.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    textArea.setEditable(false); 

    add(scroll); 
    GroupLayout groupLayout = new GroupLayout(this); 
    groupLayout.setHorizontalGroup(
     groupLayout.createParallelGroup(Alignment.LEADING) 
      .addGroup(groupLayout.createSequentialGroup() 
       .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
         .addGroup(groupLayout.createSequentialGroup() 
          .addContainerGap() 
          .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
           .addComponent(lblNewLabel) 
           .addComponent(lblNewLabel_1) 
           .addComponent(lblNewLabel_2)) 
          .addGap(53) 
          .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
           .addComponent(txt_firstName, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE) 
           .addComponent(txt_lastName, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE) 
           .addComponent(txt_sport, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))) 
         .addGroup(groupLayout.createSequentialGroup() 
          .addGap(59) 
          .addComponent(btnNewButton))) 
        .addGroup(groupLayout.createSequentialGroup() 
         .addContainerGap() 
         .addComponent(lbl_error))) 
       .addGap(5) 
       .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) 
       .addContainerGap()) 
    ); 
    groupLayout.setVerticalGroup(
     groupLayout.createParallelGroup(Alignment.LEADING) 
      .addGroup(groupLayout.createSequentialGroup() 
       .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
        .addGroup(groupLayout.createSequentialGroup() 
         .addGap(47) 
         .addComponent(lbl_error) 
         .addGap(53) 
         .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) 
          .addComponent(txt_firstName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
          .addComponent(lblNewLabel)) 
         .addPreferredGap(ComponentPlacement.UNRELATED) 
         .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING) 
          .addComponent(lblNewLabel_1) 
          .addComponent(txt_lastName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) 
         .addPreferredGap(ComponentPlacement.UNRELATED) 
         .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) 
          .addComponent(txt_sport, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
          .addComponent(lblNewLabel_2)) 
         .addPreferredGap(ComponentPlacement.UNRELATED) 
         .addComponent(btnNewButton)) 
        .addGroup(groupLayout.createSequentialGroup() 
         .addContainerGap() 
         .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))) 
       .addContainerGap()) 
    ); 
    setLayout(groupLayout); 
    } 


    //ButtonListener is a listener class that listens to 
    //see if the button "Create an Athlete" is pushed. 
    //When the event occurs, it adds an athlete using the information 
    //entered by a user. 
    //It also performs error checking. 
    private class ButtonListener implements ActionListener 
    { 
    public void actionPerformed(ActionEvent event) 
    { 

     //TO BE COMPLETED 
     if(txt_firstName.getText().length() == 0 || txt_lastName.getText().length() == 0 || txt_sport.getText().length() == 0) 
     { 
      lbl_error.setText("Please enter all fields."); 
      lbl_error.setVisible(true); 
     } 
     else 
     { 
      Athlete athlete = new Athlete(); 
      athlete.setFirstName(txt_firstName.getText()); 
      athlete.setLastName(txt_lastName.getText()); 
      athlete.setSport(txt_sport.getText()); 
      athleteList.add(athlete); 
      textArea.setText(textArea.getText().concat(athlete.toString())); 
      lbl_error.setText("athlete added."); 
      //clearControls(); 
     } 
    } //end of actionPerformed method 
    } //end of ButtonListener class 

    private void clearControls() 
    { 
    txt_firstName.setText(""); 
    txt_lastName.setText(""); 
    txt_sport.setText(""); 
    } 
} //end of CreatePanel class 

답변

0

귀하의 GroupLayout는 레이아웃에 scroll을 추가하지만, textArea하지 않습니다

.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) 

시도가

,536처럼 scroll을 추가
.addComponent(scroll, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) 
관련 문제