未在jscrollpane中设置jtextarea的位置和大小 [英] location and size of jtextarea in jscrollpane is not set

查看:166
本文介绍了未在jscrollpane中设置jtextarea的位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑.我正在使用Java swing.我已经在JScrollPane中嵌入了JTextArea.我想将特定尺寸的jtextarea放置在JScrollPane的中间.为此,我使用了setLocation函数.但这行不通吗?

I am working on the editor. I am using Java swing for it. I have embedded a JTextArea with JScrollPane. i want to position the jtextarea of particular size at the middle of JScrollPane. To do this I used setLocation function. But this is not working?

public class ScrollPaneTest extends JFrame {
private Container myCP;
private JTextArea resultsTA;
private JScrollPane scrollPane;
private  JPanel jpanel;

public ScrollPaneTest() {
resultsTA = new JTextArea(50,50);
resultsTA.setLocation(100,100);
jpanel=new JPanel(new BorderLayout());
jpanel.add(resultsTA,BorderLayout.CENTER);

scrollPane = new JScrollPane(jpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(800, 800));
scrollPane.setBounds(0, 0, 800, 800);

setSize(800, 800);
setLocation(0, 0);
myCP = this.getContentPane();
myCP.setLayout(new BorderLayout());
myCP.add(scrollPane);
setVisible(true);
addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
 });
}
public static void main(String[] args) {
     new ScrollPaneTest();
     }
}

推荐答案

您只需要将JTextArea添加到JScrollPane,然后将其添加到具有BorderLayoutJPanelCENTER中.

You simply have to add the JTextArea to the JScrollPane, and add it to the CENTER of the JPanel having BorderLayout.

不要使用AbsolutePositioning.添加适当的 LayoutManager ,然后让LayoutManager完成其余的定位操作并在屏幕上调整组件的大小.

Don't use AbsolutePositioning. Add a proper LayoutManager, and let LayoutManager do the rest for positioning and sizing your components on the screen.

要使用setBounds(...)方法,必须为

In order to use the setBounds(...) method you have to use a null Layout for your component, which is not worth using, provided the perspective, as mentioned in the first paragraph of the AbsolutePositioning. Though in the code example provided by you, you are doing both the thingies together i.e. using Layout and using AbsolutePositioning, which is wrong in every way. My advice STOP DOING IT :-)

在提供的示例中,您提供的 ROWS COLUMNS 足以根据布局问题调整JTextArea的大小.

In the example provided the ROWS and COLUMNS provided by you are sufficient to size the JTextArea by the Layout concern.

代码示例:

import java.awt.*;
import javax.swing.*;

public class Example
{
    private JTextArea tarea;

    private void displayGUI()
    {
        JFrame frame = new JFrame("JScrollPane Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        JScrollPane textScroller = new JScrollPane();
        tarea = new JTextArea(30, 30);
        textScroller.setViewportView(tarea);

        contentPane.add(textScroller);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Example().displayGUI();
            }
        });
    }
}

这篇关于未在jscrollpane中设置jtextarea的位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆