在使用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();
}
}发布于 2021-10-15 02:50:40
所以,有两件事,首先,总是用JScrollPane包装你的JTextArea,事情会变得非常…很奇怪..。不然的话。其次,虽然使用多个容器对于复杂的布局是一个好主意,但在这种情况下,我可能会考虑只使用一个容器并使用不同的布局(例如,除了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
https://stackoverflow.com/questions/69579365
复制相似问题