2015-07-20 4 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'. Это макет поля по имени 'content', который находится внутри панели прокрутки, но эта область прокрутки нигде не добавляется. Остальные компоненты добавляются непосредственно в «CPanel», который по-прежнему использует «FlowLayout» по умолчанию. – kiheru

+0

Вы правы .. Спасибо за предложение –

ответ

1

кажется, что вы хотите CPanel иметь JScrollPane, который появляется как компонент с BoxLayout. Вы были правы в создании JPanel, установив его расположение и добавить его к JScrollPane, но вам все еще нужно добавить JScrollPane к вашим CPanel и кнопкам и demo к content

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

 Смежные вопросы

  • Нет связанных вопросов^_^