2015-07-20 130 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