简体   繁体   English

我如何实现一个java GUI,它具有多行按钮彼此间隔开的多行按钮

[英]How can I implement a java GUI that has multiple rows of buttons spaced equally apart from one another

I'm trying to get 4 buttons on my top row, 4 buttons on the bottom row, and I need a button below that one- it's a voting application with each of the buttons representing a candidate. 我正在尝试在我的顶行上获得4个按钮,在底行上有4个按钮,我需要一个按钮,这是一个投票应用程序,每个按钮代表一个候选人。 initializeThings() is the only method called from the init method of this applet. initializeThings()是从这个applet的init方法调用的唯一方法。 The Person class is simply a container for 3 Strings representing the name, position the candidate's running for, and other information about them. Person类只是3个字符串的容器,代表名称,候选者的运行位置以及有关它们的其他信息。 My main problem is really just figuring out how to format it with the 4 buttons on the top row, 4 on the bottom, and the vote button below the bottom row. 我的主要问题是如何使用顶行的4个按钮,底部的4个按钮以及底行下方的投票按钮来确定如何格式化它。

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

Thanks for taking the time to read this/help 感谢您抽出宝贵时间阅读本文/帮助

You could take a look at TableLayoutManager, I'm not that experienced using Java myself, but at school I liked to use this. 您可以看一下TableLayoutManager,我不是自己使用Java经验丰富,但在学校我喜欢使用它。

http://code.google.com/p/table-layout/ http://code.google.com/p/table-layout/

also try to loop your persons, pics etc, to simplify your code 也尝试循环你的人,图片等,以简化你的代码

From NetBeans 来自NetBeans

在此输入图像描述

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Feb 21, 2013, 11:23:48 PM
 */
package javaapplication19;

/**
 *
 * @author yccheok
 */
public class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();

        setLayout(new java.awt.GridLayout(2, 4));

        jButton1.setText("jButton1");
        add(jButton1);

        jButton2.setText("jButton2");
        add(jButton2);

        jButton3.setText("jButton2");
        add(jButton3);

        jButton4.setText("jButton2");
        add(jButton4);

        jButton5.setText("jButton2");
        add(jButton5);

        jButton6.setText("jButton2");
        add(jButton6);

        jButton7.setText("jButton2");
        add(jButton7);

        jButton8.setText("jButton2");
        add(jButton8);
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    // End of variables declaration
}

我建议你看看GridBagLayout ,一旦你弄清楚如何使用它,你的用户界面将很容易做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM