Advertisement
Guest User

Combobox

a guest
Nov 18th, 2018
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.24 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * Combobox.java
  8.  *
  9.  * Created on 05.11.2018, 11:56:35
  10.  */
  11. package combobox;
  12. import java.text.DecimalFormat;
  13. import javax.swing.JOptionPane;
  14. /**
  15.  *
  16.  * @author B309U09
  17.  */
  18. public class Combobox extends javax.swing.JFrame {
  19.  
  20.     /** Creates new form Combobox */
  21.     public Combobox() {
  22.         initComponents();
  23.     varianteLabel.setVisible(false);
  24.     milchOptionCombo.setEnabled(false);
  25.     vorspeiseLabel.setVisible(false);
  26.     hauptgangLabel.setVisible(false);
  27.     nachspeiseLabel.setVisible(false);
  28.     endbetragLabel.setVisible(false);
  29.     extraLabel.setVisible(false);
  30.     }
  31.  
  32.     /** This method is called from within the constructor to
  33.      * initialize the form.
  34.      * WARNING: Do NOT modify this code. The content of this method is
  35.      * always regenerated by the Form Editor.
  36.      */
  37.     @SuppressWarnings("unchecked")
  38.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  39.     private void initComponents() {
  40.  
  41.         headerlabel = new javax.swing.JLabel();
  42.         vorLabel = new javax.swing.JLabel();
  43.         hauptLabel = new javax.swing.JLabel();
  44.         nachLabel = new javax.swing.JLabel();
  45.         vorCombo = new javax.swing.JComboBox();
  46.         hauptCombo = new javax.swing.JComboBox();
  47.         nachCombo = new javax.swing.JComboBox();
  48.         varianteLabel = new javax.swing.JLabel();
  49.         milchOptionCombo = new javax.swing.JComboBox();
  50.         vorspeiseLabel = new javax.swing.JLabel();
  51.         hauptgangLabel = new javax.swing.JLabel();
  52.         nachspeiseLabel = new javax.swing.JLabel();
  53.         extraLabel = new javax.swing.JLabel();
  54.         endbetragLabel = new javax.swing.JLabel();
  55.         jButton1 = new javax.swing.JButton();
  56.  
  57.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  58.  
  59.         headerlabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  60.         headerlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  61.         headerlabel.setText("Bitte wählen sie Ihr Menü");
  62.  
  63.         vorLabel.setText("Wähle eine Vorspeise:");
  64.  
  65.         hauptLabel.setText("Wähle einen Hauptgang:");
  66.  
  67.         nachLabel.setText("Wähle eine Nachspeise:");
  68.  
  69.         vorCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bitte auswählen", "Salat", "Suppe", "Bruscetta" }));
  70.         vorCombo.addActionListener(new java.awt.event.ActionListener() {
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  72.                 vorComboActionPerformed(evt);
  73.             }
  74.         });
  75.  
  76.         hauptCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bitte auswählen", "Pizza", "Sphagetti", "Döner" }));
  77.  
  78.         nachCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bitte auswählen", "Kirschquark", "Schokoeis", "Milchreis" }));
  79.         nachCombo.addActionListener(new java.awt.event.ActionListener() {
  80.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  81.                 nachComboActionPerformed(evt);
  82.             }
  83.         });
  84.  
  85.         varianteLabel.setForeground(new java.awt.Color(255, 0, 0));
  86.         varianteLabel.setText("Sie können eine Milchreisvariante wählen:");
  87.  
  88.         milchOptionCombo.setForeground(new java.awt.Color(255, 0, 0));
  89.         milchOptionCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Standard (+0,00€)", "Zimt (+0,20€)", "Schokolade (+0,20€)" }));
  90.         milchOptionCombo.addActionListener(new java.awt.event.ActionListener() {
  91.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  92.                 milchOptionComboActionPerformed(evt);
  93.             }
  94.         });
  95.  
  96.         vorspeiseLabel.setText("Vorspeise:XX€");
  97.         vorspeiseLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
  98.  
  99.         hauptgangLabel.setText("Hautpgang:XX€");
  100.         hauptgangLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
  101.  
  102.         nachspeiseLabel.setText("Nachspeise:XX€");
  103.         nachspeiseLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
  104.  
  105.         extraLabel.setForeground(new java.awt.Color(0, 0, 255));
  106.         extraLabel.setText("Zuschlag Milchreis: 0,20€");
  107.  
  108.         endbetragLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
  109.         endbetragLabel.setText("Gesammtbetrag:XX€");
  110.  
  111.         jButton1.setText("Berechne Preis");
  112.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  113.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  114.                 jButton1ActionPerformed(evt);
  115.             }
  116.         });
  117.  
  118.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  119.         getContentPane().setLayout(layout);
  120.         layout.setHorizontalGroup(
  121.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  122.             .addGroup(layout.createSequentialGroup()
  123.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.                     .addGroup(layout.createSequentialGroup()
  125.                         .addContainerGap()
  126.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127.                             .addComponent(headerlabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
  128.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  129.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130.                                     .addComponent(vorLabel)
  131.                                     .addComponent(hauptLabel)
  132.                                     .addComponent(nachLabel)
  133.                                     .addComponent(varianteLabel))
  134.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  135.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  136.                                     .addComponent(milchOptionCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  137.                                     .addComponent(vorCombo, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  138.                                     .addComponent(hauptCombo, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  139.                                     .addComponent(nachCombo, javax.swing.GroupLayout.Alignment.TRAILING, 0, 166, Short.MAX_VALUE)))))
  140.                     .addGroup(layout.createSequentialGroup()
  141.                         .addGap(99, 99, 99)
  142.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143.                             .addGroup(layout.createSequentialGroup()
  144.                                 .addGap(10, 10, 10)
  145.                                 .addComponent(extraLabel))
  146.                             .addComponent(nachspeiseLabel)
  147.                             .addComponent(endbetragLabel)
  148.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  149.                                 .addComponent(vorspeiseLabel)
  150.                                 .addComponent(hauptgangLabel)))
  151.                         .addGap(63, 63, 63)
  152.                         .addComponent(jButton1)))
  153.                 .addContainerGap())
  154.         );
  155.         layout.setVerticalGroup(
  156.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  157.             .addGroup(layout.createSequentialGroup()
  158.                 .addContainerGap()
  159.                 .addComponent(headerlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                 .addGap(18, 18, 18)
  161.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  162.                     .addComponent(vorLabel)
  163.                     .addComponent(vorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  164.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  165.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  166.                     .addComponent(hauptLabel)
  167.                     .addComponent(hauptCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  168.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  169.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  170.                     .addComponent(nachLabel)
  171.                     .addComponent(nachCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  172.                 .addGap(30, 30, 30)
  173.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  174.                     .addComponent(varianteLabel)
  175.                     .addComponent(milchOptionCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  176.                 .addGap(18, 18, 18)
  177.                 .addComponent(vorspeiseLabel)
  178.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  179.                 .addComponent(hauptgangLabel)
  180.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  181.                 .addComponent(nachspeiseLabel)
  182.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  183.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  184.                     .addGroup(layout.createSequentialGroup()
  185.                         .addComponent(extraLabel)
  186.                         .addGap(18, 18, 18)
  187.                         .addComponent(endbetragLabel))
  188.                     .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  189.                 .addContainerGap(17, Short.MAX_VALUE))
  190.         );
  191.  
  192.         pack();
  193.     }// </editor-fold>                        
  194.  
  195. private void milchOptionComboActionPerformed(java.awt.event.ActionEvent evt) {                                                
  196. // TODO add your handling code here:
  197. }                                                
  198.  
  199. private void vorComboActionPerformed(java.awt.event.ActionEvent evt) {                                        
  200. // TODO add your handling code here:
  201.  
  202. }                                        
  203.  
  204. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  205. DecimalFormat f = new DecimalFormat("#0.00");
  206. double [] preiseVorspeise={0.00,2.99,3.45,2.35};
  207. double [] preiseHauptspeise={0.00,3.50,3.95,3.55};
  208. double [] preisNachspeise={0.00,1.50,2.55,1.95};
  209. int auswahl1 = vorCombo.getSelectedIndex();
  210. int auswahl2 = hauptCombo.getSelectedIndex();
  211. int auswahl3 = nachCombo.getSelectedIndex();
  212. int auswahlmilch= milchOptionCombo.getSelectedIndex();
  213. int extra = milchOptionCombo.getSelectedIndex();
  214. double preisVorspeise = preiseVorspeise[auswahl1];
  215. vorspeiseLabel.setText("Vorspeise: "+f.format(preisVorspeise)+"€");
  216. vorspeiseLabel.setVisible(true);
  217. double preisHaupt = preiseHauptspeise[auswahl2];
  218. hauptgangLabel.setText("Vorspeise: "+f.format(preisHaupt)+"€");
  219. hauptgangLabel.setVisible(true);
  220. double preisNach = preisNachspeise[auswahl3];
  221. nachspeiseLabel.setText("Vorspeise: "+f.format(preisNach)+"€");
  222. nachspeiseLabel.setVisible(true);
  223. if(auswahlmilch==0){
  224. extraLabel.setVisible(false);
  225.     double ergebnis = preisVorspeise+preisHaupt+preisNach;
  226. endbetragLabel.setText("Gesammtbetrag: "+f.format(ergebnis)+"€");
  227. endbetragLabel.setVisible(true);
  228. }
  229. else{
  230. double ergebnis = preisVorspeise+preisHaupt+preisNach+0.20;
  231. JOptionPane.showMessageDialog(null,"Schoko/Zimt kostet 20 Cent Aufpreis!", "Hinweis", JOptionPane.CANCEL_OPTION);
  232. extraLabel.setVisible(true);
  233. endbetragLabel.setText("Gesammtbetrag: "+f.format(ergebnis)+"€");
  234. endbetragLabel.setVisible(true);
  235. }
  236. }                                        
  237.  
  238. private void nachComboActionPerformed(java.awt.event.ActionEvent evt) {                                          
  239. if (nachCombo.getSelectedIndex()==3){
  240.     milchOptionCombo.setEnabled(true);
  241.     varianteLabel.setVisible(true);
  242. }
  243. else {milchOptionCombo.setEnabled(false);
  244.     varianteLabel.setVisible(false);}
  245. }                                        
  246.  
  247.     /**
  248.      * @param args the command line arguments
  249.      */
  250.     public static void main(String args[]) {
  251.         /* Set the Nimbus look and feel */
  252.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  253.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  254.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  255.          */
  256.         try {
  257.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  258.                 if ("Nimbus".equals(info.getName())) {
  259.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  260.                     break;
  261.                 }
  262.             }
  263.         } catch (ClassNotFoundException ex) {
  264.             java.util.logging.Logger.getLogger(Combobox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  265.         } catch (InstantiationException ex) {
  266.             java.util.logging.Logger.getLogger(Combobox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  267.         } catch (IllegalAccessException ex) {
  268.             java.util.logging.Logger.getLogger(Combobox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  269.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  270.             java.util.logging.Logger.getLogger(Combobox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  271.         }
  272.         //</editor-fold>
  273.         /* Create and display the form */
  274.         java.awt.EventQueue.invokeLater(new Runnable() {
  275.  
  276.             public void run() {
  277.                 new Combobox().setVisible(true);
  278.             }
  279.         });
  280.     }
  281.     // Variables declaration - do not modify                    
  282.     private javax.swing.JLabel endbetragLabel;
  283.     private javax.swing.JLabel extraLabel;
  284.     private javax.swing.JComboBox hauptCombo;
  285.     private javax.swing.JLabel hauptLabel;
  286.     private javax.swing.JLabel hauptgangLabel;
  287.     private javax.swing.JLabel headerlabel;
  288.     private javax.swing.JButton jButton1;
  289.     private javax.swing.JComboBox milchOptionCombo;
  290.     private javax.swing.JComboBox nachCombo;
  291.     private javax.swing.JLabel nachLabel;
  292.     private javax.swing.JLabel nachspeiseLabel;
  293.     private javax.swing.JLabel varianteLabel;
  294.     private javax.swing.JComboBox vorCombo;
  295.     private javax.swing.JLabel vorLabel;
  296.     private javax.swing.JLabel vorspeiseLabel;
  297.     // End of variables declaration                  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement