Swing Program
public class SwingCalc extends javax.swing.JFrame {
public SwingCalc() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
inputTxt1 = new javax.swing.JTextField();
inputTxt2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
outputTxt = new javax.swing.JTextField();
sumbt = new javax.swing.JButton();
subbt = new javax.swing.JButton();
mulbt = new javax.swing.JButton();
divbt = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Input Value 1");
jLabel2.setText("Input Value 2");
inputTxt1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
inputTxt1ActionPerformed(evt);
}
});
jLabel3.setText("Output Value");
sumbt.setText("SUM");
sumbt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sumbtActionPerformed(evt);
}
});
subbt.setText("SUB");
subbt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subbtActionPerformed(evt);
}
});
mulbt.setText("MUL");
mulbt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mulbtActionPerformed(evt);
}
});
divbt.setText("DIV");
divbt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divbtActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(55, 55, 55)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(inputTxt1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
.addComponent(inputTxt2)
.addComponent(outputTxt)))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(sumbt, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(subbt, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(mulbt, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(divbt, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(inputTxt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(inputTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(outputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sumbt)
.addComponent(subbt)
.addComponent(mulbt)
.addComponent(divbt))
.addContainerGap(63, Short.MAX_VALUE))
);
pack();
}
private void sumbtActionPerformed(java.awt.event.ActionEvent evt) {
int val1 = Integer.parseInt(inputTxt1.getText());
int val2 = Integer.parseInt(inputTxt2.getText());
int val3 = val1 + val2;
outputTxt.setText(Integer.toString(val3));
}
private void subbtActionPerformed(java.awt.event.ActionEvent evt) {
int val1 = Integer.parseInt(inputTxt1.getText());
int val2 = Integer.parseInt(inputTxt2.getText());
int val3 = val1 - val2;
outputTxt.setText(Integer.toString(val3));
}
private void mulbtActionPerformed(java.awt.event.ActionEvent evt) {
int val1 = Integer.parseInt(inputTxt1.getText());
int val2 = Integer.parseInt(inputTxt2.getText());
int val3 = val1 * val2;
outputTxt.setText(Integer.toString(val3));
}
private void divbtActionPerformed(java.awt.event.ActionEvent evt) {
int val1 = Integer.parseInt(inputTxt1.getText());
int val2 = Integer.parseInt(inputTxt2.getText());
int val3 = val1 / val2;
outputTxt.setText(Integer.toString(val3));
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SwingCalc().setVisible(true);
}
});
}
private javax.swing.JButton divbt;
private javax.swing.JTextField inputTxt1;
private javax.swing.JTextField inputTxt2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton mulbt;
private javax.swing.JTextField outputTxt;
private javax.swing.JButton subbt;
private javax.swing.JButton sumbt;
}
Output
sorry sir but mah swing application not work properly????
ReplyDeletehow can i solve it....
Tell me what kind of difficulty you are face ?
Delete