codisinmyvines

swingtask

Dec 18th, 2021
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. package hw11;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. class PlafFrame extends JFrame {
  9.     private JPanel buttonPanel, textPanel;
  10.     private JTextField field;
  11.     private String[] fonts;
  12.     private int[] stylesFont;
  13.  
  14.     public PlafFrame() {
  15.         setTitle("PlafTest");
  16.         setSize(800, 200);
  17.         field = new JTextField(20);
  18.         field.addActionListener(new ActionListener() {
  19.             @Override
  20.             public void actionPerformed(ActionEvent e) {
  21.                 JOptionPane.showMessageDialog(PlafFrame.this,
  22.                         "Ваше слово: " + field.getText());
  23.             }
  24.         });
  25.         textPanel = new JPanel(new FlowLayout());
  26.         textPanel.add(field);
  27.         buttonPanel = new JPanel();
  28.         JMenuBar menuBar = new JMenuBar();
  29.         fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  30.         menuBar.add(createFontMenu());
  31.         menuBar.add(createColorMenu());
  32.         setJMenuBar(menuBar);
  33.         makeButtonStyle("BOLD");
  34.         makeButtonStyle("ITALIC");
  35.         makeButtonStyle("PLAIN");
  36.         String[] fontItems = {
  37.                 "8px",
  38.                 "10px",
  39.                 "14px"
  40.         };
  41.         JComboBox comboBox = new JComboBox(fontItems);
  42.         comboBox.addActionListener(new ActionListener() {
  43.             @Override
  44.             public void actionPerformed(ActionEvent e) {
  45.                 JComboBox box = (JComboBox) e.getSource();
  46.                 String item = (String) box.getSelectedItem();
  47.                 switch (item) {
  48.                     case "8px" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, 8));
  49.                     case "10px" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, 10));
  50.                     case "14px" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, 14));
  51.                     default -> {
  52.                     }
  53.                 }
  54.             }
  55.         });
  56.         getContentPane().add("North", textPanel);
  57.         getContentPane().add("Center", buttonPanel);
  58.         getContentPane().add("East", comboBox);
  59.     }
  60.  
  61.     private JMenu createFontMenu() {
  62.         JMenu fontsMenu = new JMenu("Шрифты");
  63.         JMenuItem[] f = new JMenuItem[fonts.length];
  64.         for (int i = 0; i < fonts.length; i++) {
  65.             f[i] = new JMenuItem(fonts[i]);
  66.             fontsMenu.add(f[i]);
  67.             f[i].addActionListener(new ActionListener() {
  68.                 @Override
  69.                 public void actionPerformed(ActionEvent e) {
  70.                     try {
  71.                         for (int i = 0; i < fonts.length; i++) {
  72.                             if (e.getSource() == f[i]) {
  73.                                 field.setFont(new Font(f[i].getText(), field.getFont().getStyle(), field.getFont().getSize()));
  74.                             }
  75.                         }
  76.                     } catch (Exception ex) {
  77.                         ex.printStackTrace();
  78.                     }
  79.                 }
  80.             });
  81.         }
  82.         return fontsMenu;
  83.     }
  84.  
  85.     private JMenu createColorMenu() {
  86.         JMenu colorsMenu = new JMenu("Цвета");
  87.         JMenuItem[] f = new JMenuItem[4];
  88.         String[] c = new String[]{"Красный", "Зеленый", "Синий", "Желтый"};
  89.         for (int i = 0; i < 4; i++) {
  90.             f[i] = new JMenuItem(c[i]);
  91.             colorsMenu.add(f[i]);
  92.             f[i].addActionListener(new ActionListener() {
  93.                 @Override
  94.                 public void actionPerformed(ActionEvent e) {
  95.                     try {
  96.                         for (int i = 0; i < 4; i++) {
  97.                             if (e.getSource() == f[i]) {
  98.                                 switch (f[i].getText()) {
  99.                                     case "Красный" -> field.setForeground(new Color(255, 0, 0));
  100.                                     case "Зеленый" -> field.setForeground(new Color(0, 255, 0));
  101.                                     case "Синий" -> field.setForeground(new Color(0, 0, 255));
  102.                                     case "Желтый" -> field.setForeground(new Color(255, 255, 0));
  103.                                     default -> {
  104.                                     }
  105.                                 }
  106.                             }
  107.                         }
  108.                     } catch (Exception ex) {
  109.                         ex.printStackTrace();
  110.                     }
  111.                 }
  112.             });
  113.         }
  114.         return colorsMenu;
  115.     }
  116.  
  117.     void makeButtonStyle(String style) {
  118.         JButton button = new JButton(style);
  119.         buttonPanel.add(button);
  120.         button.addActionListener(new ActionListener() {
  121.             @Override
  122.             public void actionPerformed(ActionEvent e) {
  123.                 try {
  124.                     if (e.getSource() == button) {
  125.                         switch (button.getText()) {
  126.                             case "BOLD" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, field.getFont().getSize()));
  127.                             case "ITALIC" -> field.setFont(new Font(field.getFont().getName(), Font.ITALIC, field.getFont().getSize()));
  128.                             case "PLAIN" -> field.setFont(new Font(field.getFont().getName(), Font.PLAIN, field.getFont().getSize()));
  129.                             default -> {
  130.                             }
  131.                         }
  132.                     }
  133.                 } catch (Exception ex) {
  134.                     ex.printStackTrace();
  135.                 }
  136.             }
  137.         });
  138.     }
  139. }
  140.  
  141. public class SwingTask {
  142.     public static void main(String[] args) {
  143.         EventQueue.invokeLater(new Runnable() {
  144.             @Override
  145.             public void run() {
  146.                 PlafFrame frame = new PlafFrame();
  147.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  148.                 frame.setVisible(true);
  149.             }
  150.         });
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment