我有一个带有盒子布局的 JPanel(a),所有组件都垂直堆叠在上面。其中,我有一个带有水平框布局的 JPanel(b)。在该 JPanel(b) 中,我添加了刚性区域 JPanel 和 JTextArea。
我想要的是每次 JTextArea 由于自动换行而扩展时,JPanel(b) 都会增加其高度。但是,由于我的 JPanel 在开始时具有单行的高度。JTextArea 不会扩展,因为所有空间都已填满。
有没有办法解决这个问题,另一种选择?
它不一定是 JPanel 和 JTextArea,只要是可以包含组件和支持多行的 JTextComponent 的东西。
class Question extends JPanel
{
public JPanel questionArea;
public JTextArea number, question;
public Question(Page page)
{
setSize(new Dimension(556, 100));
setBackground(Color.PINK);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
Border in = BorderFactory.createDashedBorder(Color.BLACK);
Border out = BorderFactory.createMatteBorder(0, 0, 10, 0, Color.WHITE);
setBorder(BorderFactory.createCompoundBorder(out, in));
questionArea = new JPanel();
questionArea.setPreferredSize(new Dimension(556, 32));
questionArea.setBackground(Color.YELLOW);
questionArea.setLayout(new BoxLayout(questionArea, BoxLayout.LINE_AXIS));
out = BorderFactory.createMatteBorder(0, 0, 8, 0, Color.WHITE);
setBorder(BorderFactory.createCompoundBorder(out, in));
number = new JTextArea();
number.setPreferredSize(new Dimension(25, 32));
number.setBackground(Color.GREEN);
number.setFont(new Font("Arial", Font.BOLD, 15));
number.setText("10.");
number.setEditable(false);
question = new JTextArea();
question.setPreferredSize(new Dimension(494, 32));
question.setBackground(Color.PINK);
question.setFont(new Font("Arial", Font.BOLD, 15));
question.setText("What is the first question?");
questionArea.add(Box.createRigidArea(new Dimension(35, 32)));
questionArea.add(number);
questionArea.add(question);
add(questionArea);
page.editArea.add(this, page.content);
}
}
休息
class Page extends JPanel
{
public JPanel editArea;
public Box.Filler blank;
public Page(JPanel panel)
{
setLayout(null);
setBounds(92, panel.getPreferredSize().height+40, 794, 1123);
setBackground(Color.WHITE);
editArea = new JPanel();
editArea.setLayout(new BoxLayout(editArea, BoxLayout.PAGE_AXIS));
editArea.setBounds(119, 96, 556, 931);
editArea.setBackground(Color.LIGHT_GRAY);
blank = new Box.Filler(new Dimension(556, -1), new Dimension(556, 931), new Dimension(556, 931));
editArea.add(blank);
add(editArea);
}
}
页面类本身位于具有空布局的 JPanel 上,不需要代码,对吧?