我想要在頂行上獲得4個按鈕,在底行上獲得4個按鈕,並且我需要一個下面的按鈕 - 這是一個投票應用程序,每個按鈕代表一個候選人。 initializeThings()是從此applet的init方法調用的唯一方法。 Person類只是一個3字符串的容器,代表名稱,候選人的位置以及其他關於它們的信息。我的主要問題是隻想弄清楚如何使用最上面一行的4個按鈕,最下面的4個按鈕和最下面一行下面的投票按鈕進行格式化。我如何實現一個具有多行按鈕間隔相等的多個按鈕的GUI GUI
public class MainElectionWindow extends JApplet implements ActionListener, MouseListener{
private JPanel panel, topRow, bottomRow, voteBtnRow;
private JButton person1But,person2But,person3But,person4But,person5But,person6But,person7But,person8But,castVote;
private Person person1,person2,person3,person4,person5,person6,person7,person8;
private ImageIcon pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8;
private static final int HORIZONTAL_SPACING = 10;
private static final int VERTICAL_SPACING = 20;
private void initializeThings() {
// Initialize variables
topRow = new JPanel();
topRow.setLayout(new BoxLayout(topRow,BoxLayout.X_AXIS));
bottomRow = new JPanel();
bottomRow.setLayout(new BoxLayout(bottomRow,BoxLayout.X_AXIS));
voteBtnRow = new JPanel();
//Create people with names, position they're running for, and other info
person1 = new Person("Name 1", "Pos 1", "Other Info 1");
person2 = new Person("Name 2", "Pos 2", "Other Info 2");
person3 = new Person("Name 3", "Pos 3", "Other Info 3");
person4 = new Person("Name 4", "Pos 4", "Other Info 4");
person5 = new Person("Name 5", "Pos 5", "Other Info 5");
person6 = new Person("Name 6", "Pos 6", "Other Info 6");
person7 = new Person("Name 7", "Pos 7", "Other Info 7");
person8 = new Person("Name 8", "Pos 8", "Other Info 8");
pic1 = new ImageIcon("facebackground.png");
pic2 = new ImageIcon("facebackground.png");
pic3 = new ImageIcon("facebackground.png");
pic4 = new ImageIcon("facebackground.png");
pic5 = new ImageIcon("facebackground.png");
pic6 = new ImageIcon("facebackground.png");
pic7 = new ImageIcon("facebackground.png");
pic8 = new ImageIcon("facebackground.png");
// Get the content pane
Container container = this.getContentPane();
// Add panel to container
container.add(topRow);
container.add(bottomRow);
container.add(voteBtnRow);
person1But = new JButton(pic1);
person2But = new JButton(pic2);
person3But = new JButton(pic3);
person4But = new JButton(pic4);
person5But = new JButton(pic5);
person6But = new JButton(pic6);
person7But = new JButton(pic7);
person8But = new JButton(pic8);
castVote = new JButton("Vote");
// Add action listeners
person1But.addActionListener(this);
person2But.addActionListener(this);
person3But.addActionListener(this);
person4But.addActionListener(this);
person5But.addActionListener(this);
person6But.addActionListener(this);
person7But.addActionListener(this);
person8But.addActionListener(this);
castVote.addActionListener(this);
//Add Mouse Listeners
person1But.addMouseListener(this);
person2But.addMouseListener(this);
person3But.addMouseListener(this);
person4But.addMouseListener(this);
person5But.addMouseListener(this);
person6But.addMouseListener(this);
person7But.addMouseListener(this);
person8But.addMouseListener(this);
// Add components to the visual panel1
topRow.add(person1But);
topRow.add(person2But);
topRow.add(person3But);
topRow.add(person4But);
bottomRow.add(person5But);
bottomRow.add(person6But);
bottomRow.add(person7But);
bottomRow.add(person8But);
voteBtnRow.add(castVote);
// Make it visible
//person1But.requestFocusInWindow();
add(container);
setVisible(true);
}
感謝百忙之中閱讀本文時/幫助
網格佈局將使在面板4個等距項目,儘管你需要格式化按鈕的方式有點不同,因爲gridlayout往往會使按鈕填滿整個空間 – 2013-02-21 14:24:02
請**閱讀**輸入名稱時彈出標籤的有用說明。這對數據庫中的[tag:rows]有* nothing *。 – 2013-02-22 11:05:44