Create a window with a JTextPane and a JMenuBar to modify aspects of text

0

I am trying to make a simple exercise work for me in which I want to create a window with a JTextPane and a JMenuBar to modify aspects of all the text within the JTextPane (not just the selected one) as size, bold or font. I want the changes you make to the text, for example, to make it bold, to accumulate to the following changes as I touch the items of the JMenuBar. For this I have created a class in which I pass through the parameters of the constructor the 3 variables of setFont. I have also created 3 other variables in the main class to save the parameters of the constructor and so the changes I make to the text format are accumulated. The problem I have at this point, I can not keep the changes in these variables.

package graficosyventanas;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ejercicio_practico__procesador_texto {


    public static void main(String[] args) {

        marq.setBounds(150,150,1000,450);
        marq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        marq.add(lam);
        lam.setLayout(new BorderLayout());
        lam.add(mibarra, BorderLayout.NORTH);
        lam.add(texto, BorderLayout.CENTER);
        mibarra.add(fuente);
        mibarra.add(estilo);
        mibarra.add(tamanio);

        agrex fuente1 = new ejercicio_practico__procesador_texto().new agrex("Verdana", est, tam, fuente,"Verdana");
        agrex estilo1 = new ejercicio_practico__procesador_texto().new agrex(fu, Font.BOLD, tam, estilo,"Negrita");
        agrex estilo2 = new ejercicio_practico__procesador_texto().new agrex(fu, Font.PLAIN, tam, estilo,"Normal");
        agrex fuente2 = new ejercicio_practico__procesador_texto().new agrex("Calibri", est, tam, fuente,"Calibri");

        marq.setVisible(true);
    }

    private static JFrame marq = new JFrame();

    private static JMenuBar mibarra = new JMenuBar();
    private static JTextPane texto = new JTextPane();
    private static JPanel lam = new JPanel();
    private static JMenu fuente = new JMenu("Fuente");
    private static JMenu tamanio = new JMenu("Tamaño");
    private static JMenu estilo = new JMenu("Estilo");

    private static String fu="Arial";  //las 3 variables para que se me acumulen los cambios de formato del texto
    private static int est=Font.PLAIN;
    private static int tam=12;

    private static agrex tamanio1 = new ejercicio_practico__procesador_texto().new agrex(fu, est, 20, tamanio,"20");

    private class agrex {
            public agrex(String fuent, int estil, int taman, JMenu x,String titulo){

                JMenuItem item = new JMenuItem(titulo);
                x.add(item);
                item.addActionListener(new ActionListener(){

                        public void actionPerformed(ActionEvent e) {        //clase interna anónima

                        texto.setFont(new Font(fuent,estil,taman));
                        tam=taman;
                        est=estil;
                        fu=fuent;
                        System.out.print(fu + ", ");
                        System.out.print(est + ", ");
                        System.out.println(tam);
                    }
                });
            }
          }
        }
    
asked by sombeo 06.10.2016 в 17:05
source

1 answer

2

Here, adapt your code a bit so that you can store the values, change the Agrex class to inherit the JMenuItem and add the new attributes, adding them to the menu, each item will keep the values you sent in the constructor.

package main;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {


    public static void main(String[] args) {

        marq.setBounds(150,150,1000,450);
        marq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        marq.add(lam);
        lam.setLayout(new BorderLayout());
        lam.add(mibarra, BorderLayout.NORTH);
        lam.add(texto, BorderLayout.CENTER);
        mibarra.add(fuente);
        mibarra.add(estilo);
        mibarra.add(tamanio);

        test.Agrex fuente1 = new test.Agrex(fuente, texto, "Verdana", "Verdana", est, tam);
        test.Agrex estilo1 = new test.Agrex(estilo, texto,"Negrita","Negrita", est, tam);

        marq.setVisible(true);
    }

    private static JFrame marq = new JFrame();

    private static JMenuBar mibarra = new JMenuBar();
    private static JTextPane texto = new JTextPane();
    private static JPanel lam = new JPanel();
    private static JMenu fuente = new JMenu("Fuente");
    private static JMenu tamanio = new JMenu("Tamaño");
    private static JMenu estilo = new JMenu("Estilo");

    private static String fu="Arial";  //las 3 variables para que se me acumulen los cambios de formato del texto
    private static int est=Font.PLAIN;
    private static int tam=12;

    private static class Agrex extends JMenuItem{
    private String fuente;
    private int estilo, tamano;
    private JTextPane pane;

    public Agrex(JMenu menu, JTextPane pane, String titulo, String fuente, int estilo, int tamano){
        super(titulo);

        this.pane = pane;
        this.fuente = fuente;
        this.estilo = estilo;
        this.tamano = tamano;
        menu.add(this);

        this.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Agrex agrex = (Agrex)e.getSource();
                System.out.println("agrex: " + agrex.fuente);
                agrex.pane.setFont(new Font(agrex.fuente, agrex.estilo, agrex.tamano));
            }
        });
    }
}

}
    
answered by 07.10.2016 в 04:50