3

我在使用 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");
                    }
                }
            }
        });

    }
}

我真的很陌生,我真的不知道从这里去哪里。感谢你的帮助。

4

2 回答 2

5

现在到实际的东西:-)

  • 为什么不简单地使用JTextArea.setLineWrap(true)JTextArea.setWrapStyleWord(true)而不是定义JScrollBar策略,这在视图上甚至看起来不错:-)
  • 此外,不是指定setSize()/setLocation()方法,而是简单地使用frameReference.pack()and frame.setLocationByPlatform(true),关于后者的好处的一个非常好的答案在这个答案中提到,how to best position Swing GUIs
  • 不要在一个类中创建这么多静态字段,这闻起来像是糟糕的编程设计,并且会降低您的类的可扩展性。
  • 您正在扩展JFrame到您的TestGUI类,然后在它的main()方法中创建相同的实例。实际上,再次尝试赋予组合更多的权重而不是继承,因为在这里,您实际上并没有尝试修改 已经定义的功能,而是按原样使用它们,因此至少在这种情况下JFrame不需要扩展:- JFrame)
  • 阅读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);
    }
}
于 2013-08-26T16:47:43.780 回答
3

添加 JScrollPane,然后将 JLabel 添加到相同的网格位置。然后您稍后添加没有 JScrollPane 的原始 JTextArea。

试试这个,它只添加包含你的 JTextArea 的 JScrollPane。我还将您的 GUI 创建移到了一个构造函数中,该构造函数通过SwingUtilities.invokeLater调用来确保它位于 EDT 上。有关EDT 的更多详细信息,请参阅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();
            }
        });
    }
}
于 2013-08-26T16:59:26.127 回答