我在使用 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");
}
}
}
});
}
}
我真的是 swing 的新手,我真的不知道从这里去哪里。感谢您的帮助。
最佳答案
- 首先请学习Java Naming Conventions ,这让其他人更容易理解 Java 代码。
现在进入实际的东西:-)
- 为什么不直接使用 JTextArea.setLineWrap(true)和
JTextArea.setWrapStyleWord(true)而不是定义
JScrollBar
策略, 这在 View 上什至会很好看 :-) - 此外,不是指定
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);
}
}
关于java - 使用 GridBagLayout 在 JTextArea 中添加 JScrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18448586/