Java - 如何通过按下另一个 JButton 来隐藏 JButton?

Java - How to hide JButton by pressing another JButton?

我需要写一个简单的程序:

  1. 三个按钮 GEN - 生成一个数字 HEX - 将我的号码计算为十六进制数 DEC - 将我的数字计算为十进制数

  2. JLabel 它显示数字。

DEC 按钮应该在我的数字是 DECIMAL 时隐藏,HEX 按钮应该在我的数字是 HEXADECIMAL 时隐藏。

当我生成例如第三个数字时,它必须生成十六进制数字,而前一个生成的数字也是十六进制的。

这是我已经完成的:

界面

import java.awt.GridBagLayout;
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.JLabel;

public class Okno {

    private JFrame okno;
    private JButton DecButt, HexButt, GenerujButt;
    private JLabel label_num;



    public JButton getDecButt() {
        return DecButt;
    }
    public void setDecButt(JButton decButt) {
        DecButt = decButt;
    }
    public JButton getHexButt() {
        return HexButt;
    }
    public JLabel getLabel_num() {
        return label_num;
    }



    public void createGUI() {
        okno = new JFrame("Okienko");
        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        okno.setSize(400, 300);
        okno.setLayout(new GridBagLayout());


        //rzytowanie liczby na String zeby wyswietlic
        label_num = new JLabel();
        okno.add(label_num);

        //przyciski
        DecButt = new JButton("DEC");
        okno.add(DecButt);
        HexButt = new JButton("HEX");
        okno.add(HexButt);
        GenerujButt = new JButton("Generuj");
        okno.add(GenerujButt);


        ButtonAction przycisk;
        przycisk = new ButtonAction();

        //sluchacze do przyciskow
        DecButt.addActionListener(przycisk);
        HexButt.addActionListener(przycisk);
        GenerujButt.addActionListener(przycisk);


        okno.setVisible(true);

    }//createGUI

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName());

        javax.swing.SwingUtilities.invokeLater(new Runnable() {//klasa anonimowa - bo chcemy jej uzyc tylko raz

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                Okno application = new Okno();
                    application.createGUI();
            }
        });//klasa anonimowa

    }


}//class

ActionListeners+生成数字代码和HEX、DEC计算器

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream.GetField;
import java.util.Random;

import javax.swing.JLabel;



public class ButtonAction extends Okno implements ActionListener{

    private int dec;
    private String hex;

    public void setDec(int dec) {
        this.dec = dec;
    }
    public int getDec() {
        return dec;
    }

    public void setHex(String hex) {
        this.hex = hex;
    }
    public String getHex() {
        return hex;
    }

    public int generateNum() {

        Random r = new Random();
        int number = r.nextInt(1000);
        return number;
    }

    public static int hex2decimal(String decimal) {
        String digits = "0123456789ABCDEF";
        decimal = decimal.toUpperCase();
        int val = 0;
        for (int i = 0; i < decimal.length(); i++) {
            char c = decimal.charAt(i);
            int d = digits.indexOf(c);
            val = 16*val + d;
        }
        return val;
    }
    // precondition:  d is a nonnegative integer
    public static String decimal2hex(int d) {
        String digits = "0123456789ABCDEF";
        if (d == 0) return "0";
        String hex = "";
        while (d > 0) {
            int digit = d % 16;
            hex = digits.charAt(digit) + hex;
            d = d / 16;
        }
        return hex;
    }





    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getActionCommand() == "DEC")
        {
            setDec(hex2decimal(getHex()));
            System.out.println(getDec());
        }
        else if(arg0.getActionCommand() == "HEX")
        {
            setHex(decimal2hex(getDec()));
            System.out.println(getHex());
        }
        else if(arg0.getActionCommand() == "Generuj")
        {
            setDec(generateNum());
            System.out.println(getDec());
            //getDecButt().setVisible(false);**********HERE is NullPointerExceprion
        }

    }//actionperformed

}//class

我在 ActionPerformed 中有 nullpointerexception。谁能告诉我为什么?

当您按下按钮时,在 ActionListener 中,使用 buttonObject.setEnabled(false)

告诉按钮被禁用

你的 ActionListener 目前是如何设置的,解决它的最简单方法是将 JButton 暴露给包的其余部分(虽然,这并不是真的......推荐) .

但是,您始终可以使用这个和匿名 class 在 GUI class 中以编程方式添加 ActionListener(因为这样做可以使 JButton 保持私有):

buttonName.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        buttonName.setEnabled(false);
        // Whatever other stuff you want to do.
    }
});

要处理显示,只需创建一个 private 方法,您可以在每个 ActionListener 匿名 classes 中调用该方法。

DEC button should hide when my number is DECIMAL and HEX button should hide when my number is HEXADECIMAL.

我不确定我知道你为什么要打扰。相反,您可以将按钮添加到 ButtonGroup 中,这样一次只能有一个按钮 "selected",与 JToggleButtonJRadioButton 组合使用时,您还会得到关于选择哪个按钮的视觉提示

有关详细信息,请参阅 How to Use the ButtonGroup Component and How to Use Buttons, Check Boxes, and Radio Buttons