2015-11-04 71 views
1

我想JScrollPane中添加到我的JTextArea,它不工作。有人能告诉我什么是错的吗?我想只有水平的一个为什么我的JTextArea中没有JScrollPanel?

import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

import javax.swing.*; 

public class Program extends JFrame{ 

    JButton button1, button2; 
    JTextArea textArea1; 
    JScrollPane scroller; 
    public static String directory, nameOfFile, finalPath; 

    public static void main(String[] args){ 

     new Program(); 

    } 

    public Program(){ 

     this.setSize(500,500); 
     this.setLocationRelativeTo(null); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setResizable(false); 
     this.setTitle("swinging"); 

     JPanel thePanel = new JPanel(); 
     thePanel.setLayout(null); 
     thePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 

     button1 = new JButton("Wybierz plik:"); 
     button2 = new JButton("Dodaj"); 

     final JTextField text = new JTextField(24); 
     textArea1 = new JTextArea(); 
     textArea1.setEditable(true);           
     textArea1.setLineWrap(true); 
     textArea1.setPreferredSize(new Dimension(490,200)); 

     scroller = new JScrollPane(textArea1); 
     scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

     thePanel.add(button1); 
     thePanel.add(text); 
     thePanel.add(button2); 

     thePanel.add(textArea1); 
     thePanel.add(scroller);  
     this.add(thePanel); 
     this.setVisible(true);   
    } 
} 

所有我看到后添加它是textarea下面的一些小广场。感谢您的反馈

+1

'textArea1.setPreferredSize(新尺寸(490200));' - ** **从来没有做到这一点。认真。您约束的JTextArea,使其永远不会扩大,因为更多的文本添加,所以你永远不会看到滚动条,而不是看文字这种规模超越。改为设置可见的列和行。 –

+0

接下来,将textArea本身添加到JScrollPane和JPanel(??)。不会。将它添加到一个组件,即JScrollPane,然后将该JScrollPane添加到其他组件中。 –

+0

我已回滚您的问题代码。您正在做的事情会大幅改变,从而使答案失效。请参阅我的答案中的示例代码。 –

回答

5

你的问题是由于你的加入JScrollPane中和的JTextArea同时向thePanel的JPanel,所以你看到两个:一个JTextArea 没有 JScrollPanes和一个空的JScrollPane中。

  • 不要添加textarea的自身添加到JScrollPane的,也到一个JPanel,因为你只能添加组件一个容器。相反,它添加到一个组件,JScrollPane的(实际上是你将它添加到它的视景,但如果你将它传递到JScrollPane的构造函数,那么你这样做),然后添加JScrollPane的一种东西。
  • 此外,NEVER设置文本组件的优选的尺寸。您限制JTextArea,以便在添加更多文本时不会展开,因此您将永远不会看到滚动条并且看不到超出此大小的文本。改为设置可见的列和行。例如,textArea1 = new JTextArea(rows, columns);

请注意,这并没有多大意义:

thePanel.setLayout(null); 
thePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 

我不知道你正在尝试做的在这里,因为1)你想设置一个容器的布局中只有一次,2)总体而言,您将希望避免使用null布局。

例如:

import java.awt.BorderLayout; 
import javax.swing.*; 

public class MyProgram extends JPanel { 
    private static final int T_FIELD_COLS = 20; 
    private static final int TXT_AREA_ROWS = 15; 
    private static final int TXT_AREA_COLS = 20; 
    private JButton button1 = new JButton("Button 1"); 
    private JButton button2 = new JButton("Button 2"); 
    private JTextField textField = new JTextField(T_FIELD_COLS); 
    private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS); 

    public MyProgram() { 
     // Create a JPanel to hold your top line of components 
     JPanel topPanel = new JPanel(); 

     int gap = 3; 
     topPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap)); 

     // set this JPanel's layout. Here I use BoxLayout. 
     topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS)); 
     topPanel.add(button1); 
     topPanel.add(Box.createHorizontalStrut(gap)); 
     topPanel.add(textField); 
     topPanel.add(Box.createHorizontalStrut(gap)); 
     topPanel.add(button2); 

     // so the JTextArea will wrap words 
     textArea.setLineWrap(true); 
     textArea.setWrapStyleWord(true); 

     // add the JTextArea to the JScrollPane's viewport: 
     JScrollPane scrollPane = new JScrollPane(textArea); 
     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

     // set the layout of the main JPanel. 
     setLayout(new BorderLayout()); 
     add(topPanel, BorderLayout.PAGE_START); 
     add(scrollPane, BorderLayout.CENTER); 
    } 

    private static void createAndShowGui() { 
     MyProgram mainPanel = new MyProgram(); 

     JFrame frame = new JFrame("My Program"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); // don't set the JFrame's size, preferred size or bounds 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     // start your program on the event thread 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 
0

试试这个:

textArea1 = new JTextArea(); 

textArea1.setColumns(20); 

textArea1.setRows(5); 

scroller.setViewportView(textArea1); 
+0

*“试试这个:”* - 为什么?在unexpecting人为之倾倒的代码并不像看起来那么有用,为什么你的方法将有助于OP解决他们的问题,不仅有助于他们犯同样的错误在未来,也帮助别人谁可能有一些解释类似的问题 – MadProgrammer