组织一个用于十六进制到二进制到十进制转换器的GUI? (JAVA)

时间:2012-12-17 01:37:12

标签: java user-interface layout converter

我正在尝试编写一个将十六进制转换为二进制到十进制的程序,并且最多可以转换为5位十六进制数。转换是一个完全不同的问题,现在我不得不组织GUI。接口的标准是它必须具有:一个窗口(文本框)用于输入十六进制数,一个窗口输出十进制等值,五个窗口排成一行输出二进制数。每个十六进制数字的二进制等值必须以数组排列,十六进制数字可以是数组的第二维,十进制数必须使用“通常算法”计算。我查看了示例和教程,我提出的是基于华氏温度到摄氏温度转换器。我到目前为止的代码是:

package finalConverter;

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

public class Converter extends JFrame {

    private JTextField textField;
    private JButton convert;
    private JLabel hexadecimal, binary, decimal; 

    public Converter() {
        setTitle("Converter");
        setSize(400,400);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().add(new ConverterPanel());
    }

    protected class ConverterPanel extends JPanel{
        public ConverterPanel(){
            setLayout(new GridLayout(3,5));
            textField = new JTextField();
            convert = new JButton ("Convert");
            hexadecimal = new JLabel("Hexadecimal");
            binary = new JLabel ("Binary");
            decimal = new JLabel ("Decimal");
            add(hexadecimal);
            add(textField);
            add(convert);
            add(binary);
            add(decimal);
        }
    }
    protected class EventHandler implements ActionListener{
        public void actionPerformed(ActionEvent ev){
            if(ev.getSource() == convert){
                double result = ()
            }
        }
    }
    public static void main(String[] args) {
        new Converter().setVisible(true);
    }
}

当我运行它时,它给了我一个有两列三行的窗口。这不是我想要的,修补newGridLayout中的数字似乎没有改变任何东西。我应该使用一些不同的布局来获得我想要的结果吗?或者是因为我缺少文本字段以及输出二进制数字的区域? 我尝试使用pack();在转换器窗口中:它只是通过三行GUI返回一个较小的两列。

1 个答案:

答案 0 :(得分:1)

您需要使用布局管理器,为您提供更大的灵活性......

enter image description here

对于,GridBagLayout,您可能还想考虑MigLayout或JGoodies FormLayout

public class TestConverter {

    public static void main(String[] args) {
        new TestConverter();
    }

    public TestConverter() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ConverterPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ConverterPane extends JPanel {

        private JTextField textField;
        private JButton convert;
        private JLabel[] binary;
        private JLabel decimal;

        public ConverterPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("Hexadecimal"), gbc);
            gbc.gridy++;
            add(new JLabel("Decimal"), gbc);
            gbc.gridy++;
            add(new JLabel("Binary"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 5;

            textField = new JTextField();
            textField.setHorizontalAlignment(JTextField.RIGHT);
            decimal = new JLabel("0");
            decimal.setHorizontalAlignment(JLabel.RIGHT);
            binary = new JLabel[]{
                new JLabel("00000000"),
                new JLabel("00000000"),
                new JLabel("00000000"),
                new JLabel("00000000"),
                new JLabel("00000000")
            };

            add(textField, gbc);
            gbc.gridy++;
            add(decimal, gbc);
            gbc.gridy++;
            gbc.fill = GridBagConstraints.NONE;
            gbc.gridwidth = 1;
            for (JLabel label : binary) {
                add(label, gbc);
                gbc.gridx++;
            }

            convert = new JButton("Convert");
            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.CENTER;
            add(convert, gbc);
        }
    }
}