2010-05-09 29 views
0

我的程序已經完成了,但是測試一下,我發現scrollpanel沒有出現,它只是改變了JTextArea的大小。代碼提供如下:java中的ScrollPanel沒有出現JTextArea改變了大小

package javaapplication15; 

import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.*; 
import java.io.IOException; 

import javax.swing.*; 

public class Tekstprogram extends JFrame { 

    public Tekstprogram() { 

     setSize(400, 600); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setResizable(false); 
     Container Indhold = getContentPane(); 
     Indhold.setLayout(new FlowLayout()); 

     JButton openButton = new JButton("Open"); 
     JButton saveButton = new JButton("Save"); 

     final JLabel statusbar = 
       new JLabel("Output of your selection will go here"); 

     final JTextArea TekstOmråde = new JTextArea(29, 30); 

     JScrollPane scrollText = new JScrollPane(TekstOmråde); 

     openButton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent ae) { 

       JFileChooser chooser = new JFileChooser(); 
       chooser.setMultiSelectionEnabled(true); 
       int option = chooser.showOpenDialog(Tekstprogram.this); 
       if (option == JFileChooser.APPROVE_OPTION) { 
        File[] sf = chooser.getSelectedFiles(); 
        String filelist = "nothing"; 
        if (sf.length > 0) { 
         filelist = sf[0].getName(); 
        } 
        for (int i = 1; i < sf.length; i++) { 
         filelist = filelist + ", " + sf[i].getName(); 
        } 

        try { 
         String strLine; 
         File selectedFile = chooser.getSelectedFile(); 
         FileInputStream in = new FileInputStream(selectedFile); 
         BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
         while ((strLine = br.readLine()) != null) { 
          TekstOmråde.append(strLine + "\n"); 
         } 
        } catch (Exception e) { 
         System.out.println("En fejl opstod ved" + e); 
        } 

       } 
      } 
     }); 

     saveButton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent ae) { 
       JFileChooser chooser = new JFileChooser(); 
       int option = chooser.showSaveDialog(Tekstprogram.this); 
       if (option == JFileChooser.APPROVE_OPTION) { 
        File file = chooser.getSelectedFile(); 
        try { 
         BufferedWriter out = new BufferedWriter(new FileWriter(file)); 

         out.write(TekstOmråde.getText()); 
         out.close(); 

        } catch (IOException e) { 
         System.out.println("IOException fejl opstod :"); 
         e.printStackTrace(); 
        } 

       } 

      } 
     }); 

     Indhold.add(openButton); 
     Indhold.add(saveButton); 
     Indhold.add(TekstOmråde); 
     Indhold.add(scrollText); 
     Indhold.add(statusbar); 
    } 

    public static void main(String args[]) { 
     Tekstprogram sfc = new Tekstprogram(); 
     sfc.setVisible(true); 
    } 
} 

是否有任何使JTextArea靜態?

回答

2

刪除 Indhold.add(TekstOmråde);

既然你有

JScrollPane scrollText = new JScrollPane(TekstOmråde); 

你正在做

Indhold.add(scrollText); 
+0

已經加入TexstOmråde間接+ 1,Swing組件只能有一個單親。首先將其添加到滾動窗格中,但是當您將其直接添加到面板時,將其從滾動窗格中移除。 – camickr 2010-05-09 14:56:08

1

textarea的需要是滾動窗格內