在我這個小項目的最後部分抓住我的頭。它是一個使用JFrames/Jpanels的二進制/十進制轉換器小程序。所以,我所擁有的小黑鬼是小程序需要有一個箭頭(unicode)來顯示你正在轉換的方向,並且你改變了單選按鈕的轉換方向。但是,我無法使用單選按鈕獲取初始箭頭。我已經在單選按鈕監聽器類中嘗試了repaint()和revalidate(),並且沒有運氣的情況下對代碼進行了其他小改動。以下是我有...用單選按鈕更新JPanel
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方法和我的單選按鈕監聽的地方。任何幫助或想法表示讚賞,謝謝!
這樣做,謝謝一堆。我總是讓事情過度複雜。 – Devigili