0

Scratching my head on the last part of this little project I have. It is a Binary/Decimal converter applet using JFrames/Jpanels. So the little niggle I am having is the applet needs to have an arrow (unicode) showing which direction you are converting and you change the direction of converting with a radio button. However, I can not get the initial arrow to change with the radio button. I have tried repaint(), and revalidate() in the radio button listener class and other little changes to the code with no luck. Here is what I have...

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);
        }
    }
}

}

So my issue, I think, is somewhere in my buildArrowPanel method and my radio button listener. Any help or ideas are appreciated, Thanks!

4

1 回答 1

1

您没有更新您的标签message1中的文本RadioButtonListener或替换为 new JPanel。也不需要打电话

arrowPanel.revalidate()

或者

arrowPanel.removeAll()  (!)

所以要修复你可以JLabel message1arrowPanel类成员变量上创建并简单地调用:

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);
于 2012-10-28T10:31:51.313 回答