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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | /* * 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. } } |