/*
Converter
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
/**
* 该程序对十进制、十六进制和二进制的整数进行相互转换。
*
* @author Chen
*/
public class Converter
{
/**
* 整数转换器类的一个对象,用它来实现整数进制间的转换。
*/
private NumberConverter converter;
/**
* 一个输入框,用于输入和显示十进制整数
*/
private JTextField decEditField;
/**
* 一个输入框,用于输入和显示十六进制整数
*/
private JTextField hexEditField;
/**
* 一个输入框,用于输入和显示二进制整数
*/
private JTextField binEditField;
/**
* 十进制输入框的监听器,用于监听该输入框值的改变;
* 若发生改变,则将改变后的数据进行转换,并在其余的两个文本框中显示。
*/
private DecTextChanged decListener;
/**
* 十六进制输入框的监听器,用于监听该输入框值的改变;
* 若发生改变,则将改变后的数据进行转换,并在其余的两个文本框中显示。
*/
private HexTextChanged hexListener;
/**
* 二进制输入框的监听器,用于监听该输入框值的改变;
* 若发生改变,则将改变后的数据进行转换,并在其余的两个文本框中显示。
*/
private BinaryTextChanged binListener;
public static void main(String[] args)
{
new Converter();
}
/**
* 构造一个图形的整数转换器。
*/
public Converter()
{
converter = new NumberConverter();
JFrame frame = new JFrame();
Container mainLayout = frame.getContentPane();
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
mainLayout.setLayout(gridBag);
Label tmpLable;
//(0,0)
cons.insets = new Insets(2,2,2,2);
cons.fill = GridBagConstraints.NONE;
cons.gridx = 0;
cons.gridy = 0;
cons.weightx = 0.0;
cons.weighty = 0.0;
cons.anchor = GridBagConstraints.EAST;
tmpLable = new Label("Decimal: ");
gridBag.setConstraints(tmpLable, cons);
mainLayout.add(tmpLable);
//(1,0)
cons.fill = GridBagConstraints.HORIZONTAL;
cons.gridx=1;
cons.gridwidth=2;
cons.weightx = 1.0;
decEditField = new JTextField(15);
decListener = new DecTextChanged();
decEditField.getDocument().addDocumentListener(decListener);
gridBag.setConstraints(decEditField, cons);
mainLayout.add(decEditField);
//(0,1)
cons.fill = GridBagConstraints.NONE;
cons.gridx=0;
cons.gridy=1;
cons.weightx = 0.0;
cons.gridwidth=1;
cons.anchor = GridBagConstraints.EAST;
tmpLable = new Label("hexadecimal: ");
gridBag.setConstraints(tmpLable, cons);
mainLayout.add(tmpLable);
//(1,1)
cons.fill = GridBagConstraints.HORIZONTAL;
cons.gridx=1;
cons.weightx = 1.0;
cons.gridwidth=2;
hexEditField = new JTextField(15);
hexListener = new HexTextChanged();
hexEditField.getDocument().addDocumentListener(hexListener);
gridBag.setConstraints(hexEditField, cons);
mainLayout.add(hexEditField);
//(0,2)
cons.fill = GridBagConstraints.NONE;
cons.gridx=0;
cons.gridy=2;
cons.weightx = 0.0;
cons.gridwidth=1;
cons.anchor = GridBagConstraints.EAST;
tmpLable = new Label("Binary: ");
gridBag.setConstraints(tmpLable, cons);
mainLayout.add(tmpLable);
//(1,2)
cons.fill = GridBagConstraints.HORIZONTAL;
cons.gridx=1;
cons.weightx = 1.0;
cons.gridwidth=2;
binEditField = new JTextField(15);
binListener = new BinaryTextChanged();
binEditField.getDocument().addDocumentListener(binListener);
gridBag.setConstraints(binEditField, cons);
mainLayout.add(binEditField);
cons.fill = GridBagConstraints.NONE;
cons.insets = new Insets(1, 1, 10, 10);
cons.gridx=2;
cons.gridy=3;
cons.gridwidth=1;
cons.gridheight=2;
cons.weighty=1.0;
cons.anchor = GridBagConstraints.SOUTHEAST;
cons.ipadx = 1;
JButton quitButton = new JButton("Quit");
gridBag.setConstraints(quitButton, cons);
mainLayout.add(quitButton);
quitButton.addActionListener(new QuitAction());
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = new Dimension();
frame.getSize(frameSize);
frame.setBounds(screenSize.width/3,screenSize.height/3,
frameSize.width,frameSize.height);
frame.setVisible(true);
}
/**
* Quit 按钮的监听器,当按下该按钮时,关闭窗口。
*/
class QuitAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
/**
* {@code decEditField} 的监听器类。在该程序中,当正在监听时,用的对象为
* {@code decListener}
*/
class DecTextChanged implements DocumentListener
{
/**
* 对文本框进行更新。即根据十进制文本框内容的改变,更改十六进制和二进制文本框的显示。
*/
private void update()
{
binEditField.getDocument().removeDocumentListener(binListener);
hexEditField.getDocument().removeDocumentListener(hexListener);
converter.setDecimal(decEditField.getText());
converter.decToOthers();
hexEditField.setText(converter.getHexacimal());
binEditField.setText(converter.getBinary());
binEditField.getDocument().addDocumentListener(binListener);
hexEditField.getDocument().addDocumentListener(hexListener);
}
/**
* 调用函数update()进行更新操作
*/
public void changedUpdate(DocumentEvent e)
{
update();
}
/**
* 调用函数update()进行更新操作
*/
public void insertUpdate(DocumentEvent e)
{
update();
}
/**
* 调用函数update()进行更新操作
*/
public void removeUpdate(DocumentEvent e)
{
update();
}
}
/**
* {@code binEditField} 的监听器类。在该程序中,当正在监听时,用的对象为
* {@code binListener}
*/
class BinaryTextChanged implements DocumentListener
{
/**
* 对文本框进行更新。即根据二进制文本框内容的改变,更改十进制和十六进制文本框的显示。
*/
private void update()
{
decEditField.getDocument().removeDocumentListener(decListener);
hexEditField.getDocument().removeDocumentListener(hexListener);
converter.setBinary(binEditField.getText());
converter.binToOthers();
hexEditField.setText(converter.getHexacimal());
decEditField.setText(converter.getDecimal());
decEditField.getDocument().addDocumentListener(decListener);
hexEditField.getDocument().addDocumentListener(hexListener);
}
/**
* 调用函数update()进行更新操作
*/
public void changedUpdate(DocumentEvent e)
{
update();
}
/**
* 调用函数update()进行更新操作
*/
public void insertUpdate(DocumentEvent e)
{
update();
}
/**
* 调用函数update()进行更新操作
*/
public void removeUpdate(DocumentEvent e)
{
update();
}
}
/**
* {@code hexEditField} 的监听器类。在该程序中,当正在监听时,用的对象为
* {@code hexListener}
*/
class HexTextChanged implements DocumentListener
{
/**
* 对文本框进行更新。即根据十六进制文本框内容的改变,更改十进制和二进制文本框的显示。
*/
private void update()
{
binEditField.getDocument().removeDocumentListener(binListener);
decEditField.getDocument().removeDocumentListener(decListener);
converter.setHeximal(hexEditField.getText());
converter.hexToOthers();
binEditField.setText(converter.getBinary());
decEditField.setText(converter.getDecimal())
- 1
- 2
前往页