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);
}
});
}
}
}