Domanda

Please could someone provide a little insight into my code. I am wanting to get the button on the balance tab to be aligned to the top left. I have tried different layout managers but all seem to result in the same or similar result of the button being in the center

public class DefaultView extends JFrame {

    public DefaultView() {    
        JButton SendBalInc = new JButton();
        SendBalInc.setText("Balance");    
        GridBagConstraints c = new GridBagConstraints();
        GridBagLayout gridbag = new GridBagLayout();
        c.fill = GridBagConstraints.BOTH;
        // Bal.fill = GridBagConstraints.NONE;    
        JPanel window = new JPanel();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);
        // this.setUndecorated(true);    
        window.setBackground(Color.WHITE);    
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBackground(Color.WHITE);    
        JComponent panel1 = makeTextPanel("Panel #1");
        tabbedPane.addTab("PIN ", panel1);    
        JPanel panel2 = new JPanel();
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());    
        box.add(SendBalInc);    
        panel2.setBackground(Color.WHITE);    
        panel2.add(box);    
        tabbedPane.addTab("Balance", panel2);    
        JComponent panel3 = makeTextPanel("Panel #3");
        tabbedPane.addTab("Dep", panel3);    
        JComponent panel4 = makeTextPanel("Panel #4");
        tabbedPane.addTab("Bill", panel4);    
        window.setLayout(new GridLayout(1, 2));
        window.add(tabbedPane);    
        this.add(window);    
    }

    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        return panel;
    }    
}
È stato utile?

Soluzione

I prefer GridBagLayout:

    final JPanel panel2 = new JPanel();
    panel2.setLayout(new GridBagLayout());
    panel2.setBackground(Color.WHITE);

    final GridBagConstraints cons = new GridBagConstraints();
    cons.weightx = 1D;
    cons.weighty = 1D;
    cons.anchor = GridBagConstraints.NORTHWEST;
    panel2.add(box, cons);

    tabbedPane.addTab("Balance", panel2);

note: box is not needed

Altri suggerimenti

You can simply use BoxLayout

JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBackground(Color.WHITE);
panel2.add(SendBalInc);

But I think that Betlista's answer with GridBag may be preferable, since you have many more options to tweak the layout further in the future.

Create the Box and add it directly to the JTabbedPane, rather than putting it in a panel first. And make the box a vertical box, so that you can control the alignment of its children by setting setAlignmentX on each child.

    Box box = Box.createVerticalBox();
    box.add(SendBalInc);
    tabbedPane.addTab("Balance", box);

You can add the following to alligh the button to the left:

panel2.setLayout(new FlowLayout(FlowLayout.LEFT));


You can find more in documentation http://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top