how can u get a clear JButton to work on a gui. I am working on the same triangle program but two different ways. I got the exit but to work but cant see to get the clear button to work.
I have tried using: ClearBbtn.setText("); but is is not working properly. the word clear seem to disappear on the frame. I tried something else but is still not working. can someone help me or tell me what i did wrong.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AreaOfTriangle extends javax.swing.JFrame {
JTextField sideA = new JTextField("sideA");
JTextField sideB = new JTextField("sideB");
JTextField sideC = new JTextField("sideC");
JTextField area = new JTextField("area");
JButton CalculateB;
JButton ExitB;
JButton ClearB;
CalculateButtonHandler cbhandler;
ExitButtonHandler ebhandler;
ClearButtonHandler cbHandler;
public AreaOfTriangle() {
initComponents();
super.setTitle("Area of Triangle");
this.setLayout(null);
CalculateB = new JButton("Calculate");
cbhandler = new CalculateButtonHandler();
CalculateB.addActionListener(cbhandler);
ExitB = new JButton("Exit");
ebhandler = new ExitButtonHandler();
ExitB.addActionListener(ebhandler);
ClearB= new JButton("Clear");
cbHandler = new ClearButtonHandler();
ClearB.addActionListener(cbhandler);
Container pane = getContentPane();
pane.add(CalculateB);
pane.add(ExitB);
pane.add(ClearB);
pane.setSize(400, 400);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
sideAbtn = new javax.swing.JLabel();
sideblbl = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
sideajtf = new javax.swing.JTextField();
sidebjtf = new javax.swing.JTextField();
sidecjtf = new javax.swing.JTextField();
arealbl = new javax.swing.JLabel();
calareabtn = new javax.swing.JButton();
inputjtf = new javax.swing.JTextField();
ClearBbtn = new javax.swing.JButton();
anglelbl = new javax.swing.JLabel();
exitbtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
sideAbtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
sideAbtn.setText("Side A");
sideblbl.setFont(new java.awt.Font("Calibri", 3, 12)); // NOI18N
sideblbl.setText("Side B");
jLabel3.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
jLabel3.setText("Side C");
sideajtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
sidebjtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
sidecjtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
arealbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
arealbl.setText("The area of the triangle is:");
calareabtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
calareabtn.setText("Calculate ");
calareabtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calareabtnActionPerformed(evt);
}
});
inputjtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
ClearBbtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
ClearBbtn.setText("Clear");
ClearBbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearBbtnActionPerformed(evt);
}
});
anglelbl.setFont(new java.awt.Font("Cambria", 3, 18)); // NOI18N
anglelbl.setText("Area of Triangle");
exitbtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
exitbtn.setText("Exit");
exitbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitbtnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(46, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(anglelbl, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sideAbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sideblbl, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sidebjtf, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sideajtf, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sidecjtf, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(calareabtn, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(15, 15, 15))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(arealbl, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(inputjtf, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ClearBbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(exitbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(263, 263, 263))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(anglelbl)
.addGap(89, 89, 89)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideAbtn)
.addComponent(sideajtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(61, 61, 61)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideblbl)
.addComponent(sidebjtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(sidecjtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addComponent(calareabtn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(arealbl)
.addComponent(inputjtf, 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(ClearBbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(exitbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26))
);
pack();
}// </editor-fold>
private void exitbtnActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void ClearBbtnActionPerformed(java.awt.event.ActionEvent evt) {
}
private void calareabtnActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AreaOfTriangle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AreaOfTriangle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AreaOfTriangle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AreaOfTriangle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AreaOfTriangle().setVisible(true);
}
});
}
private static class ExitButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
private static class ClearButtonHandler {
public ClearButtonHandler() {
}
}
public class CalculateButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()== ClearB){
ClearBbtn.setText("");
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton ClearBbtn;
private javax.swing.JLabel anglelbl;
private javax.swing.JLabel arealbl;
private javax.swing.JButton calareabtn;
private javax.swing.JButton exitbtn;
private javax.swing.JTextField inputjtf;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel sideAbtn;
private javax.swing.JTextField sideajtf;
private javax.swing.JTextField sidebjtf;
private javax.swing.JLabel sideblbl;
private javax.swing.JTextField sidecjtf;
// End of variables declaration
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
