JTextArea 问题

Posted

技术标签:

【中文标题】JTextArea 问题【英文标题】:JTextArea problems 【发布时间】:2021-12-03 08:12:49 【问题描述】:

JTextArea 有点麻烦,特别是它的缩放。每当我尝试重新调整指令区域的大小时,它真的会与我的所有其他组件混淆。我认为这是由于一些奇怪的 BorderLayout 交互,但我不确定。找到一种方法来保留当前组件,同时拥有一个合适大小的说明区域将是惊人的。

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

public class CoffeeShop extends JFrame 
  
  private JLabel coffeemenu, seasonaldrinks, instructions;
  private JPanel westpanel, westpanel2;
  private JComboBox menubox, seasonalmenubox;
  private JTextArea instructionsarea;
  
  
  public CoffeeShop()
    
    coffeemenu = new JLabel("Coffee Menu:");
    seasonaldrinks = new JLabel("Seasonal Drinks:");
    instructions = new JLabel("List Any Special Instructions:");
    
    instructionsarea = new JTextArea(3, 1);
    
    westpanel = new JPanel();
    westpanel2 = new JPanel();
    westpanel.add(coffeemenu);
    westpanel2.add(westpanel);
    add(westpanel2, BorderLayout.WEST);
    westpanel.setLayout(new GridLayout(6,1));
    
    
    
    String menuboxoption [] = "Choose Below:","Drip Coffee", "Cookie Butter Latte", "Caramel Latte", 
                               "Matcha Latte", "Craft Matcha", "Classic Latte", "Vanilla Latte", "Hazlenut Latte";
    
    String seasonalmenuboxoption [] = "Choose Below:", "Iced Pumpkin Butter Latte","Iced Autumn Spice Latte", "Crisp Apple Sparkling Cider", 
                                       "Iced Melting Monster Latte", "Hot Pumpkin Cookie Butter Latte";
    menubox = new JComboBox(menuboxoption);
    seasonalmenubox = new JComboBox(seasonalmenuboxoption);
    westpanel.add(menubox);
    westpanel.add(seasonaldrinks);
    westpanel.add(seasonalmenubox);
    
    westpanel.add(instructions);
    westpanel.add(instructionsarea);
    
    setTitle("Commonwealth Joe's");
    setSize(900, 500);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
  
  
  
  
  public static void main(String [] args)
    CoffeeShop run = new CoffeeShop();
    
  

【问题讨论】:

【参考方案1】:

所以,有两件事,第一,总是把你的 JTextArea 包裹在一个 JScrollPane 中,事情会变得非常……奇怪……否则。其次,虽然对于复杂的布局使用多个容器是一个好主意,但在这种情况下,我可能会考虑只使用一个容器并使用不同的布局(例如 GridLayout 以外的其他布局......

可运行示例

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class CoffeeShop extends JFrame 

    private JLabel coffeemenu, seasonaldrinks, instructions;
    private JPanel westpanel;
    private JComboBox menubox, seasonalmenubox;
    private JTextArea instructionsarea;

    public CoffeeShop() 

        coffeemenu = new JLabel("Coffee Menu:");
        seasonaldrinks = new JLabel("Seasonal Drinks:");
        instructions = new JLabel("List Any Special Instructions:");

        instructionsarea = new JTextArea(3, 1);

        String menuboxoption[] = "Choose Below:", "Drip Coffee", "Cookie Butter Latte", "Caramel Latte",
            "Matcha Latte", "Craft Matcha", "Classic Latte", "Vanilla Latte", "Hazlenut Latte";

        String seasonalmenuboxoption[] = "Choose Below:", "Iced Pumpkin Butter Latte", "Iced Autumn Spice Latte", "Crisp Apple Sparkling Cider",
            "Iced Melting Monster Latte", "Hot Pumpkin Cookie Butter Latte";
        menubox = new JComboBox(menuboxoption);
        seasonalmenubox = new JComboBox(seasonalmenuboxoption);

        westpanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(8, 8, 8, 8);

        westpanel.add(coffeemenu, gbc);
        westpanel.add(menubox, gbc);

        westpanel.add(seasonaldrinks, gbc);
        westpanel.add(seasonalmenubox, gbc);

        westpanel.add(instructions, gbc);
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        westpanel.add(new JScrollPane(instructionsarea), gbc);

        add(westpanel, BorderLayout.WEST);

        setTitle("Commonwealth Joe's");
        setSize(900, 500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    public static void main(String[] args) 
        CoffeeShop run = new CoffeeShop();

    

查看How to Use GridBagLayout了解更多详情

【讨论】:

以上是关于JTextArea 问题的主要内容,如果未能解决你的问题,请参考以下文章

JTextArea 问题

JTextArea:居中对齐

JTextArea 到 ArrayList<String> 的行

更改 JTextArea 中的文本选择类型

JTextArea 中的文本更改事件?如何?

如何保存 JTextArea 文本?