gpt4 book ai didi

java - 带单选按钮的二进制到十进制小程序

转载 作者:行者123 更新时间:2023-12-02 08:06:45 26 4
gpt4 key购买 nike

我正在尝试制作一个将二进制转换为十进制和十进制转换为二进制的小程序。我已经编写了每个单独的小程序,但现在我想制作一个使用单选按钮来选择用户想要执行的转换,然后让转换按钮执行该转换。我现在陷入困境,不太确定从这里该去哪里......它目前无法编译。

我还想包含一个向上或向下的箭头,具体取决于所选的单选按钮...我尝试将所述箭头的 Unicode 实现到 JLabel 中,但它们不接受字符,该怎么办关于这个?

非常感谢您的任何帮助。

这是我当前的困惑代码...

编辑:

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class binaryAndDecimalConvert extends JApplet
{
private JPanel bPanel;
private JPanel dPanel;
private JPanel buttonPanel;
private JPanel radioPanel;
private JPanel arrowPanel;

private JLabel arrowUp;
private JLabel arrowDown;

private JTextField binaryTxt;
private JTextField decimalTxt;

private ButtonGroup radioButtonGroup;
private JRadioButton binaryConvButton;
private JRadioButton decimalConvButton;

public void init()
{
Font font = new Font("display font", Font.BOLD, 15);



//build the panels
buildBpanel();
buildArrowPanel();
buildDpanel();
buildButtonPanel();
buildRadioPanel();



//create Layout Manager.
setLayout(new GridLayout(5, 1));

// Add the panels to the content pan.
add(bPanel);
add(arrowPanel);
add(dPanel);
add(buttonPanel);
add(radioPanel);
}


private void buildDpanel()
{
dPanel = new JPanel();
dPanel.setBackground(Color.pink);
JLabel message2 = new JLabel("Decimal Number: ");

decimalTxt = new JTextField(15);

dPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

dPanel.add(message2);
dPanel.add(decimalTxt);
}



private void buildBpanel()
{
//create the panel
bPanel = new JPanel();
bPanel.setBackground(Color.pink);
//create a label to display a mssage
JLabel message1 = new JLabel("Binary Number: ");

//create a text field for the binary number
binaryTxt = new JTextField(15);


//create a layout manager for the panel
bPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

///add the label and text field to the panel
bPanel.add(message1);
bPanel.add(binaryTxt);
}

public void buildRadioPanel()
{
radioPanel = new JPanel();
radioPanel.setBackground(Color.pink);

binaryConvButton = new JRadioButton("Binary to Decimal");
decimalConvButton = new JRadioButton("Decimal to Binary");

radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(binaryConvButton);
radioButtonGroup.add(decimalConvButton);

binaryConvButton.addActionListener(new RadioButtonListener());
decimalConvButton.addActionListener(new RadioButtonListener());

binaryConvButton.addActionListener(new RadioButtonListener());
decimalConvButton.addActionListener(new RadioButtonListener());

radioPanel.add(binaryConvButton);
radioPanel.add(decimalConvButton);

binaryConvButton.setEnabled(true);
}

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

arrowUp = new JLabel("\u2191");
arrowDown = new JLabel("\u2193");

arrowPanel.setBackground(Color.pink);

arrowPanel.add(arrowDown);

}

private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == binaryConvButton)
{
arrowPanel.add(arrowUp);
}
else if(e.getSource() == decimalConvButton)
arrowPanel.add(arrowDown);




}
}



private void buildButtonPanel()
{
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.pink);
JButton convButton = new JButton("Convert");

convButton.addActionListener(new ButtonListener());
buttonPanel.add(convButton);

}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//binary to decimal conversion
String decimalNum= "";
int decimal1 = 0;
String binaryNum = "";
int power = 1;
int dec;

if(e.getSource() == decimalConvButton)
{
binaryNum=binaryTxt.getText();
for(int i = 1; i <= binaryNum.length(); i++)
{
if(binaryNum.charAt(binaryNum.length()-i) == '1')
{
decimal1 = (decimal1 + power);
}
power = (power*2);
}
decimalNum = Integer.toString(decimal1);
decimalTxt.setText(decimalNum);
}

//decimal to binary
if(e.getSource() == binaryConvButton)
{
dec = Integer.parseInt(decimalTxt.getText());
while (dec != 0)
{
binaryNum = (dec % 2) + binaryNum;
dec /= 2;
}
binaryTxt.setText(binaryNum);

}
}


}
}

最佳答案

您遇到的一个问题是,您在方法内部重新声明了一个类字段,并有效地“隐藏”了该字段,使其不可见。该字段是“二进制”

这是您最初声明它的位置:

public class BinaryAndDecimalConvert extends JApplet {
private JPanel bPanel;

//...

private JTextField binary;

这是您对其进行阴影处理的位置:

private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String decimalNum = "";
int decimal1 = 0;
String binaryNum = "";
int power = 1;

String binary; // **** redeclared here ****

if (binaryToDec = true) {

binaryNum = binary.getText(); // so this won't work

解决方案:不要为方法的局部变量提供与重要类字段相同的名称。

接下来,您尝试对字符串变量、binaryNumber 调用 setText():

binaryNumber.setText(decimal1);

String没有这样的方法,所以去掉这个方法调用。

关于java - 带单选按钮的二进制到十进制小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8042101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com