Advertisement
Guest User

GUIFrame

a guest
Jan 14th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 KB | Software | 0 0
  1. public class GUIFrame extends javax.swing.JFrame {
  2.     public GUIFrame() {
  3.         initComponents();
  4.     }
  5.     @SuppressWarnings("unchecked")
  6.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  7.     private void initComponents() {
  8.  
  9.         Label = new javax.swing.JLabel();
  10.         TextField = new javax.swing.JTextField();
  11.         ComboBox = new javax.swing.JComboBox<>();
  12.         Button = new javax.swing.JButton();
  13.  
  14.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  15.  
  16.         Label.setText("Label");
  17.  
  18.         ComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4" }));
  19.         ComboBox.addItemListener(new java.awt.event.ItemListener() {
  20.             public void itemStateChanged(java.awt.event.ItemEvent evt) {
  21.                 ComboBoxItemStateChanged(evt);
  22.             }
  23.         });
  24.  
  25.         Button.setText("OK");
  26.         Button.addActionListener(new java.awt.event.ActionListener() {
  27.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  28.                 ButtonActionPerformed(evt);
  29.             }
  30.         });
  31.  
  32.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  33.         getContentPane().setLayout(layout);
  34.         layout.setHorizontalGroup(
  35.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  36.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  37.                 .addContainerGap()
  38.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  39.                     .addComponent(ComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  40.                     .addComponent(TextField, javax.swing.GroupLayout.Alignment.TRAILING)
  41.                     .addComponent(Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  42.                 .addContainerGap())
  43.             .addGroup(layout.createSequentialGroup()
  44.                 .addGap(163, 163, 163)
  45.                 .addComponent(Button)
  46.                 .addContainerGap(165, Short.MAX_VALUE))
  47.         );
  48.         layout.setVerticalGroup(
  49.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  50.             .addGroup(layout.createSequentialGroup()
  51.                 .addContainerGap()
  52.                 .addComponent(Label)
  53.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  54.                 .addComponent(TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  55.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  56.                 .addComponent(ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  57.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 193, Short.MAX_VALUE)
  58.                 .addComponent(Button)
  59.                 .addContainerGap())
  60.         );
  61.  
  62.         pack();
  63.     }// </editor-fold>                        
  64.  
  65.     private void ComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {                                          
  66.         // TODO add your handling code here:
  67.         String selectedValue = ComboBox.getSelectedItem().toString();
  68.    
  69.     // Установка текста для Label в зависимости от выбранной цифры
  70.         switch (selectedValue) {
  71.             case "1":
  72.                 Label.setText("Текст для цифры 1");
  73.                 break;
  74.             case "2":
  75.                 Label.setText("Текст для цифры 2");
  76.                 break;
  77.             case "3":
  78.                 Label.setText("Текст для цифры 3");
  79.                 break;
  80.             case "4":
  81.                 Label.setText("Текст для цифры 4");
  82.                 break;
  83.             default:
  84.                 Label.setText("Другой текст");
  85.                 break;
  86.         }
  87.     }                                        
  88.  
  89.     private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                      
  90.         // TODO add your handling code here:
  91.         String title = TextField.getText();
  92.         setTitle(title);
  93.     }                                      
  94.  
  95.     /**
  96.      * @param args the command line arguments
  97.      */
  98.     public static void main(String args[]) {
  99.         /* Set the Nimbus look and feel */
  100.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  101.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  102.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  103.          */
  104.         try {
  105.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  106.                 if ("Nimbus".equals(info.getName())) {
  107.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  108.                     break;
  109.                 }
  110.             }
  111.         } catch (ClassNotFoundException ex) {
  112.             java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  113.         } catch (InstantiationException ex) {
  114.             java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  115.         } catch (IllegalAccessException ex) {
  116.             java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  117.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  118.             java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  119.         }
  120.         //</editor-fold>
  121.  
  122.         /* Create and display the form */
  123.         java.awt.EventQueue.invokeLater(new Runnable() {
  124.             public void run() {
  125.                 new GUIFrame().setVisible(true);
  126.             }
  127.         });
  128.     }
  129.  
  130.     // Variables declaration - do not modify                    
  131.     private javax.swing.JButton Button;
  132.     private javax.swing.JComboBox<String> ComboBox;
  133.     private javax.swing.JLabel Label;
  134.     private javax.swing.JTextField TextField;
  135.     // End of variables declaration                  
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement