Estou iniciando em JAVA, fiz uma calculadora utilizando o Net Beans.
Peguei um tutorial passo à passo.
http://www.scribd.com/doc/260919/Tutorial-Netbeans-By-WallCk
Código classe Main:
[code]
package aplicacaoteste;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
new CalculadoraGUI().setVisible(true);
}
} [/code]
Códigos classe Calculadora:
package aplicacaoteste;
public class Calculadora{
public Calculadora(){
}
public int soma(int x, int y){
return x + y;
}
public int subtrai(int x, int y){
return x - y;
}
public int multiplica(int x, int y){
return x * y;
}
public int divide(int x, int y){
return y == 0 ? -1 : x / y;
}
}
Códigos class CalculadoraGUI:
package aplicacaoteste;
import javax.swing.JOptionPane;
/**
*
* @author Java
*/
public class CalculadoraGUI extends javax.swing.JFrame {
/** Creates new form CalculadoraGUI */
public CalculadoraGUI() {
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();
jLabelX = new javax.swing.JLabel();
jLabelY = new javax.swing.JLabel();
jTextFieldX = new javax.swing.JTextField();
jTextFieldY = new javax.swing.JTextField();
jButtonSoma = new javax.swing.JButton();
jButtonSubtrai = new javax.swing.JButton();
jButtonMultiplica = new javax.swing.JButton();
jButtonDivide = new javax.swing.JButton();
jMenuBarPrincipal = new javax.swing.JMenuBar();
jMenuPrincipal = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuSobre = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
jLabelX.setText("Valor de X:");
jLabelY.setText("Valor de Y:");
jTextFieldX.setText("0");
jTextFieldY.setText("0");
jButtonSoma.setText("+");
jButtonSoma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSomaActionPerformed(evt);
}
});
jButtonSubtrai.setText("-");
jButtonSubtrai.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSubtraiActionPerformed(evt);
}
});
jButtonMultiplica.setText("x");
jButtonMultiplica.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMultiplicaActionPerformed(evt);
}
});
jButtonDivide.setText("/");
jButtonDivide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDivideActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jLabelX)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextFieldX, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
.add(jPanel1Layout.createSequentialGroup()
.add(jLabelY)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jButtonSoma, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButtonMultiplica, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jButtonDivide, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButtonSubtrai, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)))
.add(jTextFieldY, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))))
.add(35, 35, 35))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabelX)
.add(jTextFieldX, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabelY)
.add(jTextFieldY, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButtonSoma)
.add(jButtonSubtrai))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButtonMultiplica)
.add(jButtonDivide))
.addContainerGap(12, Short.MAX_VALUE))
);
jMenuPrincipal.setText("Principal");
jMenuItem1.setText("Sair");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenuPrincipal.add(jMenuItem1);
jMenuBarPrincipal.add(jMenuPrincipal);
jMenuSobre.setText("Sobre");
jMenuSobre.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuSobreActionPerformed(evt);
}
});
jMenuItem2.setText("Tutorial");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenuSobre.add(jMenuItem2);
jMenuBarPrincipal.add(jMenuSobre);
setJMenuBar(jMenuBarPrincipal);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void jButtonSubtraiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Calculadora calc = new Calculadora();
int x = Integer.parseInt(this.jTextFieldX.getText());
int y = Integer.parseInt(this.jTextFieldY.getText());
int result = calc.subtrai(x, y);
JOptionPane.showMessageDialog(null, x + "-" + y + "=" + result);
this.jTextFieldX.setText("0");
this.jTextFieldY.setText("0");
}
private void jButtonSomaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Calculadora calc = new Calculadora();
int x = Integer.parseInt(this.jTextFieldX.getText());
int y = Integer.parseInt(this.jTextFieldY.getText());
int result = calc.soma(x, y);
JOptionPane.showMessageDialog(null, x + "+" + y + "=" + result);
this.jTextFieldX.setText("0");
this.jTextFieldY.setText("0");
}
private void jButtonMultiplicaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Calculadora calc = new Calculadora();
int x = Integer.parseInt(this.jTextFieldX.getText());
int y = Integer.parseInt(this.jTextFieldY.getText());
int result = calc.multiplica(x, y);
JOptionPane.showMessageDialog(null, x + "x" + y + "=" + result);
this.jTextFieldX.setText("0");
this.jTextFieldY.setText("0");
}
private void jButtonDivideActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Calculadora calc = new Calculadora();
int x = Integer.parseInt(this.jTextFieldX.getText());
int y = Integer.parseInt(this.jTextFieldY.getText());
int result = calc.divide(x, y);
JOptionPane.showMessageDialog(null, x + "/" + y + "=" + result);
this.jTextFieldX.setText("0");
this.jTextFieldY.setText("0");
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null,"Obrigado!");
System.exit(0);
}
private void jMenuSobreActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null,"Tutorial NetBeans");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CalculadoraGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonDivide;
private javax.swing.JButton jButtonMultiplica;
private javax.swing.JButton jButtonSoma;
private javax.swing.JButton jButtonSubtrai;
private javax.swing.JLabel jLabelX;
private javax.swing.JLabel jLabelY;
private javax.swing.JMenuBar jMenuBarPrincipal;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenu jMenuPrincipal;
private javax.swing.JMenu jMenuSobre;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextFieldX;
private javax.swing.JTextField jTextFieldY;
// End of variables declaration
}
Tutorial criado por WallCk!