简体   繁体   English

十六进制到二进制到十进制GUI

[英]Hexadecimal to binary to decimal GUI

I'm trying to make a GUI for this. 我正在为此做一个GUI。 Basically you press a button and you convert the hexadecimal to a binary and decimal. 基本上,您按下一个按钮,然后将十六进制转换为二进制和十进制。 I feel like I'm getting close with my codes. 我觉得我正在接近我的代码。 I created a drive class and a panel class so far. 到目前为止,我已经创建了驱动器类和面板类。 I'm trying to create methods in my panel class that will convert hexadecimal to binary and a hexadecimal to a decimal. 我正在尝试在面板类中创建将十六进制转换为二进制并将十六进制转换为十进制的方法。 I keep getting errors though, and I'm not sure why. 我一直收到错误消息,但不确定为什么。 I've been following my other codes, but I've been struggling getting these methods at the bottom of the panel class. 我一直在遵循其他代码,但是我一直在努力将这些方法放在面板类的底部。 I'll show you my classes so far. 到目前为止,我将向您展示我的课程。

Driver class: 驱动类别:

import javax.swing.JFrame;


public class DriverConverter {

    //--------------------------------------------------------
    // GUI for Converter
    //--------------------------------------------------------
    public static void main(String[] args) {
    JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal.");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    frame.getContentPane().add(new ConverterPanel());
    frame.pack();
    frame.setVisible(true);

    }
}

Here's my panel class: 这是我的小组课:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ConverterPanel extends JPanel {
    private JLabel[] binary;
    private JLabel decimal;
    private JLabel inputLabel, outputLabel, resultLabel, totalLabel, totalLabel2;
    private JButton BinaryConvert, DecimalConvert;
    private JTextField hexString;
    private Font convertFont;
    private double total;


    //-------------------------------------------------------------
    // Main GUI
    //-------------------------------------------------------------

    public ConverterPanel() {

        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400,300));

        hexString =  new JTextField();
        //hexadecimal.addActionListener(new ConvertListener());
        add(hexString);


        JLabel converterName = new JLabel ("Hexadecimal Converter");
        JPanel panelName = new JPanel();
        panelName.add(converterName);
        add(panelName, BorderLayout.NORTH);

        JPanel totalPanel = new JPanel();
        totalPanel.add(new JLabel ("Binary"));
        totalLabel = new JLabel ("------");
        totalPanel.add(totalLabel);
        add(totalPanel, BorderLayout.SOUTH);

        JPanel totalPanel2 = new JPanel();
        totalPanel2.add(new JLabel ("Decimal"));
        totalLabel2 = new JLabel ("------");
        totalPanel2.add(totalLabel);
        add(totalPanel2, BorderLayout.EAST);


    }

    // -------------------------------------------------------------
    // Equation  for Binary Conversion
    //--------------------------------------------------------------

    public void binaryConversion (double binary){

        try {
             Integer b = Integer.valueOf(hexString,16);
             Integer.toBinaryString(b);
           } catch (NumberFormatException ee) {
             ee.printStackTrace();
           }

    }

    // Equation for decimal conversion.

    public void decimalConversion (double decimal){

        String decimal = decimal.getText();
         try {
             Integer c = Integer.valueOf(hexString,16);
             Integer.parseInt(hexString, 16);
           } catch (NumberFormatException ee) {
             ee.printStackTrace();
           }



        //Integer c = Integer.valueOf("444", 16);
        //Integer.parseInt("444",16);
    }



}

I've been experimenting with these methods at the bottom. 我一直在底部尝试这些方法。 I've been trying to create them without any errors. 我一直在尝试创建它们而没有任何错误。 I also tried using the getText() method for the JTextField. 我还尝试对JTextField使用getText()方法。 I feel like I could be close with this, but there's something "off" with my logic of creating these methods for converting hexadecimal to binary, and binary to decimal. 我觉得我可能对此很了解,但是我创建这些将十六进制转换为二进制以及将二进制转换为十进制的方法的逻辑有些“偏离”。 I actually know the code in the stardard editor. 我实际上知道stardard编辑器中的代码。 It makes sense to me in an editor because all you do is system.out.println the result. 在编辑器中,这对我来说很有意义,因为您要做的只是将system.out.println作为结果。 It's getting it translated for a GUI that's causing some issues. 它正在翻译成导致某些问题的GUI。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I realize this post it long. 我意识到这篇文章很久了。 I'm just not sure where my error is. 我只是不确定我的错误在哪里。 It could be at the beginning of the panel class, or just at the end, so I thought posting the whole code would help with identifying the problem. 它可能在面板类的开始处,也可能在结尾处,所以我认为发布整个代码将有助于识别问题。 I'd appreciate any guidance you can give me. 您可以给我任何指导,我将不胜感激。 And am I at least on the right track? 而且我至少在正确的轨道上吗? I've been trying hard to get this. 我一直在努力做到这一点。 Thanks. 谢谢。

EDIT: 编辑:

Ok, I just ran it in JGrasp, and these are the errors I'm getting. 好的,我只是在JGrasp中运行它,而这正是我遇到的错误。 It runs in Eclipse tho. 它在Eclipse中运行。

Exception in thread "main" java.lang.NoClassDefFoundError: ConverterPanel
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
    at java.lang.Class.getMethod0(Class.java:2774)
    at java.lang.Class.getMethod(Class.java:1663)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: ConverterPanel
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 6 more

For the compiler errors; 对于编译器错误; Your method binaryConversion has an argument double decimal which will hide the JLabel called decimal. 您的方法binaryConversion具有一个double decimal binaryConversion double decimal参数,它将隐藏称为十进制的JLabel This means you're actually calling getText() on a double . 这意味着您实际上是在double上调用getText() Also you have defined the String to be called decimal, which further confuses the compiler. 另外,您已将字符串定义为十进制,这进一步使编译器感到困惑。 Try this instead: 尝试以下方法:

public void decimalConversion(double d) {

    String s = decimal.getText();
    try {
        Integer c = Integer.valueOf(s);
    } catch (NumberFormatException ee) {
        ee.printStackTrace();
    }
}

And in decimalConversion Integer.valueOf(hexString,16); 并在decimalConversion Integer.valueOf(hexString,16); should be Integer.valueOf(hexString.getText(),16); 应该是Integer.valueOf(hexString.getText(),16);


Here's one way of how you can do it. 这是一种方法。 This way you can enter text and press enter and it will be translated to decimal and binary and shown on the labels: 这样,您可以输入文本并按Enter键,它将转换为十进制和二进制并显示在标签上:

public class ConverterPanel extends JPanel {
    private JLabel binaryLabel = new JLabel();
    private JLabel decimalLabel = new JLabel();
    private JTextField  hexString = new JTextField();
    // omitted some variables

    public ConverterPanel() {

        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400, 300));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JLabel converterName = new JLabel("Hexadecimal Converter");
        hexString.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                convertInput();
            }
        });

        JPanel panelName = new JPanel(new GridLayout(1,2));
        panelName.add(converterName);
        panelName.add(hexString);
        add(panelName, BorderLayout.NORTH);


        JPanel totalPanel = new JPanel(new GridLayout(1,3));
        totalPanel.add(new JLabel("Binary"));
        totalLabel = new JLabel("------");
        totalPanel.add(totalLabel);
        totalPanel.add(binaryLabel);

        JPanel totalPanel2 = new JPanel(new GridLayout(1,3));
        totalPanel2.add(new JLabel("Decimal"));
        totalLabel2 = new JLabel("------");
        totalPanel2.add(totalLabel2);
        totalPanel2.add(decimalLabel);

        JPanel south = new JPanel(new GridLayout(2,1));
        south.add(totalPanel);
        south.add(totalPanel2);
        add(south, BorderLayout.SOUTH);

    }        

    private void convertInput() {

        try {
            Integer n = Integer.valueOf(hexString.getText(), 16);
            decimalLabel.setText(String.valueOf(n));
            binaryLabel.setText(Integer.toBinaryString(n));
        } catch (NumberFormatException ee) {
            ee.printStackTrace();
        }
    }
}

Here's an example of converting the text from hex to int and binary: 这是将文本从十六进制转换为int和二进制的示例:

public class Test {
    public static void main(String[] args) {
        String string = "A3";
        int parseInt = Integer.parseInt(string, 16);
        System.out.println(parseInt);
        System.out.println(Integer.toBinaryString(parseInt));
    }
}

Output: 输出:

163
10100011

Hello I found several errors in your code 您好,我在您的代码中发现了几个错误

1.- 1.-

First, you are trying to convert a Textfield when it should be a String, you must first obtain the value of Textfield with the method .getText() 首先,当您尝试将Textfield转换为String时,必须首先使用.getText()方法获取Textfield的值。

public void binaryConversion (double binary){

        try {
             Integer b = Integer.valueOf(hexString.getText(),16);
             Integer.toBinaryString(b);
           } catch (NumberFormatException ee) {
             ee.printStackTrace();
           }

    }

2.Second, you are trying to assign a value to a variable that has been declared as a parameter of the method, so you should change the name of the variable of method. 2.其次,您尝试为已声明为方法参数的变量赋值,因此应更改方法变量的名称。

public void decimalConversion(double decimal) {

        String decimal2 = String.valueOf(decimal);
        try {
            Integer c = Integer.valueOf(hexString.getText(), 16);
            Integer.parseInt(hexString.getText(), 16);
        } catch (NumberFormatException ee) {
            System.out.println(ee.getMessage());
        } 
    }

And check point 1 also in this method 并且也用这种方法检查点1

3.- 3 .-

Third item, not found the event that causes the conversion, you should add a button for each conversion and assign a actionPerformed event. 第三项,未找到导致转换的事件,应为每个转换添加一个按钮,并分配一个actionPerformed事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM