Mam projekt polegający na stworzeniu kalkulatora. Na początek dodałem przyciski itp, itd ale mam kilka problemów. Może na początek dam kod który mam:
package javaapplication3;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
jButton23 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jButton24 = new javax.swing.JButton();
jButton25 = new javax.swing.JButton();
jButton26 = new javax.swing.JButton();
jButton27 = new javax.swing.JButton();
jButton28 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kalkulator");
setResizable(false);
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("2");
jButton3.setText("3");
jButton4.setText("4");
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("6");
jButton7.setText("7");
jButton8.setText("8");
jButton9.setText("9");
jButton10.setText("0");
jButton11.setText(",");
jButton11.setMaximumSize(new java.awt.Dimension(39, 23));
jButton11.setMinimumSize(new java.awt.Dimension(39, 23));
jButton11.setPreferredSize(new java.awt.Dimension(39, 23));
jButton12.setText("+");
jButton12.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton13.setText("-");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton14.setText("*");
jButton15.setText("/");
jButton16.setText("=");
jButton16.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});
jButton17.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
jButton17.setText("1/x");
jButton17.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton17.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});
jButton18.setText("%");
jButton18.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton19.setFont(new java.awt.Font("Arial Black", 1, 11)); // NOI18N
jButton19.setText("←");
jButton19.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton20.setText("CE");
jButton20.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton21.setText("C");
jButton22.setText("±");
jButton22.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton23.setText("√");
jButton23.setMargin(new java.awt.Insets(1, 10, 1, 10));
jTextPane1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jTextPane1.setText("0");
jTextPane1.setToolTipText("");
jTextPane1.setAlignmentY(5.0F);
jTextPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jScrollPane1.setViewportView(jTextPane1);
jButton24.setText("MC");
jButton24.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton25.setText("MR");
jButton25.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton26.setText("MS");
jButton26.setMargin(new java.awt.Insets(1, 10, 1, 10));
jButton27.setText("M+");
jButton27.setMargin(new java.awt.Insets(0, 5, 0, 5));
jButton28.setText("M-");
jButton28.setMargin(new java.awt.Insets(1, 10, 1, 10));
jMenu1.setText("Widok");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edycja");
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setText("Kopiuj");
jMenu2.add(jMenuItem3);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setText("Wklej");
jMenu2.add(jMenuItem4);
jMenuBar1.add(jMenu2);
jMenu3.setText("Pomoc");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
jMenuItem1.setText("Wyświetl Pomoc");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem1);
jMenuItem2.setText("Kalkulator - Informacje");
jMenu3.add(jMenuItem2);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
[...] rozmiary przyciskow itp
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextPane1.setText("1");
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
private javax.swing.JButton jButton24;
private javax.swing.JButton jButton25;
private javax.swing.JButton jButton26;
private javax.swing.JButton jButton27;
private javax.swing.JButton jButton28;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JFrame jFrame1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration
}
Czy metoda setText będzie dobra do jTextPane1.setText("1"); aby przekazać wartość z poszczególnych klawiszy na jTextPane1 bez usuwania starych zawartości?
Jak najlepiej wykonać akcję dla znaku "=" aby wykrywało jakie działanie wykonuję? Dać kolejną zmienną dla każdego klawisza działania i w akcji dla "=" sprawdzać poszczególne ifami?
za wszelkie podpowiedzi i wskazówki dziękuję.
ok, problem 1 rozwiazany poprzez jLabel1.setText(jLabel1.getText()+"*"); itd, czekam na wskazówki, pozdrawiam