Advertisement
leomovskii

FontDialog.java

Jan 16th, 2022
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. package com;
  2.  
  3. import java.awt.Font;
  4. import java.awt.GraphicsEnvironment;
  5. import java.awt.Insets;
  6. import java.awt.event.KeyEvent;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JComponent;
  11. import javax.swing.JDialog;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16. import javax.swing.KeyStroke;
  17.  
  18. public class FontDialog {
  19.  
  20.     private String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  21.  
  22.     private String[] styleNames = { "Обычный", "Жирный", "Курсив", "Жирный курсив" };
  23.     private int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC };
  24.  
  25.     private String[] fontSizeStrings = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28",
  26.             "36", "48", "72" };
  27.     private int[] fontSizes = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
  28.  
  29.     private JDialog fontDialog;
  30.     private JTextArea textArea, exampleText;
  31.     private JComboBox<String> boxFontFamily, boxFontStyle, boxFontSize;
  32.  
  33.     public FontDialog(JFrame mainFrame, JTextArea textArea) {
  34.         fontDialog = new JDialog(mainFrame, "Шрифт", true);
  35.         fontDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  36.         fontDialog.setSize(300, 300);
  37.         fontDialog.setResizable(false);
  38.         fontDialog.setLocationRelativeTo(null);
  39.         fontDialog.setLayout(null);
  40.  
  41.         fontDialog.getRootPane().registerKeyboardAction(e -> fontDialog.dispose(),
  42.                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
  43.  
  44.         this.textArea = textArea;
  45.  
  46.         initUI();
  47.  
  48.         fontDialog.setVisible(true);
  49.     }
  50.  
  51.     private void initUI() {
  52.         exampleText = new JTextArea("Пример текста\n\nText example\n\n文本示例\n\n0 1 2 3 4 5 6 7 8 9");
  53.         exampleText.setFont(textArea.getFont());
  54.         exampleText.setTabSize(1);
  55.         exampleText.setLineWrap(true);
  56.         exampleText.setCaretPosition(exampleText.getText().length());
  57.         exampleText.setMargin(new Insets(3, 3, 3, 3));
  58.  
  59.         JScrollPane scrollPane = new JScrollPane(exampleText);
  60.         scrollPane.setBounds(5, 5, 276, 145);
  61.         fontDialog.add(scrollPane);
  62.  
  63.         JLabel fontFamily = new JLabel("Шрифт:");
  64.         fontFamily.setBounds(10, 155, 80, 20);
  65.         fontDialog.add(fontFamily);
  66.  
  67.         boxFontFamily = new JComboBox<String>(fonts);
  68.         boxFontFamily.setSelectedItem(textArea.getFont().getFamily());
  69.         boxFontFamily.setBounds(80, 155, 200, 20);
  70.         boxFontFamily.addActionListener(e -> updateFont());
  71.         fontDialog.add(boxFontFamily);
  72.  
  73.         JLabel fontStyle = new JLabel("Стиль:");
  74.         fontStyle.setBounds(10, 180, 80, 20);
  75.         fontDialog.add(fontStyle);
  76.  
  77.         boxFontStyle = new JComboBox<String>(styleNames);
  78.         boxFontStyle.setSelectedItem(styleNames[textArea.getFont().getStyle()]);
  79.         boxFontStyle.setBounds(80, 180, 200, 20);
  80.         boxFontStyle.addActionListener(e -> updateFont());
  81.         fontDialog.add(boxFontStyle);
  82.  
  83.         JLabel fontSize = new JLabel("Размер:");
  84.         fontSize.setBounds(10, 205, 80, 20);
  85.         fontDialog.add(fontSize);
  86.  
  87.         boxFontSize = new JComboBox<String>(fontSizeStrings);
  88.         boxFontSize.setSelectedItem(getFontSizeString());
  89.         boxFontSize.setBounds(80, 205, 200, 20);
  90.         boxFontSize.addActionListener(e -> updateFont());
  91.  
  92.         fontDialog.add(boxFontSize);
  93.  
  94.         JButton buttonOK = new JButton("ОК");
  95.         buttonOK.setBounds(94, 235, 80, 20);
  96.         buttonOK.addActionListener(e -> {
  97.             textArea.setFont(exampleText.getFont());
  98.             fontDialog.dispose();
  99.         });
  100.         fontDialog.add(buttonOK);
  101.  
  102.         JButton buttonCancel = new JButton("Отмена");
  103.         buttonCancel.setBounds(196, 235, 80, 20);
  104.         buttonCancel.addActionListener(e -> fontDialog.dispose());
  105.         fontDialog.add(buttonCancel);
  106.     }
  107.  
  108.     private void updateFont() {
  109.         String fontFamily = (String) boxFontFamily.getSelectedItem();
  110.         int fontStyle = styles[boxFontStyle.getSelectedIndex()];
  111.         int fontSize = fontSizes[boxFontSize.getSelectedIndex()];
  112.  
  113.         exampleText.setFont(new Font(fontFamily, fontStyle, fontSize));
  114.     }
  115.  
  116.     private String getFontSizeString() {
  117.         for (int i = 0; i < fontSizes.length; i++)
  118.             if (fontSizes[i] == textArea.getFont().getSize())
  119.                 return fontSizeStrings[i];
  120.         return fontSizeStrings[0];
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement