Avatar billede javanybegynder Nybegynder
30. november 2005 - 14:54 Der er 1 løsning

Problemer med placering af knapper i boxlayout

Vi har 6 knapper som skal centreres under en tabel. Knapperne skal stå i 2 rækker under og over hinanden.

På øverste række skal der være 2 knapper og på nederste 4.

Hvordan får  vi knapperne centreret?
Hvordan bestemmer man afstanden mellem knapperne horisontalt og vertikalt?

Her er vores kode...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;


public class GUIButtons extends JPanel {

    private static final long serialVersionUID = 1L;
    private GUICallback callback;
    private JButton book;
    private JButton exchange;
    private JButton takeShift;
    private JButton print;
    private JButton forward;
    private JButton backward;
   
   
    public GUIButtons(GUICallback callback){
        this.callback = callback;
        this.setLayout(new BorderLayout());
        this.add(getButtons(), BorderLayout.SOUTH);
    }


    private JPanel getButtons() {
       
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel();
        JPanel southPanel = new JPanel();
        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
        southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
       
       
        backward = new JButton("<<");
        backward.setPreferredSize(new Dimension(20,20));
        backward.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //callback.lastWeek();
            }
        });
        northPanel.add(backward, BorderLayout.CENTER);
       
        forward = new JButton(">>");
        forward.setPreferredSize(new Dimension(20,20));       
        forward.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //callback.nextWeek();
            }
        });
        northPanel.add(forward);

       
        book = new JButton("Book");
        book.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callback.bookShift();
            }
           
        });
        southPanel.add(book);
       
        exchange = new JButton("Byt");
        exchange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callback.exchangeShift();
            }
           
        });
        southPanel.add(exchange);
       
       
        takeShift = new JButton("Overtag");
        takeShift.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callback.takeShift();
            }
           
        });
        southPanel.add(takeShift);
   
        print = new JButton("Print");
        print.setEnabled(false);
       
        southPanel.add(print);
       
        mainPanel.add(northPanel, BorderLayout.NORTH);
        mainPanel.add(southPanel, BorderLayout.SOUTH);
        return mainPanel;
    }
   
}
Avatar billede jensgram Nybegynder
30. november 2005 - 23:48 #1
Et bud:

    private JPanel getButtons() {
       
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel();
        JPanel centerPanel = new JPanel();
        JPanel southPanel = new JPanel();
       
        centerPanel.setPreferredSize(new Dimension(10, 10));

        //northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
        //southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
       
       
        backward = new JButton("<<");
        backward.setPreferredSize(new Dimension(50,20));
        backward.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //callback.lastWeek();
            }
        });
        northPanel.add(backward);
       
        JPanel spacer = new JPanel();
        spacer.setPreferredSize(new Dimension(360, 5)); // Horisontalt mellemrum mellem navigationsknapperne.
        northPanel.add(spacer);
       
        forward = new JButton(">>");
        forward.setPreferredSize(new Dimension(50,20));
        forward.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //callback.nextWeek();
            }
        });
        northPanel.add(forward);

       
        book = new JButton("Book");
        book.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callback.bookShift();
            }
           
        });
        southPanel.add(book);
       
        exchange = new JButton("Byt");
        exchange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callback.exchangeShift();
            }
           
        });
        southPanel.add(exchange);
       
       
        takeShift = new JButton("Overtag");
        takeShift.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callback.takeShift();
            }
           
        });
        southPanel.add(takeShift);
   
        print = new JButton("Print");
        print.setEnabled(false);
       
        southPanel.add(print);
               
        mainPanel.add(northPanel, BorderLayout.NORTH);
        mainPanel.add(centerPanel, BorderLayout.CENTER); // En slags vertikal luft mellem navigations- og funktionsknapper
        mainPanel.add(southPanel, BorderLayout.SOUTH);
        return mainPanel;
    }
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester