【问题标题】:Updating JPanel with radio button使用单选按钮更新 JPanel
【发布时间】:2012-10-27 20:23:26
【问题描述】:

我在这个小项目的最后一部分摸不着头脑。它是一个使用 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 方法和单选按钮侦听器中的某个地方。感谢您提供任何帮助或想法,谢谢!

【问题讨论】:

    标签: java swing user-interface unicode jlabel


    【解决方案1】:

    您没有更新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);
    

    【讨论】:

    • 成功了,非常感谢。我总是把事情复杂化哈哈。
    猜你喜欢
    • 2014-10-04
    • 2017-06-03
    • 1970-01-01
    • 2012-06-14
    • 2018-06-12
    • 2021-10-12
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多