En Netbeans se crea un JFrame y en modo diseño se añaden los siguientes componentes:
jButton_Calcular
jButton_Info
jButton_X
jTextField3 -> "------=------"
jTextField_a
jTextField_b
jTextField_c
jTextField_X
jTextField_Resultat
jPanels varios para agrupar y facilitar la inserción de los componentes en la GUID.
Código Java (ReglaTres.java):
package regla;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class ReglaTres extends javax.swing.JFrame {
DecimalFormat f = new DecimalFormat("#.00");
public ReglaTres() {
initComponents();
this.setLocationRelativeTo(null);
}
private void initComponents() { ... Código generado automaticamente NetBeans* ... }
private void jButton_CalcularActionPerformed(java.awt.event.ActionEvent evt) {
calcular();
}
private void jButton_XActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void jButton_InfoActionPerformed(java.awt.event.ActionEvent evt) {
String msg
= "\n Autor: Censor Cosmico\n"
+ " Versión: 2021 DIC\n"
+ " Fecha de creación: 04-12-2021\n"
+ " http://censorcosmico.blogspot.com.es/\n ";
JOptionPane.showMessageDialog(null, msg, "Info - Regla de Tres -", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ReglaTres().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton_Calcular;
private javax.swing.JButton jButton_Info;
private javax.swing.JButton jButton_X;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPanel jPanel7;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField_Resultat;
private javax.swing.JTextField jTextField_X;
private javax.swing.JTextField jTextField_a;
private javax.swing.JTextField jTextField_b;
private javax.swing.JTextField jTextField_c;
// End of variables declaration
private void calcular() {
float a = Float.parseFloat(jTextField_a.getText());
float b = Float.parseFloat(jTextField_b.getText());
float c = Float.parseFloat(jTextField_c.getText());
float x = b * c / a;
jTextField_Resultat.setText("" + f.format(x));
}
}
Resultado:
*Código generado por Netbeans en modo diseño:
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jPanel5 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jTextField_a = new javax.swing.JTextField();
jTextField_b = new javax.swing.JTextField();
jPanel3 = new javax.swing.JPanel();
jTextField_c = new javax.swing.JTextField();
jTextField_X = new javax.swing.JTextField();
jPanel4 = new javax.swing.JPanel();
jTextField3 = new javax.swing.JTextField();
jPanel6 = new javax.swing.JPanel();
jButton_Calcular = new javax.swing.JButton();
jTextField_Resultat = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jPanel7 = new javax.swing.JPanel();
jButton_Info = new javax.swing.JButton();
jButton_X = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setResizable(false);
jLabel1.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(0, 0, 204));
jLabel1.setText("Regla de Tres - v2021 -");
jPanel2.setLayout(new java.awt.GridLayout(1, 0));
jTextField_a.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jTextField_a.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField_a.setText("1");
jTextField_a.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.add(jTextField_a);
jTextField_b.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jTextField_b.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField_b.setText("1");
jTextField_b.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.add(jTextField_b);
jPanel3.setLayout(new java.awt.GridLayout(1, 0));
jTextField_c.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jTextField_c.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField_c.setText("100");
jTextField_c.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel3.add(jTextField_c);
jTextField_X.setEditable(false);
jTextField_X.setBackground(new java.awt.Color(255, 255, 204));
jTextField_X.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jTextField_X.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField_X.setText("X");
jTextField_X.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel3.add(jTextField_X);
jPanel4.setLayout(new java.awt.GridLayout(1, 0));
jTextField3.setEditable(false);
jTextField3.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jTextField3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField3.setText("------------ = ------------");
jTextField3.setBorder(null);
jPanel4.add(jTextField3);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel2, jPanel3});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPanel2, jPanel3});
jButton_Calcular.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jButton_Calcular.setText("Calcular");
jButton_Calcular.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_CalcularActionPerformed(evt);
}
});
jTextField_Resultat.setEditable(false);
jTextField_Resultat.setBackground(new java.awt.Color(255, 255, 204));
jTextField_Resultat.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
jTextField_Resultat.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField_Resultat.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel4.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 24)); // NOI18N
jLabel4.setText("Resultat (X):");
javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
jPanel6.setLayout(jPanel6Layout);
jPanel6Layout.setHorizontalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel6Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton_Calcular, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
.addComponent(jTextField_Resultat)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel6Layout.setVerticalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel6Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton_Calcular, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField_Resultat, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
jPanel5.setLayout(jPanel5Layout);
jPanel5Layout.setHorizontalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel5Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel5Layout.setVerticalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel5Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
jPanel7.setLayout(new java.awt.GridLayout());
jButton_Info.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
jButton_Info.setText("Info");
jButton_Info.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_InfoActionPerformed(evt);
}
});
jPanel7.add(jButton_Info);
jButton_X.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
jButton_X.setText("X");
jButton_X.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_XActionPerformed(evt);
}
});
jPanel7.add(jButton_X);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20))
.addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(56, Short.MAX_VALUE))
);
pack();
}// </editor-fold>