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;
}
}