简体   繁体   English

如何使JTextArea粘贴到窗口

[英]How to make JTextArea Stick to the window

Hello I would like to make this TextArea stick to the windows size whene I resize it by mouse, the same way as lower buttons does. 你好我想让这个TextArea坚持使用鼠标调整窗口尺寸,就像下按钮一样。 This is the code it is perfectly working no bugs, please have a glance at it. 这是完全没有错误的代码,请一目了然。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;


import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Rozklady extends JFrame {

public Rozklady() {
    super();    
}

public void createGUI(){
    setPreferredSize(new Dimension(400,150));
    JPanel jp = new JPanel();
//  jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
    jp.setLayout(new GridLayout(0,1));

    JPanel gora = new JPanel();
    JPanel dol = new JPanel();
    pack();
    JTextArea jt1 = new JTextArea("JF1");


    gora.add(jt1);

    jt1.setPreferredSize(new Dimension(getWidth(),getHeight()/2));
    dol.setLayout(new BorderLayout());

    JPanel lewo = new JPanel();
    JPanel prawo = new JPanel();
    JPanel srodek = new JPanel();



    dol.add(lewo, BorderLayout.EAST);
    dol.add(prawo,BorderLayout.WEST);
    dol.add(srodek, BorderLayout.CENTER);


    lewo.setLayout(new GridLayout(2,2));
    prawo.setLayout(new GridLayout(2,2));
    srodek.setLayout(new GridLayout(0,1));

    for(int i  = 0; i < 4; i++){
        lewo.add(new JButton(i+""));
        prawo.add(new JButton(i+""));
        if(i < 3){
            srodek.add(new JTextField("JF"+i));
        }
    }




    jp.add(gora);
    jp.add(dol);
    add(jp);
    setVisible(true);
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}





public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
               new Rozklady().createGUI();
            }
        });

}

}

Use BorderLayout for you gora panel. 为您使用BorderLayout gora面板。 Put text area to the center: 将文本区域放到中心:

gora.setLayout(new BorderLayout());
gora.add(jt1, BorderLayout.CENTER);
// declare a GridLayout in constructor, one component will 'fill the container'
JPanel gora = new JPanel(new GridLayout());
JPanel dol = new JPanel();
// this should be called after all components are added!  BNI
pack();
JTextArea jt1 = new JTextArea("JF1");

// be sure to use a scroll pane for multi-line text components
gora.add(new JScrollPane(jt1));
// ..

Stretching a single component to fill the available space can be achieved various was. 可以实现拉伸单个组件以填充可用空间。 Two common ways are using either BorderLayout as mentioned by AlexR or GridLayout . 两种常见的方法是使用AlexR或GridLayout提到的BorderLayout See this answer for sample code. 有关示例代码,请参阅此答案 I prefer GridLayout because it is shorter (less typing). 我更喜欢GridLayout因为它更短(更少打字)。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM