如何使 JScrollPane 与 JTextArea 一起出现?
How do I make JScrollPane appear with JTextArea?
我正在尝试制作一个 UI 来查看存储在计算机上的食谱中的食谱。此选项卡的一部分是一个 JScrollPanel,它存储一个显示可用食谱的 JTextArea。所有调用的函数都按预期工作(例如 allRecipes() returns 一串可用的食谱正确);但是,滚动窗格本身不会出现。它被添加到框架中,正如我可以通过窗格所在的小灰色块看到的那样,但它没有按应有的方式填充。代码如下:
//First panel, buttons to limit displayed recipes
JPanel pane1 = new JPanel();
JButton all = new JButton("All");
JButton makeable = new JButton("Makeable");
JTextField search = new JTextField("", 10);
JButton searchButton = new JButton("Search Ingredient");
//Second panel, display of recipes
JPanel pane2 = new JPanel();
JTextArea recipes = new JTextArea(allRecipes());
JLabel list = new JLabel("List of Recipes:");
JScrollPane scroll = new JScrollPane(recipes);
//Third panel, options to add recipe and view specific recipe
JPanel pane3 = new JPanel();
JButton add = new JButton("Add Recipe");
JTextField view = new JTextField("", 10);
JButton viewButton = new JButton("View Recipe");
//Central method
public Recipes() {
//basic UI stuff
super("Recipes");
setSize(475,350);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
//add pane 1
pane1.add(all);
pane1.add(makeable);
pane1.add(search);
pane1.add(searchButton);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane1);
//add pane 2
pane2.add(list);
scroll.setPreferredSize(new Dimension(10,15));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane2.add(scroll, BorderLayout.CENTER);
pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane2);
//add pane 3
pane3.add(add);
pane3.add(view);
pane3.add(viewButton);
add(pane3);
//start up the UI
setVisible(true);
}
JTextArea recipes = new JTextArea(allRecipes());
我们不知道 allRecipes()
的作用,但我猜它会设置文本区域的文本。
相反,您应该使用您希望的 rows/columns 定义文本区域。类似于:
JTextArea recipes = new JTextArea(5, 30);
然后在构造函数中添加文本:
recipes.setText( allRecipes() );
您不应尝试设置滚动窗格的首选大小。首选大小将根据文本区域的首选大小自动确定,文本区域的首选大小是根据构造函数中提供的 rows/columns 计算得出的。
//scroll.setPreferredSize(new Dimension(10,15));
此外,组件的首选大小是以像素为单位指定的,以上内容毫无意义。
pane2.add(scroll, BorderLayout.CENTER);
JPanel 的默认布局管理器是 FlowLayout。所以你不能在添加组件时只使用 BorderLayout 约束。
我正在尝试制作一个 UI 来查看存储在计算机上的食谱中的食谱。此选项卡的一部分是一个 JScrollPanel,它存储一个显示可用食谱的 JTextArea。所有调用的函数都按预期工作(例如 allRecipes() returns 一串可用的食谱正确);但是,滚动窗格本身不会出现。它被添加到框架中,正如我可以通过窗格所在的小灰色块看到的那样,但它没有按应有的方式填充。代码如下:
//First panel, buttons to limit displayed recipes
JPanel pane1 = new JPanel();
JButton all = new JButton("All");
JButton makeable = new JButton("Makeable");
JTextField search = new JTextField("", 10);
JButton searchButton = new JButton("Search Ingredient");
//Second panel, display of recipes
JPanel pane2 = new JPanel();
JTextArea recipes = new JTextArea(allRecipes());
JLabel list = new JLabel("List of Recipes:");
JScrollPane scroll = new JScrollPane(recipes);
//Third panel, options to add recipe and view specific recipe
JPanel pane3 = new JPanel();
JButton add = new JButton("Add Recipe");
JTextField view = new JTextField("", 10);
JButton viewButton = new JButton("View Recipe");
//Central method
public Recipes() {
//basic UI stuff
super("Recipes");
setSize(475,350);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
//add pane 1
pane1.add(all);
pane1.add(makeable);
pane1.add(search);
pane1.add(searchButton);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane1);
//add pane 2
pane2.add(list);
scroll.setPreferredSize(new Dimension(10,15));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane2.add(scroll, BorderLayout.CENTER);
pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane2);
//add pane 3
pane3.add(add);
pane3.add(view);
pane3.add(viewButton);
add(pane3);
//start up the UI
setVisible(true);
}
JTextArea recipes = new JTextArea(allRecipes());
我们不知道 allRecipes()
的作用,但我猜它会设置文本区域的文本。
相反,您应该使用您希望的 rows/columns 定义文本区域。类似于:
JTextArea recipes = new JTextArea(5, 30);
然后在构造函数中添加文本:
recipes.setText( allRecipes() );
您不应尝试设置滚动窗格的首选大小。首选大小将根据文本区域的首选大小自动确定,文本区域的首选大小是根据构造函数中提供的 rows/columns 计算得出的。
//scroll.setPreferredSize(new Dimension(10,15));
此外,组件的首选大小是以像素为单位指定的,以上内容毫无意义。
pane2.add(scroll, BorderLayout.CENTER);
JPanel 的默认布局管理器是 FlowLayout。所以你不能在添加组件时只使用 BorderLayout 约束。