java - 使用单选按钮更新 JPanel

标签 java swing user-interface unicode jlabel

我对这个小项目的最后一部分感到摸不着头脑。它是一个使用 JFrames/Jpanels 的二进制/十进制转换器小程序。所以我遇到的小问题是小程序需要有一个箭头(unicode)来显示您正在转换的方向,并且您可以使用单选按钮更改转换方向。但是,我无法使用单选按钮更改初始箭头。我在单选按钮监听器类中尝试了 repaint() 和 revalidate() 以及对代码的其他一些小更改,但没有成功。这是我所拥有的...

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;


public class NumberConverter extends JApplet {
private JPanel decimalPanel;
private JPanel arrowPanel;
private JPanel binaryPanel;
private JPanel buttonPanel;
private JPanel convertPanel;
private TextField decimal;
private TextField binary;
private JRadioButton convertToBinary;
private JRadioButton convertToDecimal;
private ButtonGroup radioButtonGroup;
private JButton convertButton;
private Font myFont = new Font("Courier New", Font.BOLD, 15);
private Font arrowFont = new Font("Courier New", Font.BOLD, 25);
private Color colorAll = Color.red;
private String currentConversion = "toBinary";
private String currentArrow = "\u2193";

public void init(){

    setSize(400, 250);

    buildDpanel();
    buildArrowPanel();
    buildBpanel();
    buildButtonPanel();
    buildConvertPanel();

    setLayout(new GridLayout(5,1));

    add(decimalPanel);
    add(arrowPanel);
    add(binaryPanel);
    add(buttonPanel);
    add(convertPanel);

    decimal.setEditable(true);
    binary.setEditable(false);

    setVisible(true);
}

private void buildDpanel(){
    decimalPanel = new JPanel();
    decimalPanel.setFont(myFont);
    Label message1 = new Label("Decimal: ");
    decimal = new TextField(20);
    decimalPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    decimalPanel.setBackground(colorAll);

    decimalPanel.add(message1);
    decimalPanel.add(decimal);
}

private void buildArrowPanel(){
    arrowPanel = new JPanel();

    JLabel message1 = new JLabel(currentArrow);
    message1.setFont(arrowFont);
    arrowPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    arrowPanel.setBackground(colorAll);

    arrowPanel.add(message1);
}

private void buildBpanel(){
    binaryPanel = new JPanel();
    binaryPanel.setFont(myFont);
    Label message1 = new Label("Binary:");
    binary = new TextField(20);
    binaryPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    binaryPanel.setBackground(colorAll);

    binaryPanel.add(message1);
    binaryPanel.add(binary);
}

private void buildButtonPanel(){
    buttonPanel = new JPanel();
    buttonPanel.setFont(myFont);
    convertToBinary = new JRadioButton("Decimal to binary", true);
    convertToDecimal = new JRadioButton("Binary to decimal");
    buttonPanel.setBackground(colorAll);

    radioButtonGroup = new ButtonGroup();
    radioButtonGroup.add(convertToBinary);
    radioButtonGroup.add(convertToDecimal);

    convertToBinary.setFont(myFont);
    convertToDecimal.setFont(myFont);

    convertToBinary.addActionListener(new RadioButtonListener());
    convertToDecimal.addActionListener(new RadioButtonListener());

    buttonPanel.add(convertToBinary);
    buttonPanel.add(convertToDecimal);

}

private void buildConvertPanel(){
    convertPanel = new JPanel();
    convertPanel.setFont(myFont);
    convertButton = new JButton("Convert");
    convertButton.addActionListener(new ButtonListener());
    convertButton.setBackground(Color.cyan);

    convertButton.setFont(myFont);

    convertPanel.add(convertButton);
}

private class RadioButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == convertToBinary){
            decimal.setEditable(true);
            binary.setEditable(false);
            currentConversion = "toBinary";
            currentArrow = "\u2191";
            arrowPanel.removeAll();
            arrowPanel.revalidate();
        }if (e.getSource() == convertToDecimal){
            decimal.setEditable(false);
            binary.setEditable(true);
            currentConversion = "toDecimal";
            currentArrow = "\u2193";
            arrowPanel.removeAll();
            arrowPanel.revalidate();
        }
    }
}

private class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){


        if (currentConversion.equals("toBinary")){
            String binaryNum = "";
            String revString = "" ;
            int decimalNum;
            int quotient;
            int remainder;

            String deciStr = decimal.getText();

            decimalNum = Integer.parseInt(deciStr);

            for(int i = 0; i < 8; i++){

                quotient = decimalNum / 2;                  
                remainder = decimalNum % 2;             
                binaryNum = binaryNum + remainder;              
                decimalNum = quotient;

            }

            for(int i = binaryNum.length() -1 ; i >= 0 ; i--){
                revString = revString + binaryNum.charAt(i);
            } 


            binary.setText(revString);
        }else{
            int total = 0;
            String strTotal;

            String binStr = binary.getText();


            for(int i = 0; i <= binStr.length() - 1; i++){
                if(binStr.charAt(i) == '1'){
                    total += Math.pow(2,((binStr.length()-1)-i));

                }else if(binStr.charAt(i) == 0){

                }else{
                    strTotal = "Invalid character entered!";
                }
            }

            strTotal = total + "";

            decimal.setText(strTotal);
        }
    }
}

}

所以我认为我的问题出在我的 buildArrowPanel 方法和单选按钮监听器中的某个地方。感谢任何帮助或想法,谢谢!

最佳答案

您没有更新 RadioButtonListener 中标签 message1 中的文本或替换为新的 JPanel。也不需要调用

arrowPanel.revalidate()

arrowPanel.removeAll()  (!)

因此,要解决此问题,您可以将 arrowPanel 上的 JLabel message1 设为类成员变量,然后简单地调用:

message1.setText(currentArrow);

(您的向上和向下箭头 unicode 字符似乎是错误的:))

一些建议:

您有多个名为 message1 的标签,建议使用易于区分的名称,例如 arrowLabel

为了提高可读性,请考虑使用 String 常量作为箭头字符:

private static final String DOWN_ARROW = "\u2191";
private static final String UP_ARROW = "\u2193";

并调用:

arrowLabel.setText(UP_ARROW);

关于java - 使用单选按钮更新 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103871/

相关文章:

java - 无法在 GridLayout/FlowLayout 中正确设置 BoxLayout(JPanel)

java - 如何使用 gridControl 组件获取替代行颜色

c++ - 信号槽架构最佳实践

java - 如何使 jPanel 半透明?

java - 运行 JAR 文件时出错

java - 创建一个通用方法来组合多个 .close() 连接方法

java - 在 JPanel 中显示 JPanel 数组

java - 为什么 java 不将 int[] 自动装箱到 Integer[]

java - throttle 测试

ios - 更新用户界面 Swift