更新时间:2023-02-05 21:57:22
作为第一个改进,您可以进行一些小的布局更改,以使JTextArea
占据整个空间并随着框架调整大小:
As first improvement you can make some small layout changes that will make the JTextArea
occupy the whole space and resize with the frame :
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class Problematic {
public static void main(String[] args) {
JFrame f = new JFrame("frame");
f.setLayout(new BorderLayout());
JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
JTextArea t2 = new JTextArea(5, 30);
t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");
JScrollPane scrollPane = new JScrollPane(t2);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p.add(scrollPane);
f.add(p, BorderLayout.CENTER); //place text area panel in center position
JPanel p1 = new JPanel();
JButton button = new JButton("Whatever here");
p1.add(button);
f.add(p1, BorderLayout.PAGE_END);
f.setSize(600, 500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
为获得更大的灵活性,您可以添加JSplitPane
:
for additional flexibility you can add a JSplitPane
:
public class Problematic {
public static void main(String[] args) {
JFrame f = new JFrame("frame");
f.getContentPane().setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JButton button = new JButton("Whatever here");
p1.add(button);
JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
JTextArea t2 = new JTextArea(5, 30);
t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");
JScrollPane scrollPane = new JScrollPane(t2); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p.add(scrollPane);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
f.getContentPane().add(splitPane, BorderLayout.CENTER);
f.setSize(600, 500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}