Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw11;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- class PlafFrame extends JFrame {
- private JPanel buttonPanel, textPanel;
- private JTextField field;
- private String[] fonts;
- private int[] stylesFont;
- public PlafFrame() {
- setTitle("PlafTest");
- setSize(800, 200);
- field = new JTextField(20);
- field.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JOptionPane.showMessageDialog(PlafFrame.this,
- "Ваше слово: " + field.getText());
- }
- });
- textPanel = new JPanel(new FlowLayout());
- textPanel.add(field);
- buttonPanel = new JPanel();
- JMenuBar menuBar = new JMenuBar();
- fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
- menuBar.add(createFontMenu());
- menuBar.add(createColorMenu());
- setJMenuBar(menuBar);
- makeButtonStyle("BOLD");
- makeButtonStyle("ITALIC");
- makeButtonStyle("PLAIN");
- String[] fontItems = {
- "8px",
- "10px",
- "14px"
- };
- JComboBox comboBox = new JComboBox(fontItems);
- comboBox.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JComboBox box = (JComboBox) e.getSource();
- String item = (String) box.getSelectedItem();
- switch (item) {
- case "8px" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, 8));
- case "10px" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, 10));
- case "14px" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, 14));
- default -> {
- }
- }
- }
- });
- getContentPane().add("North", textPanel);
- getContentPane().add("Center", buttonPanel);
- getContentPane().add("East", comboBox);
- }
- private JMenu createFontMenu() {
- JMenu fontsMenu = new JMenu("Шрифты");
- JMenuItem[] f = new JMenuItem[fonts.length];
- for (int i = 0; i < fonts.length; i++) {
- f[i] = new JMenuItem(fonts[i]);
- fontsMenu.add(f[i]);
- f[i].addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- for (int i = 0; i < fonts.length; i++) {
- if (e.getSource() == f[i]) {
- field.setFont(new Font(f[i].getText(), field.getFont().getStyle(), field.getFont().getSize()));
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- return fontsMenu;
- }
- private JMenu createColorMenu() {
- JMenu colorsMenu = new JMenu("Цвета");
- JMenuItem[] f = new JMenuItem[4];
- String[] c = new String[]{"Красный", "Зеленый", "Синий", "Желтый"};
- for (int i = 0; i < 4; i++) {
- f[i] = new JMenuItem(c[i]);
- colorsMenu.add(f[i]);
- f[i].addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- for (int i = 0; i < 4; i++) {
- if (e.getSource() == f[i]) {
- switch (f[i].getText()) {
- case "Красный" -> field.setForeground(new Color(255, 0, 0));
- case "Зеленый" -> field.setForeground(new Color(0, 255, 0));
- case "Синий" -> field.setForeground(new Color(0, 0, 255));
- case "Желтый" -> field.setForeground(new Color(255, 255, 0));
- default -> {
- }
- }
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- return colorsMenu;
- }
- void makeButtonStyle(String style) {
- JButton button = new JButton(style);
- buttonPanel.add(button);
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- if (e.getSource() == button) {
- switch (button.getText()) {
- case "BOLD" -> field.setFont(new Font(field.getFont().getName(), Font.BOLD, field.getFont().getSize()));
- case "ITALIC" -> field.setFont(new Font(field.getFont().getName(), Font.ITALIC, field.getFont().getSize()));
- case "PLAIN" -> field.setFont(new Font(field.getFont().getName(), Font.PLAIN, field.getFont().getSize()));
- default -> {
- }
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- }
- public class SwingTask {
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- PlafFrame frame = new PlafFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment