Java 使用GridBagLayout在JTextArea中添加JScrollPane
我在使用GridBagLayout在JTextArea中添加JScrollPane时遇到问题。基本上,当不需要滚动条时,程序运行正常,但当滚动条被使用时,布局会变得混乱,内容会被切断。相关代码如下所示Java 使用GridBagLayout在JTextArea中添加JScrollPane,java,swing,jscrollpane,jtextarea,gridbaglayout,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 { pu
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");
}
}
}
});
}
}
import java.io.*;
导入java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.util.*;
公共类testGUI扩展了JFrame
{
公共静态字符串名;
静态JTextField textfield=新的JTextField(30);
静态JTextArea textarea=新的JTextArea(30,30);
公共静态void main(字符串[]args)
{
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
帧。设置标题(“Checkem”);
帧设置位置(500400);
框架设置尺寸(800800);
JPanel panel=newjpanel(newgridbagloayout());
GridBagConstraints c=新的GridBagConstraints();
JScrollPane scrolltxt=新的JScrollPane(文本区,JScrollPane.VERTICAL\u SCROLLBAR\u根据需要,JScrollPane.HORIZONTAL\u SCROLLBAR\u NEVER);
scrolltxt.setWheelScrollingEnabled(真);
scrolltxt.getVerticalScrollBar().isVisible();
panel.add(scrolltxt,c);
JLabel=newjlabel(“输入文件名:”);
c、 gridx=0;
c、 gridy=0;
c、 插图=新插图(2,2,2,2);
面板。添加(标签,c);
c、 gridx=0;
c、 gridy=1;
面板。添加(文本区域,c);
JButton按钮=新JButton(“搜索”);
c、 gridx=1;
c、 gridy=1;
面板。添加(按钮,c);
c、 gridx=1;
c、 gridy=0;
panel.add(textfield,c);
frame.getContentPane().add(面板,BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
addActionListener(新建ActionListener())
{
已执行的公共无效行动(行动事件ae)
{
Checkem记录=新Checkem();
name=textfield.getText();
String[]print=record.run(name);
对于(int i=0;i
- 首先请学习,这使其他人更容易理解Java代码
现在谈谈实际情况:-)
- 为什么不简单地使用和
而不是定义
JScrollBar
策略,
这甚至在视图上看起来也不错:-)
- 此外,不是指定
setSize()/setLocation()
方法,
只需使用frameReference.pack()
和
frame.setLocationByPlatform(true)
,一个非常好的答案
关于后者的好处,在回答中提到
- 不要在一个类中创建这么多静态字段,这闻起来像是一个坏消息
编程设计,并降低类的可扩展性
- 您正在将
JFrame
扩展到TestGUI
类,然后在
它是main()
方法,您可以创建相同的实例
再一次,试着给予组合更多的权重,而不是继承,因为
在这里,您实际上并没有试图修改已经定义的
JFrame的特性,而您只是按原样使用它们,所以
在这种情况下,无需扩展JFrame
,至少:-)
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);
}
}
import java.io.*;
导入java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.util.*;
公共类TestGUI{
私有字符串名称;
私有JTextField textfield=新JTextField(30);
私有JTextArea textarea=新JTextArea(30,30);
私有void displayGUI(){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
帧。设置标题(“Checkem”);
JPanel panel=newjpanel(newgridbagloayout());
GridBagConstraints c=新的GridBagConstraints();
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
JScrollPane scrolltxt=新的JScrollPane();
scrolltxt.setViewportView(文本区域);
scrolltxt.setWheelScrollingEnabled(真);
JLabel=newjlabel(“输入文件名:”);
c、 gridx=0;
c、 gridy=0;
c、 插图=新插图(2,2,2,2);
面板。添加(标签,c);
c、 gridx=0;
c、 gridy=1;
panel.add(scrolltxt,c);
JButton按钮=新JButton(“搜索”);
c、 gridx=1;
c、 gridy=1;
面板。添加(按钮,c);
c、 gridx=1;
c、 gridy=0;
panel.add(textfield,c);
frame.getContentPane().add(面板,BorderLayout.NORTH);
//框架设置尺寸(800800);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
addActionListener(新建ActionListener())
{
已执行的公共无效行动(行动事件ae)
{
/*Checkem记录=新Checkem();
name=textfield.getText();
String[]print=record.run(name);
对于(inti=0;i您添加了JScrollPane,但随后将JLabel添加到相同的网格位置。然后添加原始JTextArea而不添加JScrollPane
尝试此操作,它只添加包含JTextArea的JScrollPane。我还将GUI创建移动到一个构造函数中,该构造函数使用SwingUtilities.invokeLater
调用进行调用,以确保它位于EDT上。有关EDT的更多详细信息,请参阅。这还允许您不让所有类成员变量都是静态的,这不是很正常od练习
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();
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
进口j