简体   繁体   English

如何在java中隐藏JButton上的文本?

[英]how to hide text on JButton in java?

I have some buttons with numbers on them to distinguish between the button but i dont want those numbers to be visible. 我有一些带有数字的按钮,以区分按钮,但我不希望这些数字可见。 Is there any way to have text on the buttons and just hiding it or making it invisible? 是否有任何方法可以在按钮上显示文本并隐藏它或使其不可见?

here is my code: 这是我的代码:

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] + " ");
        }
    }
}

and Action Listener: 和动作监听器:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class Listener implements ActionListener {
    JButton[][] gumbi = Start.gumbi;

    public Listener(JButton[][] gumbi) {
        this.gumbi = gumbi;

    }

    public void actionPerformed(ActionEvent e) {

        JButton gumb = (JButton) e.getSource();

        if (gumb.getBackground() == Color.WHITE) {
            gumb.setBackground(Color.BLACK);
        } else if (gumb.getBackground() == Color.BLACK) {
            gumb.setBackground(Color.WHITE);
        }
        if (gumb.getBackground() == Color.WHITE && gumb.getText() == "3") {
            Start.a += 1;
            gumbi[0][0].setText("" + Start.a);
        } else if (gumb.getBackground() == Color.BLACK && gumb.getText() == "3") {
            Start.a -= 1;
            gumbi[0][0].setText("" + Start.a);
        }
    }
}

Thanks for your time. 谢谢你的时间。

What I understand is that you need to store some data in your buttons to distinguish them in your ActionListener . 我的理解是你需要在按钮中存储一些数据,以便在ActionListener区分它们。 There is a construct for that: action commands . 有一个构造: 动作命令 An action command is something that lets you put any text on a button (think about i18n, "Quit'-en, "Quitter"-fr) and associate a given semantic to that button ("QuitCommand" inside your app). 动作命令允许您将任何文本放在按钮上(考虑i18n,“Quit'-en”,“Quitter”-fr)并将给定的语义与该按钮相关联(应用程序内部的“QuitCommand”)。

You can associate an action command to a button with setActionCommand(String) at any time and retrieve it with String getActionCommand() in your listener. 您可以随时将动作命令与具有setActionCommand(String)的按钮相关联,并在侦听器中使用String getActionCommand()检索它。

It's not a very good practice to set the text of a button to use it in comparisons etc, you should find another way to do that; 设置按钮文本以在比较中使用它不是一个很好的做法,你应该找到另一种方法来做到这一点; maybe change your gumbi object to a Map. 也许你的gumbi对象改为Map。

If you insist on using setText you could set the same color for the backgroud and the text. 如果您坚持使用setText ,则可以为背景和文本设置相同的颜色。

gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.WHITE);

Don't use the buttons text to identify which button was pressed, set and use the buttons action command instead: 不要使用按钮文本来识别按下了哪个按钮,而是设置并使用按钮动作命令:

private static final String ACTION_COMMAND = "1";

...

JButton jbutton = new JButton();
jbutton.setActionCommand(ACTION_COMMAND);

...

if ( action.getActionCommand().equals(ACTION_COMMAND) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM