Translate-Traducir

22/5/14

Pantalla para configurar un procesador de textos

Se debe desarrollar una pantalla para configurar ciertas características de un procesador de texto.
Debe aparecer y poder seleccionarse los márgenes superior e inferior de la página.
Los márgenes pueden ir en el rango de 0 a 10. Desplazar las líneas a medida que modificamos los márgenes.


Descargar fichero fuente.
Descargar fichero ejecutable.

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.SpinnerNumberModel;

public class OpcionesProcesador extends JFrame implements ActionListener, ChangeListener{
    private static final long serialVersionUID = -3061452484755028715L;
    private JPanel contentPane;
    private JButton btnInicializar;
    private JSpinner spMargenSup;
    private JSpinner spMargenInf;
    private JComboBox<String> cbOrientacion;
    private int margenSup, margenInf;
    private boolean apaisado;

    private int modifMargenSup(int valor){
        this.margenSup = valor;
        return this.getMargenSup();
    }

    private int modifMargenInf(int valor){
        this.margenInf = valor;
        return this.getMargenInf();
    }

    public static void main(String[] args) {
        OpcionesProcesador frame = new OpcionesProcesador();
        frame.setVisible(true);
    }

    public OpcionesProcesador() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 472);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        spMargenSup = new JSpinner();
        spMargenSup.setModel(new SpinnerNumberModel(0, 0, 250, 1));
        spMargenSup.setName("spMargenSup");
        spMargenSup.addChangeListener(this);
        spMargenSup.setFont(new Font("Arial", Font.PLAIN, 14));
        spMargenSup.setBounds(218, 46, 57, 30);
        contentPane.add(spMargenSup);
       
        spMargenInf = new JSpinner();
        spMargenInf.addChangeListener(this);
        spMargenInf.setModel(new SpinnerNumberModel(0, 0, 250, 1));
        spMargenInf.setName("spMargenInf");
        spMargenInf.setFont(new Font("Arial", Font.PLAIN, 14));
        spMargenInf.setBounds(218, 122, 57, 30);
       
        contentPane.add(spMargenInf);
       
        cbOrientacion = new JComboBox<String>();
        cbOrientacion.setName("cbOrientacion");
        cbOrientacion.addActionListener(this);
        cbOrientacion.setModel(new DefaultComboBoxModel<String>(new String[] {"Horizontal", "Vertical"}));
        cbOrientacion.setFont(new Font("Arial", Font.PLAIN, 14));
        cbOrientacion.setBounds(352, 46, 183, 30);
        cbOrientacion.setSelectedIndex(1);
        contentPane.add(cbOrientacion);
       
        btnInicializar = new JButton("Inicializar");
        btnInicializar.setName("btnInicializar");
        btnInicializar.setFont(new Font("Arial", Font.PLAIN, 14));
        btnInicializar.setBounds(45, 233, 125, 40);
        btnInicializar.addActionListener(this);
        contentPane.add(btnInicializar);
       
        JLabel lblOrientacion = new JLabel("Orientaci\u00F3n de la p\u00E1gina");
        lblOrientacion.setName("lblOrientacion");
        lblOrientacion.setHorizontalAlignment(SwingConstants.CENTER);
        lblOrientacion.setFont(new Font("Arial", Font.BOLD, 12));
        lblOrientacion.setBounds(352, 11, 183, 24);
        contentPane.add(lblOrientacion);
       
        JLabel lblHoja = new JLabel("Hoja");
        lblHoja.setName("lblHoja");
        lblHoja.setHorizontalAlignment(SwingConstants.CENTER);
        lblHoja.setFont(new Font("Arial", Font.BOLD, 12));
        lblHoja.setBounds(48, 11, 93, 24);
        contentPane.add(lblHoja);
       
        JLabel lblMargenSuperior = new JLabel("Margen Superior");
        lblMargenSuperior.setHorizontalAlignment(SwingConstants.CENTER);
        lblMargenSuperior.setFont(new Font("Arial", Font.BOLD, 12));
        lblMargenSuperior.setBounds(189, 11, 115, 24);
        contentPane.add(lblMargenSuperior);
       
        JLabel lblMargenInferior = new JLabel("Margen Inferior");
        lblMargenInferior.setHorizontalAlignment(SwingConstants.CENTER);
        lblMargenInferior.setFont(new Font("Arial", Font.BOLD, 12));
        lblMargenInferior.setBounds(189, 87, 115, 24);
        contentPane.add(lblMargenInferior);
       
        Inicializar();
    }
   
    private void Inicializar(){
        // Inicializamos las variables
        margenSup = 0;
        spMargenSup.setValue(0);
        margenInf = 0;
        spMargenInf.setValue(0);
        setApaisado(false);
        cbOrientacion.setSelectedIndex(1);
    }
   
    @Override
    public void paint(Graphics g){
        super.paint(g);
        // dependiento si la selección de apaisado este o no activa pintamos la hoja en vertical u horizontal
        if (isApaisado()){
            // La página está en horizontal
            g.setColor(Color.BLUE);
            g.drawRect(20, 70, 180, 120);

            g.setColor(Color.RED);
            g.drawLine(20, 70+this.getMargenSup(), 200, 70+this.getMargenSup());
            g.drawLine(20, 190-this.getMargenInf(), 200, 190-this.getMargenInf());

            g.drawRect(355, 120, 180, 120);
        } else {
            // La página está en vertical
            g.setColor(Color.BLUE);
            g.drawRect(50, 70, 120, 180);

            g.setColor(Color.RED);
            g.drawLine(50, 70+this.getMargenSup(), 170, 70+this.getMargenSup());
            g.drawLine(50, 250-this.getMargenInf(), 170, 250-this.getMargenInf());

            g.drawRect(385, 120, 120, 180);           
        }
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        // Comprobamos si el evento que se ha detectado es de un JButton o de un JComboBox
        if (ev.getSource() instanceof JButton){
            JButton boton = (JButton) ev.getSource();
            switch (boton.getName()){
            case "btnInicializar":
                Inicializar();
                repaint();
                break;
            }
        } else if (ev.getSource() instanceof JComboBox){
            @SuppressWarnings("rawtypes")
            JComboBox combo = (JComboBox) ev.getSource();
            switch ((Integer) combo.getSelectedIndex()){
            case 0:
                setApaisado(true);
                break;
            case 1:
                setApaisado(false);
                break;
            }
            repaint();
        }   
    }

    @Override
    public void stateChanged(ChangeEvent ev) {
        // Comprobamos si el evento que se ha detectado es de un JSpinner
        if (ev.getSource() instanceof JSpinner){
            JSpinner spinner = (JSpinner) ev.getSource();
            int valor = (Integer) spinner.getValue();
            switch (spinner.getName()){
            case "spMargenSup":
                this.modifMargenSup(valor);
                break;
            case "spMargenInf":
                this.modifMargenInf(valor);
                break;
            }
            repaint();
        }
    }

    public boolean isApaisado() {
        return apaisado;
    }

    public void setApaisado(boolean apaisado) {
        this.apaisado = apaisado;
    }
   
    public int getMargenSup() {
        return margenSup;
    }

    public int getMargenInf() {
        return margenInf;
    }
   
}

No hay comentarios: