2017-02-27 2 views
1

Existe-t-il un moyen de peindre un JToolBar ancré sur le reste des composants à partir d'un panneau existant?Comment peindre un JToolBar ancré sur le reste des composants à partir du panneau

Fondamentalement, je veux, lors de l'ancrage de la barre d'outils (à partir d'une position flottante), ne pas interférer avec mes autres composants et la disposition existante.

exemple simple, il suffit de commencer ..

public class ToolBarSample { 

public static void main(final String args[]) { 
    JFrame frame = new JFrame("JToolBar Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JToolBar toolbar = new JToolBar(); 

    toolbar.add(new JButton("button")); 
    toolbar.add(new JButton("button 2")); 

    Container contentPane = frame.getContentPane(); 
    contentPane.add(toolbar, BorderLayout.NORTH); 
    contentPane.add(new JLabel("I want this to be under the toolbar"), BorderLayout.CENTER); 

    // set the toolbar floating 
    ((BasicToolBarUI) toolbar.getUI()).setFloatingLocation(10, 10); 
    ((BasicToolBarUI) toolbar.getUI()).setFloating(true, null); 

    // TODO - after application starts, manually dock the toolbar to any position (north/east...) 

    frame.setSize(250, 100); 
    frame.setVisible(true); 
} 
} 

enter image description here

Répondre

3

Vous pouvez ajouter la barre d'outils directement à la JLayeredPane du JFrame.

Voici quelques documents utiles: How to Use Layered Panes

public static void main(final String args[]) { 
    JFrame frame = new JFrame("JToolBar Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JToolBar toolbar = new JToolBar(); 

    toolbar.add(new JButton("button")); 
    toolbar.add(new JButton("button 2")); 

    Container contentPane = frame.getContentPane(); 
    //contentPane.add(toolbar, BorderLayout.NORTH); 
    contentPane.add(new JLabel("I want this to be under the toolbar"), BorderLayout.CENTER); 

    JLayeredPane layeredPane = frame.getLayeredPane(); 
    layeredPane.setLayout(new BorderLayout()); 
    layeredPane.add(toolbar, BorderLayout.NORTH); 

    // set the toolbar floating 
    ((BasicToolBarUI) toolbar.getUI()).setFloatingLocation(10, 10); 
    ((BasicToolBarUI) toolbar.getUI()).setFloating(true, null); 

    // TODO - after application starts, manually dock the toolbar to any position (north/east...) 

    frame.setSize(250, 100); 
    frame.setVisible(true); 
}