使用GridBagLayout在JTextArea中添加JScrollPane [英] Adding JScrollPane in JTextArea using GridBagLayout
本文介绍了使用GridBagLayout在JTextArea中添加JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用GridBagLayout在JTextArea中添加JScrollPane时遇到问题.基本上,当不需要滚动条时,程序可以正常运行,但当布局混乱时,布局会混乱,内容会被截断.相关代码如下
I'm having an issue adding JScrollPane in JTextArea using GridBagLayout. Basically the program runs fine when the scrollbar isn't needed but the layout gets messed up and the content gets cut off when it is. The relevent code is as follows
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");
}
}
}
});
}
}
我真的很想挥杆,而且我真的茫然不知所措.感谢您的所有帮助.
I'm really new to swing and I'm really at a loss where to go from here. Thanks for all your help.
推荐答案
- 首先,请学习 Java命名约定,这使得其他人更容易理解Java代码.
- First please learn Java Naming Conventions, that makes it a bit easier for the other person to understand the Java code.
- 为什么不简单使用 JTextArea.setLineWrap(true)和
JTextArea.setWrapStyleWord(true) 而不是定义
JScrollBar
策略, 这甚至在视图上看起来也不错:-) - 此外,与其指定
setSize()/setLocation()
方法, 只需使用frameReference.pack()
并frame.setLocationByPlatform(true)
,一个非常好的答案 关于后者的好处,请参见此答案,如何 使Swing GUI处于最佳位置 - 不要在一个类中创建太多的静态字段,这闻起来很糟 编程设计,并减少类的可扩展性.
- 您要将
JFrame
扩展到您的TestGUI
类,然后将其扩展到内部 它是创建相同实例的main()
方法.实际上 再次,尝试赋予组合重于继承的权重,因为 在这里,您实际上并没有尝试修改已经定义的JFrame
的功能,而您只是按原样使用它们,所以有 在这种情况下,至少不需要扩展JFrame
:-) - 了解 Swing中的并发
- Why not simply use JTextArea.setLineWrap(true) and
JTextArea.setWrapStyleWord(true) instead of defining
JScrollBar
policy, this will even look nice on the view :-) - Moreover, instead of specifying
setSize()/setLocation()
methods, simply useframeReference.pack()
andframe.setLocationByPlatform(true)
, a very wonderful answer regarding the benefit of the latter is mentioned in this answer, how to best position Swing GUIs - Do not make so many static fields in a class, this smells like a bad programming design, and makes your class less extensible.
- You are extending
JFrame
to yourTestGUI
class and then inside it'smain()
method you creating an instance of the same. Actually again, try to give more weightage to composition over inheritance, since over here, you not actually trying to modify the already defined features ofJFrame
, instead you just using them as is, so there is no need to extendJFrame
in this case atleast :-) - Read about 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);
}
}
这篇关于使用GridBagLayout在JTextArea中添加JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文