添加十几个按钮后,并不是所有的JButton都出现.我究竟做错了什么? [英] Not all of my JButtons are showing up after I add a dozen of them. What am I doing wrong?

查看:56
本文介绍了添加十几个按钮后,并不是所有的JButton都出现.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JPanel中添加52个按钮.全部带有ActionListeners.当我达到一定数量(13)并运行该程序时,并不是所有的按钮都显示出来.例如,我添加了15个按钮,但仅显示9个或12个按钮.有时是全部,但并非每次都如此.

I want to add 52 buttons in a JPanel. All with ActionListeners. When I reach a certain amount (13) and run the program, not all of the buttons show up. For example I've added 15 buttons and only 9 or 12 of them show up. Sometimes all of them, but not every time.

这是两个JButton的代码:

Here's the code for two of the JButtons:

    JButton button_one=new JButton();
    button_one.setPreferredSize(new Dimension(100,150));
    mainpanel.add(button_one);
    button_one.addActionListener(new ActionListener(){
        
        @Override
        public void actionPerformed(ActionEvent button_1_picked){
            amount_of_cards_picked=amount_of_cards_picked+1;
            cardamountcheck();
            if(twocardspicked==true){
                userpick2=0;
                System.out.println(setoutcards[userpick2]);
                pairdetermination();
            }
            else if(twocardspicked==false){
                userpick1=0;
                System.out.println(setoutcards[userpick1]);
                
            }
        }
    });
    
    JButton button_two = new JButton();
    button_two.setPreferredSize(new Dimension(100, 150));
    mainpanel.add(button_two);
    button_two.addActionListener(new ActionListener(){
        
        @Override
        public void actionPerformed(ActionEvent button_2_picked){
            amount_of_cards_picked=amount_of_cards_picked+1;
            cardamountcheck();
            if(twocardspicked==true){
                userpick2=1;
                System.out.println(setoutcards[userpick2]);
                pairdetermination();
            }
            else if(twocardspicked==false){
                userpick1=1;
                System.out.println(setoutcards[userpick1]);
                
            }
        }
    });

基本上,当单击这些按钮之一时,我的代码中的变量将被更改. 这些按钮可以正常运行,并且可以按照我希望的方式正常工作,但是当按钮超过13个时,它们并不会全部显示出来,而我需要52个.

Basically when one of these buttons are clicked, variables in my code get changed. These buttons run fine and work exactly how I want them to, but they don't all appear when there is more than 13 of them, and I need 52.

推荐答案

我想在JPanel中添加52个按钮.全部带有ActionListeners.

I want to add 52 buttons in a JPanel. All with ActionListeners.

完成.我创建了一个显示52 JButtons的GUI.我将它们的尺寸缩小为OP的一半,因此GUI可以更好地解决问题.

Done. I created a GUI that displays 52 JButtons. I made them half the size of the OP's, so the GUI would fit better in the answer.

JPanel上使用GridLayout相当简单.

这是完整的可运行代码.一个最小的可复制示例.

Here's the complete runnable code. A minimal reproducible example.

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

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FiftyTwoJButtonGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new FiftyTwoJButtonGUI());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("52 JButton GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 13, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        CardListener listener = new CardListener();
        
        for (int i = 0; i < 52; i++) {
            JButton button = new JButton("" + (i + 1));
            button.addActionListener(listener);
            button.setPreferredSize(new Dimension(50, 75));
            panel.add(button);
        }
        
        return panel;
    }
    
    public class CardListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            int cardNumber = Integer.valueOf(button.getText());
            System.out.println(cardNumber);
        }
        
    }

}

这篇关于添加十几个按钮后,并不是所有的JButton都出现.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆