我不知道如何将Jbutton返回到以前调用它的方法

问题描述 投票:0回答:1

您好,Java程序员,我目前正在从事宾果游戏。我创建了一个名为“ card();”的方法。创建一个JFrame和Jbuttons,在其中我试图调用一个具有JButton和String数组参数的方法,而我似乎无法弄清楚如何将Jbuttons返回到以前的方法card();。 。我对Java还是很陌生,对我来说是个菜鸟,所以对您的帮助非常感谢。谢谢!

public static void card(int[] b, int[] i, int[] n, int[] g, int[] o) { //Creates frame for card
            JFrame R = new JFrame("Bingo!");

            JButton reset = new JButton();
            reset.setText("Generate New  Card");
            reset.setBounds(195, 570, 190, 40);
            reset.setForeground(Color.WHITE);
            reset.setContentAreaFilled(false);
            reset.setFocusPainted(false);

            R.setSize(600, 680);
            R.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JLabel title = new JLabel(new ImageIcon("images/title.png"));
            JLabel star = new JLabel(new ImageIcon("images/star.png"));
            nice = new JLabel(new ImageIcon("images/bk2.png"));

            star.setBounds(250,325,75,75);
            title.setBounds(40, 60, 500, 114);
            nice.setBounds(0, 0, 600, 680);

            //Creating JButtons for Bingo card
            JButton B[] = new JButton[5];
            String b1[] = new String[5];
            JButton I[] = new JButton[5];
            String i1[] = new String[5];
            JButton N[] = new JButton[4];
            String n1[] = new String[4];
            JButton G[] = new JButton[5];
            String g1[] = new String[5];
            JButton O[] = new JButton[5];
            String o1[] = new String[5];

            Text(b1, i1, n1, g1, o1, B, I, N, G, O);

            B[0].setBounds(100, 175, 75, 75);
            B[1].setBounds(100, 250, 75, 75);
            B[2].setBounds(100, 325, 75, 75);
            B[3].setBounds(100, 400, 75, 75);
            B[4].setBounds(100, 475, 75, 75);

            I[0].setBounds(175, 175, 75, 75);
            I[1].setBounds(175, 250, 75, 75);
            I[2].setBounds(175, 325, 75, 75);
            I[3].setBounds(175, 400, 75, 75);
            I[4].setBounds(175, 475, 75, 75);

            N[0].setBounds(250, 175, 75, 75);
            N[1].setBounds(250, 250, 75, 75);
            N[2].setBounds(250, 400, 75, 75);
            N[3].setBounds(250, 475, 75, 75);

            G[0].setBounds(325, 175, 75, 75);
            G[1].setBounds(325, 250, 75, 75);
            G[2].setBounds(325, 325, 75, 75);
            G[3].setBounds(325, 400, 75, 75);
            G[4].setBounds(325, 475, 75, 75);

            O[0].setBounds(400, 175, 75, 75);
            O[1].setBounds(400, 250, 75, 75);
            O[2].setBounds(400, 325, 75, 75);
            O[3].setBounds(400, 400, 75, 75);
            O[4].setBounds(400, 475, 75, 75);

            for (int p = 0; p < b.length; p++) {
                try{
                    R.add(N[p]);
                }catch(Exception ignored){              
                }
                R.add(B[p]);
                R.add(I[p]);
                R.add(G[p]);
                R.add(O[p]);

            }

            R.add(title); //adds all of the components to the Frame...
            R.add(reset);
            R.add(star);
            R.add(nice);
            R.setLayout(null);
            R.setVisible(true);

            reset.addActionListener(new ActionListener() { //Action Listener when pressing "Generate Card"
                public void actionPerformed(ActionEvent e) {
                    R.dispose();
                    random();

                }

            });
        }

        public static JButton[] Text(String b1[], String i1[], String n1[], String g1[], String o1[], JButton B[], JButton I[], JButton N[], JButton G[], JButton O[] ){
             //For loops to setText to all of the buttons
            for (int p = 0; p < b.length; p++) {
                b1[p] = String.valueOf(b[p]);
                B[p] = new JButton();
                B[p].setText(b1[p]);
                B[p].setForeground(Color.BLACK);
                B[p].setContentAreaFilled(false);
                B[p].setFocusPainted(false);
                B[p].setFont(new Font("Comic Sans MS", Font.PLAIN, 26));

            }
            for (int p = 0; p < i.length; p++) {
                i1[p] = String.valueOf(i[p]);
                I[p] = new JButton();
                I[p].setText(i1[p]);
                I[p].setForeground(Color.BLACK);
                I[p].setContentAreaFilled(false);
                I[p].setFocusPainted(false);
                I[p].setFont(new Font("Comic Sans MS", Font.PLAIN, 26));

            }
            for (int p = 0; p < n.length; p++) {
                n1[p] = String.valueOf(n[p]);
                N[p] = new JButton();
                N[p].setText(n1[p]);
                N[p].setForeground(Color.BLACK);
                N[p].setContentAreaFilled(false);
                N[p].setFocusPainted(false);
                N[p].setFont(new Font("Comnc Sans MS", Font.PLAIN, 26));

            }
            for (int p = 0; p < g.length; p++) {
                g1[p] = String.valueOf(g[p]);
                G[p] = new JButton();
                G[p].setText(g1[p]);
                G[p].setForeground(Color.BLACK);
                G[p].setContentAreaFilled(false);
                G[p].setFocusPainted(false);
                G[p].setFont(new Font("Comic Sans MS", Font.PLAIN, 26));

            }
            for (int p = 0; p < o.length; p++) {
                o1[p] = String.valueOf(o[p]);
                O[p] = new JButton();
                O[p].setText(o1[p]);
                O[p].setForeground(Color.BLACK);
                O[p].setContentAreaFilled(false);
                O[p].setFocusPainted(false);
                O[p].setFont(new Font("Comic Sans MS", Font.PLAIN, 26));

            }

           return (B,I,N,G,O);
java arrays methods return jbutton
1个回答
0
投票

Text()是具有返回类型JButton[]的非空方法。如果要使用它在card()中返回的数组,则必须将其分配给变量。

您将Text(b1, i1, n1, g1, o1, B, I, N, G, O);替换为card()

JButton[] buttons = Text(b1, i1, n1, g1, o1, B, I, N, G, O);

B = buttons[0];
I = buttons[1];
N = buttons[2];
G = buttons[3];
O = buttons[4];

这里是有关Returning a Value from a Method的更多信息的教程>

尽管这可能有效,但我强烈建议您重新考虑程序的结构。您正在使用许多重复的代码。

© www.soinside.com 2019 - 2024. All rights reserved.