gpt4 book ai didi

java - 我不知道如何将 Jbutton 返回到调用它的前一个方法

转载 作者:行者123 更新时间:2023-12-01 19:59:29 25 4
gpt4 key购买 nike

各位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);

最佳答案

Text() 是一个非 void 方法,返回类型为 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 的更多信息的教程

尽管这可能有效,但我强烈建议您重新考虑程序的结构。您正在处理大量重复的代码。

关于java - 我不知道如何将 Jbutton 返回到调用它的前一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59013374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com