将 ScrollPane 添加到 BoxLayout
Adding ScrollPane to BoxLayout
在我的程序中,
按下时我有一个按钮,将新的 JPanel 添加到带有 BOXLAYOUT 的 JPanel。因为当我添加 JPanel 时,它会将它添加到前一个的底部。但是没有滚动条。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class tes{
public static void main(String[] args) {
JFrame newL = new JFrame();
newL.setTitle("New Level Files");
//newL.setLayout(new BoxLayout());
//t.setSize(500,600);
//newL.pack();
newL.setVisible(true);
newL.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(listPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(600, 100));
newL.add(scrollPane, BorderLayout.CENTER);
JPanel levelP = new JPanel();
levelP.setBorder(BorderFactory.createLineBorder(Color.black));
levelP.setLayout(new GridBagLayout());
GridBagConstraints l = new GridBagConstraints();
l.insets = new Insets(10,10,10,5);
JButton okForFileEdit = new JButton("Edit this File");
l.gridx = 1;
l.gridy = 6;
levelP.add(okForFileEdit, l);
okForFileEdit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent n){
JPanel createInPanel = new JPanel();
createInPanel.setSize(200,200);
createInPanel.setBorder(BorderFactory.createLineBorder(Color.black));
createInPanel.setLayout(new GridBagLayout());
listPane.add(createInPanel);
JLabel yout = new JLabel("YEah this is a long sentence to see the placement");
createInPanel.add(yout);
listPane.revalidate();
listPane.repaint();
newL.revalidate();
}});
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
newL.pack();
}
** 我添加了一个没有制作滚动条的程序的简化版本
希望这更有意义。提前感谢您的帮助:);
I have a button when pressed, adds a new JPanel to a JPanel with BOXLAYOUT.
当您向面板动态添加组件时,您的 ActionListener 中需要的基本逻辑是:
panel.add(...);
panel.revalidate();
panel.repaint();
您需要调用 revalidate 方法来调用面板的布局管理器,以便重新计算首选尺寸。
编辑:
newL.add(listPane);
您不需要上述声明。一个 Swing 组件只能有一个父组件。该语句从滚动窗格中删除了面板,因此您看不到滚动条。
newL.revalidate();
你不需要那个声明。正如我在回答中所说,您只需要重新验证()您更改的面板。
此外,您不需要以下所有陈述:
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
即当您打包框架或使框架可见时调用布局管理器。您的代码应该是:
newl.pack();
newl.setVisible(true);
也就是说,您应该只在将所有组件添加到框架后才使框架可见。
在我的程序中, 按下时我有一个按钮,将新的 JPanel 添加到带有 BOXLAYOUT 的 JPanel。因为当我添加 JPanel 时,它会将它添加到前一个的底部。但是没有滚动条。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class tes{
public static void main(String[] args) {
JFrame newL = new JFrame();
newL.setTitle("New Level Files");
//newL.setLayout(new BoxLayout());
//t.setSize(500,600);
//newL.pack();
newL.setVisible(true);
newL.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(listPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(600, 100));
newL.add(scrollPane, BorderLayout.CENTER);
JPanel levelP = new JPanel();
levelP.setBorder(BorderFactory.createLineBorder(Color.black));
levelP.setLayout(new GridBagLayout());
GridBagConstraints l = new GridBagConstraints();
l.insets = new Insets(10,10,10,5);
JButton okForFileEdit = new JButton("Edit this File");
l.gridx = 1;
l.gridy = 6;
levelP.add(okForFileEdit, l);
okForFileEdit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent n){
JPanel createInPanel = new JPanel();
createInPanel.setSize(200,200);
createInPanel.setBorder(BorderFactory.createLineBorder(Color.black));
createInPanel.setLayout(new GridBagLayout());
listPane.add(createInPanel);
JLabel yout = new JLabel("YEah this is a long sentence to see the placement");
createInPanel.add(yout);
listPane.revalidate();
listPane.repaint();
newL.revalidate();
}});
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
newL.pack();
}
** 我添加了一个没有制作滚动条的程序的简化版本 希望这更有意义。提前感谢您的帮助:);
I have a button when pressed, adds a new JPanel to a JPanel with BOXLAYOUT.
当您向面板动态添加组件时,您的 ActionListener 中需要的基本逻辑是:
panel.add(...);
panel.revalidate();
panel.repaint();
您需要调用 revalidate 方法来调用面板的布局管理器,以便重新计算首选尺寸。
编辑:
newL.add(listPane);
您不需要上述声明。一个 Swing 组件只能有一个父组件。该语句从滚动窗格中删除了面板,因此您看不到滚动条。
newL.revalidate();
你不需要那个声明。正如我在回答中所说,您只需要重新验证()您更改的面板。
此外,您不需要以下所有陈述:
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
即当您打包框架或使框架可见时调用布局管理器。您的代码应该是:
newl.pack();
newl.setVisible(true);
也就是说,您应该只在将所有组件添加到框架后才使框架可见。