import javax.swing.JComboBox;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author K
*/
public class NewJFrame2 extends javax.swing.JFrame {
/**
* Creates new form NewJFrame2
*/
public NewJFrame2() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
convertB = new javax.swing.JLabel();
box1 = new javax.swing.JComboBox();
precB = new javax.swing.JLabel();
box2 = new javax.swing.JComboBox();
tf1 = new javax.swing.JTextField();
jSeparator1 = new javax.swing.JSeparator();
jLabel3 = new javax.swing.JLabel();
tf2 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("CS12MP2: IEEE754");
setPreferredSize(new java.awt.Dimension(350, 250));
jPanel1.setPreferredSize(new java.awt.Dimension(300, 300));
convertB.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
convertB.setText("Convert to:");
box1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
box1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DEC", "IEEE" }));
box1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
box1ActionPerformed(evt);
}
});
precB.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
precB.setText("Precision:");
box2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
box2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single", "Double", "L" }));
box2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
box2ActionPerformed(evt);
}
});
tf1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf1ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel3.setText("Output");
tf2.setEditable(false);
tf2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tf1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(convertB)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(box1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(16, 16, 16)
.addComponent(precB)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(box2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tf2))
.addGap(0, 82, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(convertB, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(box1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(precB)
.addComponent(box2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tf2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 140, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private String hex="";
private void box1ActionPerformed(java.awt.event.ActionEvent evt) {
javax.swing.JComboBox jb = ( javax.swing.JComboBox)evt.getSource();
Object selected = jb.getSelectedItem();
hex = tf1.getText();
if(selected.toString().equals("DEC")){
// tf2.setText(hex);
}
else if(selected.toString().equals("IEEE")){
// tf2.setText("G");
}
}
private void box2ActionPerformed(java.awt.event.ActionEvent evt) {
javax.swing.JComboBox jm = ( javax.swing.JComboBox)evt.getSource();
Object selected = jm.getSelectedItem();
if(selected.toString().equals("Single")){
// tf2.setText("Single");
}
else if(selected.toString().equals("Double")){
// tf2.setText("Double");
}
else if(selected.toString().equals("Double")){
// tf2.setText("Double");
}
}
private void tf1ActionPerformed(java.awt.event.ActionEvent evt) {
// String text = tf1.getText();
// tf1.selectAll();
//tf1.getDocument();
}
private void tf2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox box1;
private javax.swing.JComboBox box2;
private javax.swing.JLabel convertB;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JLabel precB;
private javax.swing.JTextField tf1;
private javax.swing.JTextField tf2;
// End of variables declaration
}
我应该在代码中添加什么内容,以便我可以实时访问TextField
中输入的内容并实时输出答案。