获取文本框滚动条时出现问题。我在下面给出了逻辑。有人可以帮助我吗?
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);
}
}
最佳答案
看起来像而不是
selectPanel.add(statusTestbox,scrollpane1)
你应该打电话
scrollpane1=new JScrollPane(statusTestbox);
selectPanel.add(scrollpane1);