获取textarea框的滚动条有问题

时间:2011-07-07 12:39:36

标签: java swing

获取textarea框的滚动条时出现问题。我在下面给出了我的逻辑。任何人都可以帮助我吗?

package swings_new_1stjul;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class DHMain extends javax.swing.JFrame
{    
    private static final long serialVersionUID = 1L;
    private final static String newline = "\n";
    private java.io.File selectedFile;


      public java.io.File getSelectedFile() {
            return selectedFile;
        }
        public javax.swing.JLabel piclabel;
        public javax.swing.JTextField samp;
        public javax.swing.JPanel selectPanel;
        public javax.swing.JButton verifyButton;
        public javax.swing.JLabel piclabel1;
        public javax.swing.JTextArea statusTestbox;
        public javax.swing.JLabel piclabel2;
        public javax.swing.JTextArea errorsTestbox;
        public javax.swing.JScrollPane scrollpane1;
        public javax.swing.JLabel filenameLabel;
        public javax.swing.JTextField filenameField;
        public javax.swing.JButton browseButton;  


    public DHMain() 
   {   // method is calling here. 
        initComponents();
        setSize(420,250);
        setResizable(false);
    } 
// Method to initialize components in the frame.
    private void initComponents() 
     {

    setTitle("Welcome to Automated testing tool for Penson");

    new javax.swing.JDesktopPane();
    selectPanel = new javax.swing.JPanel();     
    filenameLabel=new JLabel("File Name");
    piclabel=new JLabel("Procedure Name");
    filenameField=new JTextField(23);
    samp=new JTextField(20);
    browseButton=new JButton();
    verifyButton=new JButton();
    piclabel1=new JLabel("Status",(int) LEFT_ALIGNMENT);
    piclabel2=new JLabel("Errors");
    statusTestbox=new JTextArea(5,30);
    errorsTestbox=new JTextArea(5,30);
    statusTestbox.setEditable(false);
    errorsTestbox.setEditable(false);

    JPanel panel = new JPanel();   
    JScrollPane scroller = new JScrollPane(panel);   
    this.getContentPane().add(scroller, BorderLayout.CENTER);

    selectPanel.add(filenameLabel);
    selectPanel.add(filenameField);
    selectPanel.add(browseButton);
    selectPanel.add(piclabel);
    selectPanel.add(samp);
    selectPanel.add(verifyButton);
    selectPanel.add(piclabel1);
    selectPanel.add(statusTestbox,scrollpane1);
    selectPanel.add(piclabel2);
    selectPanel.add(errorsTestbox); 
    add(selectPanel) ;      

    browseButton.setMnemonic('B');
    browseButton.setText("Browse");

    verifyButton.setMnemonic('V');
    verifyButton.setText("Verify");



    browseButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            browseButtonActionPerformed(evt);
        }
    });

    verifyButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            verifyButtonActionPerformed(evt);
        }
    });
 }      

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt){

       // creating instance for GuiUtils class

        javax.swing.JFileChooser chooser = GuiUtils.getImageFileChooser();
        if(filenameField.getText() != null)
            chooser.setSelectedFile(new java.io.File(filenameField.getText()));
        if( chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION){
            selectedFile = chooser.getSelectedFile();
            filenameField.setText(selectedFile.getAbsolutePath());
        }

    }


    private void verifyButtonActionPerformed(java.awt.event.ActionEvent evt){
        int i;
        for (i=0;i<10;i++)
        {

            statusTestbox.append( Integer.toString(i) + newline );

        }

     }

    public static void main(String[] args) {                       

        new DHMain().show(true);

}   

}

1 个答案:

答案 0 :(得分:7)

看起来不是

selectPanel.add(statusTestbox,scrollpane1)

你应该致电

scrollpane1=new JScrollPane(statusTestbox);
selectPanel.add(scrollpane1);