java - 嵌套 BoxLayout 不起作用?

标签 java swing layout layout-manager boxlayout

我创建了两个java类,其中一个是我的主类main.java,另一个是支持类,它创建一个带有一些添加组件CPanel.java的面板。我的应用程序的布局如下:
Main 类使用 BoxLayout 创建一个主面板,并将工具栏添加到顶部,然后创建一个 CPanel 对象(添加了组件的面板)并将其添加到主面板。 CPanel 对象还具有其组件的 BoxLayout。问题是,CPanel 面板没有合并 BoxLayout,而是只坚持流布局。这是我的类(class)的代码..

public class MainFile extends JFrame {
        private JToolBar navbar ;
        private JButton backBtn, forwardBtn, homeBtn ;
        private CPanel content ;
        private static JPanel app = new JPanel() ;
        public MainFile(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        navbar = new JToolBar() ;
        navbar.setMaximumSize(new Dimension(1000, 50));
        setSize(500, 800) ;
        app.setLayout(new BoxLayout(app, BoxLayout.PAGE_AXIS));
        app.add(navbar , BorderLayout.NORTH ) ;

        ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
        ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
        ImageIcon rightButtonIcon = createImageIcon("images/left.gif");

        backBtn = new JButton(leftButtonIcon) ;
        forwardBtn = new JButton(rightButtonIcon) ;
        homeBtn = new JButton(middleButtonIcon) ;
        navbar.add(forwardBtn) ;
        navbar.add(Box.createGlue());
        navbar.add(homeBtn) ;
        navbar.add(Box.createGlue());
        navbar.add(backBtn) ;

        content = new CPanel() ;
        app.add(content) ;
        setContentPane(app) ;

    }
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = MFrame.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    public static void showGUI(){
        MFrame Reader = new MFrame() ;
        //Reader.pack();
        //Reader.setContentPane(app);
        Reader.setVisible(true) ;
    }
    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                showGUI() ;
            }
        });
    }
}



这是我的 CPanel 类

public class CPanel extends JPanel {
    private JScrollPane scrollPane ;
    private JPanel content ;
    private JEditorPane demo ;
    public CPanel(){
        scrollPane = new JScrollPane() ;
        content = new JPanel() ;
        scrollPane.setViewportView(content);
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        try{
         java.net.URL text = CPanel.class.getResource("demo.txt") ;
        demo = new JEditorPane(text) ;
        }catch(Exception e){
            System.out.println("Something bad happened with the file") ;
        }
        add(demo) ;
        JButton demob = new JButton("Button 1") ;
        JButton demob2 = new JButton("Button 2") ;
        add(demob) ;
        add(demob2) ;
    }
}

最佳答案

您似乎希望 CPanel 有一个 JScrollPane,它作为 BoxLayout 的组件出现。您创建了 JPanel、设置其布局并将其添加到 JScrollPane 中是正确的,但您仍然需要将 JScrollPane 添加到您的 CPanel 以及按钮和 democontent

public class CPanel extends JPanel {
    private JScrollPane scrollPane ;
    private JPanel content ;
    private JEditorPane demo ;

    public CPanel(){

        scrollPane = new JScrollPane() ;
        content = new JPanel() ;
        scrollPane.setViewportView(content); 
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        try{
         java.net.URL text = CPanel.class.getResource("demo.txt") ;
        demo = new JEditorPane(text) ;
        }catch(Exception e){
            System.out.println("Something bad happened with the file") ;
        }

        //These need to be added to the contentpanel
        content.add(demo) ;
        JButton demob = new JButton("Button 1") ;
        JButton demob2 = new JButton("Button 2") ;
        content.add(demob) ;
        content.add(demob2) ;

        //Here we need to add the scrollPane, to which the JPanel 
        //with BoxLayout has been added
        this.setLayout(new BorderLayout());
        this.add(scrollPane, BorderLayout.CENTER);
    }
}

关于java - 嵌套 BoxLayout 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522853/

相关文章:

java - Excel 文件(xls) 的自定义InputFormat 或InputReader

Java Selenium - 查找下一个元素

java - 为什么我应该使用单独的线程在 JAVA 中显示 GUI

java - 使用 JFrame.pack() 时如何布局两个 JSplitPanes 并均匀放置分隔符

layout - 在 Vaadin 10 Flow 中替换 Vaadin 8 框架中的 `AbsoluteLayout`?

java - Maven资源插件复制文件

Java泛型方法签名解释

java - 更新标签时,EDT 无法使用递归方法正常工作

css - 如何将 html 布局分成三列,其中 div 可以跨越多个 1/3 宽度?

c# - 构建一个 c# 桌面应用程序,从头开始,请提示