Reputation: 1
I'm following a course and, in theory, I'm already able to handle POO, JFrame
, JPanel
, Event and Layout. I'm in the middle of the Swing components and I'm trying to extend one of the practices made by the teacher.
The objective is to modify a selected text through some JItems. The teacher used JItem to modify the size of the selected text in the same way he did with the formatting and style. In my case, I would like to use a JSpinner
to control only the selected text. I don't know if I can do this.
I am enclosing the code of what I have done.
public class ProcesadorDeTextos {
public static void main(String[] args) {
// TODO Auto-generated method stub
MarcoProcesador marquito = new MarcoProcesador();
marquito.setDefaultCloseOperation(3);
}
}
class MarcoProcesador extends JFrame{
MarcoProcesador(){
setTitle("El Palabra");
setBounds(300, 200, 800, 450);
add(new LaminaProcesador());
setVisible(true);
}
}
class LaminaProcesador extends JPanel{
public LaminaProcesador() {
setLayout(new BorderLayout());
//-----2 principle components
campoTexto = new JTextPane();
barraTools = new JMenuBar();
//Scroll
scrollTexto = new JScrollPane(campoTexto);
//---------------toolbar/barraTools-------------
fuente = new JMenu("Fuente");
estilo = new JMenu("Estilo");
configuraMenu("Arial", "fuente","Arial",1,1);
configuraMenu("Courier", "fuente","Courier",1,1);
configuraMenu("Verdana", "fuente","Verdana",1,1);
configuraMenu("Negrita", "estilo","",Font.BOLD,1);
configuraMenu("Cursiva", "estilo","", Font.ITALIC,1);
configuraMenu("", "","", 1,1);
barraTools.add(fuente);
barraTools.add(estilo);
//-------------------Adding principal components----------------
JPanel Herramientas = new JPanel();
Herramientas.add(barraTools);
add(scrollTexto, BorderLayout.CENTER);
add(Herramientas, BorderLayout.NORTH);
}
private void configuraMenu(String rotulo, String menu, String tipoLetra, int estilos, int tamagnos) {
JMenuItem elemMenu = new JMenuItem(rotulo);
if(menu == "fuente") {
fuente.add(elemMenu);
elemMenu.addActionListener(new StyledEditorKit.FontFamilyAction("cambiaLetra", tipoLetra));
// StyledEditorKit.FontFamilyAction ya tiene el método ActionPerformed desarrollado
}
else if(menu == "estilo") {
estilo.add(elemMenu);
if(estilos == Font.BOLD) {
elemMenu.addActionListener(new StyledEditorKit.BoldAction());
}
else elemMenu.addActionListener(new StyledEditorKit.ItalicAction());
}
else {
JSpinner size = new JSpinner(new SpinnerNumberModel(12, 8, 24, 2));
size.setPreferredSize(new Dimension (45,2));
//size.addChangeListener(new StyledEditorKit.FontSizeAction("cambiaTamaño", ));
barraTools.add(size);
}
}
private JTextPane campoTexto;
private JScrollPane scrollTexto;
private JMenuBar barraTools;
private JMenu fuente, estilo;
}
Upvotes: 0
Views: 76