我想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下面的一些小广场。感谢您的反馈
'textArea1.setPreferredSize(新尺寸(490200));' - ** **从来没有做到这一点。认真。您约束的JTextArea,使其永远不会扩大,因为更多的文本添加,所以你永远不会看到滚动条,而不是看文字这种规模超越。改为设置可见的列和行。 –
接下来,将textArea本身添加到JScrollPane和JPanel(??)。不会。将它添加到一个组件,即JScrollPane,然后将该JScrollPane添加到其他组件中。 –
我已回滚您的问题代码。您正在做的事情会大幅改变,从而使答案失效。请参阅我的答案中的示例代码。 –