gpt4 book ai didi

java - 如何避免在已有文本的按钮上写入文本? java

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

我的项目中有一个方法,它从列表中随机选择一个按钮,然后在其上写入给定的文本。我遇到的问题是,由于它随机选择按钮,因此即使按钮上已经写有文本,它也会在按钮上写入文本。我想要的是,让这段代码选择一个按钮,然后检查上面是否已经写有文本,如果有,那么我希望它重复自身并选择另一个随机按钮,直到找到并写入给定的文本在之前没有文字的按钮上。

public class random{

public static String text2 = "text2";

public void static main(String[] args){
JButton button = new JButton("Empty");
JButton button1 = new JButton("Empty");
JButton button2 = new JButton("Empty");

randomButton(button,button1, button2);
}
public void randomButton(JButton button1, JButton button2, JButton button3){
String text = "text";
JButton[] arr = {button1, button2, button3};
Random r = new Random();
JButton b = arr[r.nextInt(arr.length)];
b.setText(text);
b.setEnabled(false);
}
}

按钮上可以包含三个文本之一 - Empty、text1 或 text2。

我想要做的是,如果在调用 randomButton 方法时随机选择按钮,我希望它检查所选按钮是否已经有 text1 或 text2,如果有,那么我希望它重新选择另一个随机按钮直到找到一个写有 Empty 的按钮,然后将其替换为 text1。

我尝试过 do while 循环、if 语句和 while 循环,但它不起作用,也许是因为我的逻辑错误,但我不确定。下面,我粘贴了我的尝试之一,但它不起作用。

do {
String text = "text";
JButton[] arr = {button1, button2, button3};
Random r = new Random();
JButton b = arr[r.nextInt(arr.length)];
if (b.getText() != "text" || b.getText() != "text2") {
b.setText(text);
b.setEnabled(false);
} else {
String text = "text";
JButton[] arr = {button1, button2, button3};
Random r = new Random();
JButton b = arr[r.nextInt(arr.length)];
b.setText(text);
b.setEnabled(false);
}
} while (b.getText() == text);

最佳答案

看看这个。有一个 JButton 的二维数组。当您单击随机按钮时,它会检查随机索引处的按钮是否有 X,如果没有,它将设置文本。如果有什么不明白的地方请告诉我

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class RandomButton {

JButton[][] buttons = new JButton[10][10];
JButton randomButton = new JButton("Choose Random Button");

public RandomButton() {
JPanel panel = new JPanel(new GridLayout(10, 10));
initButtons(panel);

randomButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

while (true) {
int[] indices = getRandom();
int i = indices[0];
int j = indices[1];
if (!"X".equals(buttons[i][j].getText())) {
buttons[i][j].setForeground(Color.BLUE);
buttons[i][j].setText("X");
break;
}
}
}
});

randomButton.setBorder(new LineBorder(Color.black, 5));
panel.setBorder(new LineBorder(Color.BLACK, 5));

JFrame frame = new JFrame("Random Button");
frame.add(panel);
frame.pack();
frame.add(randomButton, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public int[] getRandom() {
Random rand = new Random();
int i = rand.nextInt(10);
int j = rand.nextInt(10);
int[] indices = {i, j};
return indices;
}

private void initButtons(JPanel panel) {
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
buttons[i][j] = new JButton("O");
panel.add(buttons[i][j]);
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RandomButton();
}
});
}
}
<小时/>

更新

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class RandomButton {

JButton[][] buttons = new JButton[10][10];
JButton randomButton = new JButton("Choose Random Button");


public RandomButton() {
JPanel panel = new JPanel(new GridLayout(10, 10));
initButtons(panel);
panel.setBorder(new LineBorder(Color.BLACK, 5));

JFrame frame = new JFrame("Random Button");
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
System.out.println(button.getText());
}
}


private void initButtons(JPanel panel) {
ButtonListener listener = new ButtonListener();
int count = 1;
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
JButton button = new JButton(String.valueOf(count));
buttons[i][j] = button;
button.addActionListener(listener);
panel.add(button);
count++;
}
}
}

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

关于java - 如何避免在已有文本的按钮上写入文本? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21779331/

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