本文介绍了如何使JTextArea粘贴到窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我想让这个TextArea坚持使用鼠标调整大小的窗口尺寸,就像下按钮一样。这是完全没有错误的代码,请一目了然。
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. 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();
}
});
}
}
推荐答案
使用 BorderLayout
为您 gora
面板。将文本区域放到中心:
Use BorderLayout
for you gora
panel. Put text area to the center:
gora.setLayout(new BorderLayout());
gora.add(jt1, BorderLayout.CENTER);
这篇关于如何使JTextArea粘贴到窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!