📄 binaryjframe.java
字号:
//实验6.2
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JDialog;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
public class BinaryJFrame extends JFrame implements CaretListener
{
private JTextField text_decimal,text_binary,text_octal,text_hex;
private ShowmessageJDialog dialog;
public BinaryJFrame()
{
super("十进制整数转换");
this.setSize(200,140);
this.setResizable(false);
this.setBackground(java.awt.Color.lightGray);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
this.add(new JLabel(" 十进制"));
text_decimal = new JTextField("1023",10);
this.add(text_decimal);
text_decimal.addCaretListener(this);
this.add(new JLabel(" 二进制"));
text_binary = new JTextField(10);
text_binary.setEditable(false);
this.add(text_binary);
this.add(new JLabel(" 八进制"));
text_octal = new JTextField(10);
text_octal.setEditable(false);
this.add(text_octal);
this.add(new JLabel(" 十六进制"));
text_hex = new JTextField(10);
text_hex.setEditable(false);
this.add(text_hex);
this.display();
this.setVisible(true);
dialog = new ShowmessageJDialog(this);
}
private class ShowmessageJDialog extends JDialog
{
private JFrame frame;
private JLabel label_dialog;
public ShowmessageJDialog(JFrame frame)
{
super(frame,"提示",true);
this.frame = frame;
this.setSize(240,80);
label_dialog = new JLabel("",JLabel.CENTER);
this.add(label_dialog);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
}
public void showMessage(String message)
label_dialog.setText(message);
this.setLocation(frame.getX()+100,frame.getY()+100);
this.setVisible(true);
}
}
public void display()
{
try
{
int i = Integer.parseInt(text_decimal.getText());
text_binary.setText(Integer.toBinaryString(i));
text_octal.setText(Integer.toOctalString(i));
text_hex.setText(Integer.toHexString(i));
}
catch(NumberFormatException nfe)
{
dialog.showMessage("\""+text_decimal.getText()+"\" 不能转换成整数,请重新输入!");
}
finally{}
}
public void caretUpdate(CaretEvent e)
{
this.display();
}
public static void main(String arg[])
{
new BinaryJFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -