简体   繁体   English

将GUI值存储到数组中

[英]Storing GUI Values Into an Array

Given my converter program, how can I store the binary output into an array and then output it onto the screen? 给定我的转换器程序,如何将二进制输出存储到数组中,然后将其输出到屏幕上? Currently it just outputs the converted number but I need to store it into an array first. 目前,它仅输出转换后的数字,但我需要先将其存储到数组中。 I have not implemented arrays outside of console programs yet so I'm not entirely sure on how to attempt this yet. 我还没有在控制台程序之外实现数组,所以我还不确定如何进行此操作。

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodListener;

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

public class NumberConverter extends JPanel {
    private JLabel binaryLabel = new JLabel();
    private JLabel totalone = new JLabel();
    private JLabel totaltwo = new JLabel();
    private JLabel decimalLabel = new JLabel();
    private JTextField  hexdecString = new JTextField();
    private JButton convert;

    public NumberConverter() {

        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400, 300));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JLabel converterName = new JLabel("Hexadecimal Input");  

        convert = new JButton ("Convert");

        convert.addActionListener (new ButtonListener());
        add (convert);

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

        JPanel totalPanel = new JPanel(new GridLayout(2,3));
        totalPanel.add(new JLabel("Binary:"));
        totalone = new JLabel("---- ---- ---- ---- ----");
        totalPanel.add(totalone);
        totalPanel.add(binaryLabel);

        JPanel totalPanel2 = new JPanel(new GridLayout(2,3));
        totalPanel2.add(new JLabel("Decimal:"));
        totaltwo = new JLabel("------");
        totalPanel2.add(totaltwo);
        totalPanel2.add(decimalLabel);

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

    }        

    private class ButtonListener implements ActionListener {
        public void actionPerformed (ActionEvent event){       
            Integer n = Integer.valueOf(hexdecString.getText(), 16);
            decimalLabel.setText(String.valueOf(n));
            binaryLabel.setText(Integer.toBinaryString(n));     
        }
    }
}

Perhaps you want something like: 也许您想要这样的东西:

import java.util.Arrays;

...

Integer n = Integer.valueOf(hexdecString.getText(), 16);
decimalLabel.setText(String.valueOf(n));

// make an array of characters where each character 
// is a '1' or '0' corresponding to the binary representation of n
char[] charArray = Integer.toBinaryString(n).toCharArray(); 
binaryLabel.setText(Arrays.toString(charArray));

Which turns the binary string array into a binary char array. 它将二进制字符串数组转换为二进制char数组。 This is no better than what you had, mind you, only seems to correspond to what you're asking for. 请注意,这并不比您拥有的好,似乎仅与您要的内容相对应。

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

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