Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - 如何在jframe中创建Jbuttons数组(how to create an array of Jbuttons in jframe)

Someone, please help.

(有人请帮忙。)

for some reason, I am unable to create an array or 2d array of jButtons and add it to a jPanel on a JFrame.I am using eclipse build-in application window in order to make the GUI.instead of an array of buttons only one button is shown on the jframe.

(由于某些原因,我无法创建jButton的数组或2d数组并将其添加到JFrame上的jPanel中。我正在使用eclipse内置应用程序窗口来制作GUI。而不是仅将一个按钮数组按钮显示在jframe上。)

this is my code:

(这是我的代码:)

public class mastermindprototype {

(公共类mastermindprototype {)

public JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mastermindprototype window = new mastermindprototype();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public mastermindprototype() throws Exception {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * 
 * @throws Exception
 */
private void initialize() throws Exception {
    frame = new JFrame("Mastermind");
    frame.setSize(1172, 770);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    stickers();

}

public void stickers() {
    JLabel lblNewLabel2 = new JLabel();
    JPanel panel = new JPanel();
    JPanel panel_1 = new JPanel();
    JLabel lblNewLabel = new JLabel();




    lblNewLabel2.setVisible(true);
    panel.setVisible(true);
    panel_1.setVisible(true);
    lblNewLabel.setVisible(true);

    panel.setBounds(0, 0, 1154, 723);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    panel_1.setBackground(Color.ORANGE);
    panel_1.setBounds(0, 0, 1154, 723);
    panel.add(panel_1);
    panel_1.setLayout(null);

    JButton gridButtons[] = new JButton[4];// 2d array of buttons
    for (int r = 0; r < gridButtons.length; r++) {

            // System.out.print(Grid[r][c]);
            gridButtons[r] = new JButton();
            gridButtons[r].setForeground(Color.blue);
            gridButtons[r].setBorder(new LineBorder(Color.blue));

            gridButtons[r].setSize(100, 100);
            panel_1.add(gridButtons[r]);
        }




}

}

(})

  ask by curious translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...