使用GridBagLayout在JTextArea中添加JScrollPane

时间:2013-08-26 16:22:26

标签: java swing jscrollpane jtextarea gridbaglayout

我在使用GridBagLayout在JTextArea中添加JScrollPane时遇到问题。基本上,当不需要滚动条时程序运行正常,但布局搞砸了,内容被切断了。相关代码如下

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class testGUI extends JFrame
{
    public static String name;
    static JTextField textfield = new JTextField(30);
    static JTextArea  textarea = new JTextArea(30,30);

    public static void main( String[] args)
    {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Checkem");
        frame.setLocation(500,400);
        frame.setSize(800,800);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JScrollPane scrolltxt = new JScrollPane(textarea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrolltxt.setWheelScrollingEnabled(true); 
        scrolltxt.getVerticalScrollBar().isVisible();
        panel.add(scrolltxt, c); 

        JLabel label = new JLabel("Enter the Name of the file:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(2,2,2,2);

        panel.add(label,c);

        c.gridx = 0;
        c.gridy = 1;
        panel.add(textarea,c);      

        JButton button = new JButton("Search");
        c.gridx = 1;
        c.gridy = 1;
        panel.add(button,c);

        c.gridx = 1;
        c.gridy = 0;
        panel.add(textfield,c);


        frame.getContentPane().add(panel, BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);

        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                Checkem record = new Checkem();
                name = textfield.getText();         
                String [] print = record.run(name);

                for (int i=0;i<print.length;i++)
                {
                    if(print[i] == null || print[i].isEmpty())
                    {
                        continue;
                    }
                    else
                    {
                        textarea.append(print[i] + "\n");
                    }
                }
            }
        });

    }
}

我真的很开心,我真的很不知所措。谢谢你的帮助。

2 个答案:

答案 0 :(得分:5)

现在到实际的东西: - )

  • 为什么不简单地使用JTextArea.setLineWrap(true)JTextArea.setWrapStyleWord(true)而非定义JScrollBar政策, 这甚至看起来很不错: - )
  • 此外,不是指定setSize()/setLocation()方法, 只需使用frameReference.pack()frame.setLocationByPlatform(true),一个非常好的答案 关于后者的好处,在这个答案how to best position Swing GUIs
  • 中提到
  • 不要在课堂上制作这么多静态字段,这闻起来很糟糕 编程设计,使您的课程不那么可扩展。
  • 您正在将JFrame扩展到TestGUI课程,然后再进入内部 它是main()方法,您可以创建相同的实例。其实 再次,尝试给组合赋予更多的权重而不是继承,因为 在这里,你实际上并没有尝试修改已定义的 JFrame的功能,而不是按原样使用它们,所以有 在这种情况下无需延长JFrame至少: - )
  • 了解Concurrency in Swing

以下是您修改后的代码:

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class TestGUI {

    private String name;
    private JTextField textfield = new JTextField(30);
    private JTextArea  textarea = new JTextArea(30,30);

    private void displayGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Checkem");        

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        textarea.setLineWrap(true);
        textarea.setWrapStyleWord(true);
        JScrollPane scrolltxt = new JScrollPane();
        scrolltxt.setViewportView(textarea);
        scrolltxt.setWheelScrollingEnabled(true);

        JLabel label = new JLabel("Enter the Name of the file:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(2,2,2,2);

        panel.add(label,c);

        c.gridx = 0;
        c.gridy = 1;
        panel.add(scrolltxt,c);      

        JButton button = new JButton("Search");
        c.gridx = 1;
        c.gridy = 1;
        panel.add(button,c);

        c.gridx = 1;
        c.gridy = 0;
        panel.add(textfield,c);


        frame.getContentPane().add(panel, BorderLayout.NORTH);      
        //frame.setSize(800,800);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                /*Checkem record = new Checkem();
                name = textfield.getText();         
                String [] print = record.run(name);

                for (int i=0;i<print.length;i++)
                {
                    if(print[i] == null || print[i].isEmpty())
                    {
                        continue;
                    }
                    else
                    {
                        textarea.append(print[i] + "\n");
                    }
                }*/
            }
        });
    }

    public static void main( String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new TestGUI().displayGUI();
            }
        };
        EventQueue.invokeLater(r);
    }
}

答案 1 :(得分:3)

添加JScrollPane,然后将JLabel添加到相同的网格位置。然后在没有JScrollPane的情况下添加原始JTextArea。

尝试这只添加包含JTextArea的JScrollPane。我还将您的GUI创建移动到一个通过SwingUtilities.invokeLater调用调用的构造函数,以确保它在EDT上。有关EDT的更多详细信息,请参阅Concurrency in Swing。这也允许你不要将所有的类成员变量都设置为静态,这不是很好的做法。

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestGUI extends JFrame {

    String name;
    JTextField textfield = new JTextField(30);
    JTextArea textarea = new JTextArea(30, 30);

    public TestGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JScrollPane scrolltxt = new JScrollPane(textarea);

        JLabel label = new JLabel("Enter the Name of the file:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(2, 2, 2, 2);

        panel.add(label, c);
        c.gridx = 0;
        c.gridy = 1;
        panel.add(scrolltxt, c);

        JButton button = new JButton("Search");
        c.gridx = 1;
        c.gridy = 1;
        panel.add(button, c);

        c.gridx = 1;
        c.gridy = 0;
        panel.add(textfield, c);

        frame.getContentPane().add(panel, BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                Checkem record = new Checkem();
                name = textfield.getText();
                String [] print = record.run(name);

                for (int i=0;i<print.length;i++) {
                    if(print[i] == null || print[i].isEmpty()) {
                        continue;
                    } else {
                        textarea.append(print[i] + "\n");
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestGUI();
            }
        });
    }
}