gpt4 book ai didi

java - 如何将二进制文件存储到数组中并将其放入不同的标签(编译程序)?

转载 作者:行者123 更新时间:2023-11-30 09:14:06 26 4
gpt4 key购买 nike

所以我有一个编译程序,但我有两个小问题。该程序将十六进制转换为十进制和二进制,并将其显示在这个可爱的小 GUI 中。

问题:

我想将二进制文件放入一个数组中,然后在连续的 5 个小框窗口之间传输。转换代码在那里,但我不知道如何拆分它。

这是我的司机:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class HBDconverter {


public static void main(String[] args) {
JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal #suchamaze");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new HBDconverterpanel());
frame.pack();
frame.setVisible(true);

}
}

这里是所有更有趣的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class HBDconverterpanel extends JPanel {


private JTextField hexString = new JTextField();
private JLabel decimalNumber = new JLabel();
private JLabel binaryNumber = new JLabel();


public HBDconverterpanel() {

JLabel programname = new JLabel("HBD Converter");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 150));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

hexString.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e) {
converttehInput();
}
});

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

JPanel binaryPanel = new JPanel(new GridLayout(1, 6));
binaryPanel.add(new JLabel("Binary"));
binaryPanel.add(binaryNumber);
add(binaryPanel, BorderLayout.CENTER);

JPanel decimalPanel = new JPanel(new GridLayout(1, 3));
decimalPanel.add(new JLabel("Decimal"));
decimalPanel.add(decimalNumber);
add(decimalPanel, BorderLayout.SOUTH);

}
//This take the integer Input (really hex) and converts for the GUIs above
private void converttehInput() {
//wow
{
Integer input = Integer.valueOf(hexString.getText(), 16);
decimalNumber.setText(String.valueOf(input));
binaryNumber.setText(Integer.toBinaryString(input));
}

}
}

最佳答案

似乎你拥有的比你预期的要多。
我猜你使用了一些代码生成器/GUI 生成器,
你没有检查它为你生成了什么。
您应该检查并清理不需要的内容。

          binaryLabel = new JLabel ("-");
binaryLabel2 = new JLabel("-");
binaryLabel3 = new JLabel("-");
binaryLabel4 = new JLabel("-");
binaryLabel5 = new JLabel("-");
binaryPanel.add(binaryLabel);
binaryPanel.add(binaryLabel2);
binaryPanel.add(binaryLabel3);
binaryPanel.add(binaryLabel4);
binaryPanel.add(binaryLabel5);
binaryPanel.add(binaryLabel5);

试试这个。

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class HBDconverterpanel extends JPanel {

private JTextField hexString = new JTextField();
private JLabel decimalLabel, binaryLabel;
private JLabel decimalNumber = new JLabel();
private JLabel binaryNumber = new JLabel();

public HBDconverterpanel() {

JLabel programname = new JLabel("HBD Converter");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 150));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

hexString.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
converttehInput();
}
});

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

JPanel binaryPanel = new JPanel(new GridLayout(1, 6));
binaryPanel.add(new JLabel("Binary"));
binaryLabel = new JLabel("-");
binaryPanel.add(binaryLabel);
binaryPanel.add(binaryNumber);
add(binaryPanel, BorderLayout.CENTER);

JPanel decimalPanel = new JPanel(new GridLayout(1, 3));
decimalPanel.add(new JLabel("Decimal"));
decimalLabel = new JLabel("-");
decimalPanel.add(decimalLabel);
decimalPanel.add(decimalNumber);
add(decimalPanel, BorderLayout.SOUTH);

}

// This take the integer Input (really hex) and converts for the GUIs above
private void converttehInput() {
// wow
{
Integer input = Integer.valueOf(hexString.getText(), 16);
decimalNumber.setText(String.valueOf(input));
binaryNumber.setText(Integer.toBinaryString(input));
}

}
}

你应该得到这个。

enter image description here

关于java - 如何将二进制文件存储到数组中并将其放入不同的标签(编译程序)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20669251/

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