import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class form2 extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
JButton b1,b2,b3,b4,b5,b6;
JPanel p1,p2,p3;
Font font;
Box basebox,box1,box2;
MainFrame main;
String s,s1,s2;
boolean bool1=false,bool2=false;
form2()
{
setBounds(100, 100, 680, 300);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
l1=new JLabel("机器数的真值还原(定点小数(默认8位))");
font = new Font("", Font.BOLD, 29);
l1.setFont(font);
l2=new JLabel("请输入机器数:");
l3=new JLabel("真值还原结果为:");
t1=new JTextField(20);
t2=new JTextField(20);
t1.setEditable(false);
t2.setEditable(false);
b1=new JButton("输入");
b2=new JButton("原-->真值");
b3=new JButton("反-->真值");
b4=new JButton("补-->真值");
b5=new JButton("移-->真值");
b6=new JButton("返回");
p1.add(l1,BorderLayout.CENTER);
box1=Box.createVerticalBox();
box1.add(l2);
box1.add(Box.createVerticalStrut(10));
box1.add(l3);
box1.add(Box.createVerticalStrut(10));
box2=Box.createVerticalBox();
box2.add(t1);
box2.add(Box.createVerticalStrut(10));
box2.add(t2);
box2.add(Box.createVerticalStrut(10));
basebox=Box.createHorizontalBox();
basebox.add(box1);
basebox.add(Box.createHorizontalStrut(12));
basebox.add(box2);
p2.add(basebox);
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
b5.setEnabled(false);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1){
b2.setEnabled(true);
b3.setEnabled(true);
b4.setEnabled(true);
b5.setEnabled(true);
t1.setEditable(true);
t2.setEditable(true);
}
else if (e.getSource() == b2)
{
yuan ();
t2.setText(s2);
/*if(bool1==true)
{
System.out.print("1");
}
else
{
JOptionPane.showMessageDialog(this, "输入有误!请重新输入!!","警告对话框",JOptionPane.WARNING_MESSAGE);
t1.setText(null);
t2.setText(null);
t3.setText(null);
}*/
}
else if (e.getSource() == b3)
{
fan();
t2.setText(s2);
}
else if (e.getSource() == b4)
{
bu();
t2.setText(s2);
}
else if (e.getSource() == b5)
{
yi();
t2.setText(s2);
}
else if (e.getSource() == b6)
{
setVisible(false);
}
}
/*public void jc()
{
char a[]= s.toCharArray();
char b[]=s1.toCharArray();
int i=0,j=0;
for(i=0;i<=s.length()-1;i++)
{
if(a[i]=='0'||a[i]=='1')
{
while(i==s.length()-1)
{
bool1=true;
}
}
else
{
/*JOptionPane.showMessageDialog(this, "输入有误!请重新输入!!","警告对话框",JOptionPane.WARNING_MESSAGE);
t1.setText(null);
t2.setText(null);
t3.setText(null);
break;
}
}
for(j=0;j<=s1.length()-1;j++)
{
if(b[j]=='0'||b[j]=='1')
{
while(j==s1.length()-1)
{
bool2=true;
}
}
else
{}
}
}
*/
public String yuan()
{
s2="";
s=t1.getText();
if(s.length()<=9)
{ if(s.charAt(0)=='0')
{
s1=s.substring(0,9);
s2="+"+s1;
}
else if(s.charAt(0)=='1')
{
s1=s.substring(1,9);
s2="-0"+s1;
}
}
else s2="超出字长范围";
return s2;
}
public String fan()
{
s=t1.getText();
if(s.length()<=9)
{
if(s.charAt(0)=='0')
{
s1=s.substring(0,9);
s2="+"+s1;
}
else if(s.charAt(0)=='1')
{ s2="";
s1=s.substring(2,9);
for(int i=0;i<7;i++)
{if(s1.charAt(i)=='0')
{
s2=s2+"1";
}
else if(s1.charAt(i)=='1')
{
s2=s2+"0";
}
}
s2="-0."+s2;
}
}else s2="超出字长范围";
return s2;
}
public String bu()
{
s=t1.getText();
if(s.length()<=9)
{
if(s.charAt(0)=='0')
{
s1=s.substring(0,9);
s2="+"+s1;
}
else if(s.charAt(0)=='1')
{
Double x=Double.parseDouble(s);
x=x-0.0000001;
s=String.valueOf(x);
s2="";
s1=s.substring(2,9);
for(int i=0;i<7;i++)
{if(s1.charAt(i)=='0')
{
s2=s2+"1";
}
else if(s1.charAt(i)=='1')
{
s2=s2+"0";
}
}
s2="-0."+s2;
while(s2.length()<10)
{
s2=s2+"0";
}
}}else s2="超出字长范围";
return s2;
}
public String yi()
{
s=t1.getText();
if(s.length()<=9)
{
if(s.charAt(0)=='1')
{
s1=s.substring(1,9);
s2="+0"+s1;
}
else if(s.charAt(0)=='0')
{
Double x=Double.parseDouble(s);
x=x+1;
x=x-0.0000001;
s=String.valueOf(x);
s2="";
s1=s.substring(2,9);
for(int i=0;i<7;i++)
{if(s1.charAt(i)=='0')
{
s2=s2+"1";
}
else if(s1.charAt(i)=='1')
{
s2=s2+"0";
}
}
s2="-0."+s2;
while(s2.length()<10)
{
s2=s2+"0";
}
}}else s2="超出字长范围";
return s2;
}
}