如何在java中隐藏JButton上的文本?
我有一些按钮,上面有数字,以区分按钮,但我不希望这些数字是可见的。有没有办法把文字放在按钮上,然后把它隐藏起来或者让它看不见 这是我的密码:如何在java中隐藏JButton上的文本?,java,Java,我有一些按钮,上面有数字,以区分按钮,但我不希望这些数字是可见的。有没有办法把文字放在按钮上,然后把它隐藏起来或者让它看不见 这是我的密码: import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class S
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Start {
public static int a;
public static JButton[][] gumbi = new JButton[15][15];
public static void main(String[] args) {
JFrame okno = new JFrame("Nonogram");
okno.setVisible(true);
okno.setSize(800, 800);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
okno.add(panel);
JPanel polje = new JPanel(new GridLayout(15, 15));
panel.add(polje, BorderLayout.CENTER);
a = 0;
int b = 1;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (i < 5 && j < 5) {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.GREEN);
// gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
} else if (i < 5 || j < 5) {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.YELLOW);
// gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
gumbi[i][j].setEnabled(false);
} else {
if (Math.random() <= 0.6) {
a += 1;
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
// gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("3");
polje.add(gumbi[i][j]);
} else {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
// gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("4");
polje.add(gumbi[i][j]);
}
}
}
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
gumbi[i][j].addActionListener(new Listener(gumbi));
}
}
int [] array = new int[105];
for (int i = 5; i < 15; i++) {
for (int j = 5; j < 15; j++) {
int num = Integer.parseInt(gumbi[i][j].getText());
array [j + ((i-5)*10) - 5] = num;
}
}
int [] array2 = new int[105];
for(int i = 0; i < 100; i++){
array2[i] = -2;
}
array2[0] = -8;
for(int i = 0; i < 100; i++){
if(array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0){
b += 1;
} else if((array[i] == 3 && array[i] != array[i+1] && i < 99) || ((i + 1) % 10 == 0 && array[i] == 3)){
array2[i] = b;
b = 1;
}
if((i + 1) % 10 == 0){
b = 1;
}
}
int x = 0;
int y = 0;
for(int i = 1 ;i <= 100; i++){
//if(array2[(i-4) + (10*(j - 5))] != -2){
if (array2[i] != -2 && array[i] != 0){
gumbi[x + 5][y].setText("" + array2[i]);
y++;
}
if((i + 1) % 10 == 0){
x++;
y = 0;
}
}
for(int i = 0; i < 101; i++){
//System.out.println(array[i]);
if(array2[i] != -2)
System.out.print(array2[i] + " ");
}
for (int i = 5; i < 15; i++) {
for (int j = 5; j < 15; j++) {
int num = Integer.parseInt(gumbi[j][i].getText());
array [j + ((i-5)*10) - 5] = num;
}
}
for(int i = 0; i < 100; i++){
array2[i] = -2;
}
array2[0] = -8;
b = 1;
for(int i = 0; i < 100; i++){
if(array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0){
b += 1;
} else if((array[i] == 3 && array[i] != array[i+1] && i < 99) || ((i + 1) % 10 == 0 && array[i] == 3)){
array2[i] = b;
b = 1;
}
if((i + 1) % 10 == 0){
b = 1;
}
}
x = 0;
y = 0;
for(int i = 1 ;i <= 100; i++){
//if(array2[(i-4) + (10*(j - 5))] != -2){
if (array2[i] != -2 && array[i] != 0){
gumbi[y][x + 5].setText("" + array2[i]);
y++;
}
if((i + 1) % 10 == 0){
x++;
y = 0;
}
}
System.out.println();
for(int i = 0; i < 105; i++){
//System.out.println(array[i]);
if(array2[i] != -2)
System.out.print(array2[i] + " ");
}
}
}
谢谢您的时间。设置按钮文本以用于比较等不是很好的做法,您应该找到另一种方法;也许可以将您的
gumbi
对象更改为地图
如果您坚持使用setText
,您可以为背景和文本设置相同的颜色
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.WHITE);
这不是一个很好的做法,设置一个按钮的文本使用它进行比较等,你应该找到另一种方法来做到这一点;也许可以将您的
gumbi
对象更改为地图
如果您坚持使用setText
,您可以为背景和文本设置相同的颜色
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.WHITE);
我的理解是,您需要在按钮中存储一些数据,以便在
ActionListener
中区分它们。这有一个结构:动作命令。动作命令可以让你在一个按钮上放置任何文本(想想i18n,“Quit'-en,“Quitter”--fr),并将给定的语义与该按钮相关联(“QuitCommand”在你的应用程序中)
您可以随时将操作命令与按钮与
setActionCommand(String)
关联,并使用String getActionCommand()检索它
在你的监听器中。我的理解是,你需要在按钮中存储一些数据,以便在你的ActionListener
中区分它们。有一种构造:动作命令。动作命令可以让你在按钮上放置任何文本(想想i18n,“Quit'-en,“Quitter'-fr”)并将给定的语义与该按钮关联(应用程序中的“QuitCommand”)
您可以随时将操作命令与按钮与
setActionCommand(String)
关联,并在侦听器中使用String getActionCommand()
检索它。不要使用按钮文本来标识按下了哪个按钮,而是设置并使用按钮操作命令:
private static final String ACTION_COMMAND = "1";
...
JButton jbutton = new JButton();
jbutton.setActionCommand(ACTION_COMMAND);
...
if ( action.getActionCommand().equals(ACTION_COMMAND) )
不要使用按钮文本识别按下的按钮,而是设置并使用按钮操作命令:
private static final String ACTION_COMMAND = "1";
...
JButton jbutton = new JButton();
jbutton.setActionCommand(ACTION_COMMAND);
...
if ( action.getActionCommand().equals(ACTION_COMMAND) )
或者
gumbi[i][j].set前台(gumbi[i][j].getBackground())代码>或gumbi[i][j].set前台(gumbi[i][j].getBackground()代码>