簡體   English   中英

添加十幾個 JButton 后,並不是所有的 JButton 都顯示出來。 我究竟做錯了什么?

[英]Not all of my JButtons are showing up after I add a dozen of them. What am I doing wrong?

我想在 JPanel 中添加 52 個按鈕。 全部帶有 ActionListeners。 當我達到一定數量(13)並運行程序時,並非所有按鈕都顯示出來。 例如,我添加了 15 個按鈕,但其中只有 9 或 12 個出現。 有時是全部,但不是每次。

這是兩個 JButton 的代碼:

    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 個。

我想在 JPanel 中添加 52 個按鈕。 全部帶有 ActionListeners。

完畢。 我創建了一個顯示 52 JButtons的 GUI。 我把它們做成了 OP 的一半大小,所以 GUI 更適合答案。

52 JButton 圖形用戶界面

JPanel上使用GridLayout相當簡單。

這是完整的可運行代碼。 一個最小的可重現示例。

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM