抓住我这个小项目的最后一部分。它是使用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方法和单选按钮监听器。感谢任何帮助或想法,谢谢!
答案 0 :(得分:1)
您没有更新message1
标签RadioButtonListener
中的文字或更换新JPanel
。此外,无需致电
arrowPanel.revalidate()
或
arrowPanel.removeAll() (!)
因此,要解决此问题,您可以在JLabel
类成员变量上设置message1
arrowPanel
,然后只需调用:
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);