为什么我的JTextArea中没有JScrollPanel?

时间:2015-11-04 01:31:32

标签: java swing

我正在尝试将JScrollPane添加到我的JTextArea中,但它不起作用。有人能告诉我有什么问题吗?我想只有横向的

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下面的一个小方块。感谢您的反馈

2 个答案:

答案 0 :(得分:5)

您的问题是由于您将JScrollPane和JTextArea都添加到thePanel JPanel,因此您会看到两者:一个JTextArea 没有 JScrollPanes和一个空的JScrollPane。

  • 不要添加textArea本身添加到JScrollPane以及JPanel,因为您只能将组件添加到一个容器。相反,将它添加到一个组件JScrollPane(实际上你将它添加到它的视口视图,但如果你将它传递给JScrollPane的构造函数,那么你就是这样做了),然后将JScrollPane添加到其他东西。 / LI>
  • 此外,从不设置文本组件的首选大小。您约束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();
            }
        });
    }
}

答案 1 :(得分:0)

  

试试这个:

 ArrayTwo(0) = "January 2015"