2015-07-20 90 views
0

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

嵌套BoxLayout不工作?

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) ; 
    } 
} 
+1

CPanel'的'布局不'BoxLayout'。它是按名称“内容”的字段布局,它位于滚动窗格内,但不会在任何位置添加滚动窗格。其他组件直接添加到'CPanel',它仍然使用默认的'FlowLayout'。 – kiheru

+0

你是对的..谢谢你的建议 –

回答

1

看来你想要的CPanelJScrollPane,这作为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); 
    } 
} 
+0

你的代码工作完美。感谢你的回答。 –

+0

很好听,快乐编码! – milez