Trabajo Programaci N 5

TAREA JAVA TEMA DISEÑO. Realizar los ejercicios usando los mismos diseños mostrados. 1-Vamos a crear un imitador, como s

Views 52 Downloads 0 File size 390KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

TAREA JAVA TEMA DISEÑO. Realizar los ejercicios usando los mismos diseños mostrados. 1-Vamos a crear un imitador, como si fuera un espejo. Tendremos dos pares de conjunto de elementos separados (puedes usar un separador) y cuando nosotros pinchamos en un elemento o escribimos en un campo, se debe cambiar el otro lado. Por ejemplo, si yo tengo un campo de texto y escribo en él, el campo de texto que es su reflejo también recibirá ese texto. Puedes usar los elementos que quieras, ejemplo: JTextField, JRadioButton, JCheckBox, JTextArea, JSpinner, etc. Solo puedes modificar de un lado, el otro conjunto no lo podéis modificar, es decir, que no es bidireccional.

public class MainJframe { private JFrame frmImitador; private JTextField textField; private JTextField textField_1; public JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton_1_1, rdbtnNewRadioButton_2_1; public JSpinner spinner_1; public JComboBox comboBox_1; public JCheckBox chckbxNewCheckBox, chckbxNewCheckBox_1, chckbxNewCheckBox_2, chckbxNewCheckBox_3, chckbxNewCheckBox_1_1, chckbxNewCheckBox_2_1; private final ButtonGroup buttonGroup = new ButtonGroup(); private final ButtonGroup buttonGroup_1 = new ButtonGroup(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainJframe window = new MainJframe();

window.frmImitador.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public MainJframe() { initialize(); }

/** * Initialize the contents of the frame. */ private void initialize() { frmImitador = new JFrame(); frmImitador.setTitle("Imitador"); frmImitador.setBounds(100, 100, 300, 335); frmImitador.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmImitador.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); panel.setBounds(10, 11, 264, 134); frmImitador.getContentPane().add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("Original"); lblNewLabel.setBounds(0, 0, 46, 14); panel.add(lblNewLabel);

JRadioButton rdbtnNewRadioButton = new JRadioButton("Opcion 1"); rdbtnNewRadioButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (rdbtnNewRadioButton.isSelected()) { rdbtnNewRadioButton_3.setSelected(true); rdbtnNewRadioButton_1_1.setSelected(false); rdbtnNewRadioButton_2_1.setSelected(false); } } }); buttonGroup.add(rdbtnNewRadioButton); rdbtnNewRadioButton.setBounds(10, 21, 75, 23); panel.add(rdbtnNewRadioButton); JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Opcion 2"); rdbtnNewRadioButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (rdbtnNewRadioButton_1.isSelected()) { rdbtnNewRadioButton_3.setSelected(false); rdbtnNewRadioButton_1_1.setSelected(true); rdbtnNewRadioButton_2_1.setSelected(false); } } });

buttonGroup.add(rdbtnNewRadioButton_1); rdbtnNewRadioButton_1.setBounds(10, 47, 75, 23); panel.add(rdbtnNewRadioButton_1); JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Opcion 3"); rdbtnNewRadioButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (rdbtnNewRadioButton_2.isSelected()) { rdbtnNewRadioButton_3.setSelected(false); rdbtnNewRadioButton_1_1.setSelected(false); rdbtnNewRadioButton_2_1.setSelected(true); } } }); buttonGroup.add(rdbtnNewRadioButton_2); rdbtnNewRadioButton_2.setBounds(10, 73, 75, 23); panel.add(rdbtnNewRadioButton_2); chckbxNewCheckBox = new JCheckBox("Opcion 4"); chckbxNewCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (chckbxNewCheckBox.isSelected()) { chckbxNewCheckBox_3.setSelected(true); chckbxNewCheckBox_1_1.setSelected(false); chckbxNewCheckBox_2_1.setSelected(false); chckbxNewCheckBox.setSelected(true); chckbxNewCheckBox_1.setSelected(false); chckbxNewCheckBox_2.setSelected(false); } } }); chckbxNewCheckBox.setBounds(87, 21, 75, 23); panel.add(chckbxNewCheckBox); chckbxNewCheckBox_1 = new JCheckBox("Opcion 5"); chckbxNewCheckBox_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (chckbxNewCheckBox_1.isSelected()) { chckbxNewCheckBox_3.setSelected(false); chckbxNewCheckBox_1_1.setSelected(true); chckbxNewCheckBox_2_1.setSelected(false); chckbxNewCheckBox.setSelected(false); chckbxNewCheckBox_1.setSelected(true); chckbxNewCheckBox_2.setSelected(false); } } }); chckbxNewCheckBox_1.setBounds(87, 47, 75, 23); panel.add(chckbxNewCheckBox_1); chckbxNewCheckBox_2 = new JCheckBox("Opcion 6"); chckbxNewCheckBox_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (chckbxNewCheckBox_2.isSelected()) { chckbxNewCheckBox_3.setSelected(false);

chckbxNewCheckBox_1_1.setSelected(false); chckbxNewCheckBox_2_1.setSelected(true); chckbxNewCheckBox.setSelected(false); chckbxNewCheckBox_1.setSelected(false); chckbxNewCheckBox_2.setSelected(true); } } }); chckbxNewCheckBox_2.setBounds(87, 73, 75, 23); panel.add(chckbxNewCheckBox_2); textField = new JTextField(); textField.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { textField_1.setText(textField.getText()); } }); textField.setBounds(168, 22, 86, 20); panel.add(textField); textField.setColumns(10); JComboBox comboBox = new JComboBox(); comboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { comboBox_1.setSelectedIndex(comboBox.getSelectedIndex()); } }); comboBox.setModel(new DefaultComboBoxModel(new String[] {"Item1", "Item2", "Item3"})); comboBox.setMaximumRowCount(3); comboBox.setToolTipText("Item 1"); comboBox.setBounds(168, 53, 86, 22); panel.add(comboBox); JSpinner spinner = new JSpinner(); spinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { spinner_1.setValue((Integer)spinner.getValue()); } }); spinner.setModel(new SpinnerNumberModel(1, 1, 20, 1)); spinner.setBounds(168, 86, 86, 20); panel.add(spinner); JPanel panel_1 = new JPanel(); panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); panel_1.setLayout(null); panel_1.setBounds(10, 151, 264, 134); frmImitador.getContentPane().add(panel_1); JLabel lblImitador = new JLabel("Espejo"); lblImitador.setBounds(0, 0, 46, 14); panel_1.add(lblImitador); rdbtnNewRadioButton_3 = new JRadioButton("Opcion 1"); buttonGroup_1.add(rdbtnNewRadioButton_3); rdbtnNewRadioButton_3.setEnabled(false);

rdbtnNewRadioButton_3.setBounds(10, 21, 75, 23); panel_1.add(rdbtnNewRadioButton_3); rdbtnNewRadioButton_1_1 = new JRadioButton("Opcion 2"); buttonGroup_1.add(rdbtnNewRadioButton_1_1); rdbtnNewRadioButton_1_1.setEnabled(false); rdbtnNewRadioButton_1_1.setBounds(10, 47, 75, 23); panel_1.add(rdbtnNewRadioButton_1_1); rdbtnNewRadioButton_2_1 = new JRadioButton("Opcion 3"); buttonGroup_1.add(rdbtnNewRadioButton_2_1); rdbtnNewRadioButton_2_1.setEnabled(false); rdbtnNewRadioButton_2_1.setBounds(10, 73, 75, 23); panel_1.add(rdbtnNewRadioButton_2_1); chckbxNewCheckBox_3 = new JCheckBox("Opcion 4"); chckbxNewCheckBox_3.setEnabled(false); chckbxNewCheckBox_3.setBounds(87, 21, 75, 23); panel_1.add(chckbxNewCheckBox_3); chckbxNewCheckBox_1_1 = new JCheckBox("Opcion 5"); chckbxNewCheckBox_1_1.setEnabled(false); chckbxNewCheckBox_1_1.setBounds(87, 47, 75, 23); panel_1.add(chckbxNewCheckBox_1_1); chckbxNewCheckBox_2_1 = new JCheckBox("Opcion 6"); chckbxNewCheckBox_2_1.setEnabled(false); chckbxNewCheckBox_2_1.setBounds(87, 73, 75, 23); panel_1.add(chckbxNewCheckBox_2_1); textField_1 = new JTextField(); textField_1.setEditable(false); textField_1.setEnabled(false); textField_1.setColumns(10); textField_1.setBounds(168, 22, 86, 20); panel_1.add(textField_1); comboBox_1 = new JComboBox(); comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Item1", "Item2", "Item3"})); comboBox_1.setEnabled(false); comboBox_1.setToolTipText("Item 1"); comboBox_1.setMaximumRowCount(3); comboBox_1.setBounds(168, 53, 86, 22); panel_1.add(comboBox_1); spinner_1 = new JSpinner(); spinner_1.setModel(new SpinnerNumberModel(new Integer(1), null, null, new Integer(1))); spinner_1.setEnabled(false); spinner_1.setBounds(168, 86, 86, 20); panel_1.add(spinner_1); } }

2-Crea un generador de números gráfico. Nosotros escribiremos seleccionaremos dos números en unos JSpinner (contadores) y se nos mostrara en un JTextField, el número generado entre esos dos números, al pulsar en el botón. El JTextField no debe ser editable.

public class WindowsForm2 { public JSpinner spinner, spinner_1; private JFrame frmGeneradorDeNmeros; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { WindowsForm2 window = new WindowsForm2(); window.frmGeneradorDeNmeros.setVisible(true); } catch (Exception e) {

e.printStackTrace(); } } }); } /** * Create the application. */ public WindowsForm2() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frmGeneradorDeNmeros = new JFrame(); frmGeneradorDeNmeros.setTitle("Generador de n\u00FAmeros"); frmGeneradorDeNmeros.setBounds(100, 100, 300, 300); frmGeneradorDeNmeros.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); frmGeneradorDeNmeros.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); JLabel lblNewLabel = new JLabel("Numero 1"); lblNewLabel.setBounds(42, 51, 46, 14); panel.add(lblNewLabel); spinner = new JSpinner(); spinner.setBounds(138, 48, 63, 20); panel.add(spinner); JLabel lblNewLabel_1 = new JLabel("Numero 2"); lblNewLabel_1.setBounds(42, 100, 46, 14); panel.add(lblNewLabel_1); spinner_1 = new JSpinner(); spinner_1.setBounds(138, 97, 63, 20); panel.add(spinner_1); JLabel lblNewLabel_2 = new JLabel("Numero generado"); lblNewLabel_2.setBounds(42, 150, 100, 14); panel.add(lblNewLabel_2); textField = new JTextField(); textField.setEditable(false); textField.setBounds(138, 147, 79, 20); panel.add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("Generar"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int n1 = (int)spinner.getValue(); int n2 = (int)spinner_1.getValue(); int n3 = (int) Math.floor(Math.random() * (n2 - n1 + 1) + (n1)); textField.setText(n3+""); } }); btnNewButton.setBounds(83, 212, 89, 23);

panel.add(btnNewButton); } }

3-Crea una mini encuesta gráfica. Daremos una serie de opciones para que el usuario elija. La encuesta preguntará lo siguiente: Elije un sistema operativo (solo una opción, JRadioButton)

 

Windows



Linux

 

Mac Elije tu especialidad (pueden seleccionar ninguna o varias opciones, JCheckBox)



Programación



Diseño gráfico

Administración  Horas dedicadas en el ordenador (usaremos un slider entre 0 y 10) Para el slider, recomiendo usar un JLabel, que diga qué valor tiene el slider, usar el evento stateChanged.y al final mostrar un mensaje similar al que está debajo de la primera imagen. 

public class Ejercicio_3 { public JLabel lblNewLabel_3; private JFrame frmMiniEncuesta; private final ButtonGroup buttonGroup = new ButtonGroup(); private final ButtonGroup buttonGroup_1 = new ButtonGroup(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Ejercicio_3 window = new Ejercicio_3(); window.frmMiniEncuesta.setVisible(true); } catch (Exception e) { e.printStackTrace();

} } }); } /** * Create the application. */ public Ejercicio_3() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frmMiniEncuesta = new JFrame(); frmMiniEncuesta.setTitle("Mini Encuesta"); frmMiniEncuesta.setBounds(100, 100, 229, 480); frmMiniEncuesta.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmMiniEncuesta.setLocationRelativeTo(null); JPanel panel = new JPanel(); frmMiniEncuesta.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); JLabel lblNewLabel = new JLabel("Elige un sistema operativo"); lblNewLabel.setBounds(10, 27, 180, 14); panel.add(lblNewLabel); JRadioButton Radio1 = new JRadioButton("Windows"); buttonGroup.add(Radio1); Radio1.setBounds(20, 48, 109, 23); panel.add(Radio1); JRadioButton Radio2 = new JRadioButton("Linux"); buttonGroup.add(Radio2); Radio2.setBounds(20, 71, 109, 23); panel.add(Radio2); JRadioButton Radio3 = new JRadioButton("Mac"); buttonGroup.add(Radio3); Radio3.setBounds(20, 97, 109, 23); panel.add(Radio3); JSeparator separator = new JSeparator(); separator.setBounds(10, 132, 180, 2); panel.add(separator); JLabel lblNewLabel_1 = new JLabel("Elige tu especialidad"); lblNewLabel_1.setBounds(10, 155, 135, 14); panel.add(lblNewLabel_1); JCheckBox Check1 = new JCheckBox("Programaci\u00F3n"); Check1.setBounds(20, 182, 170, 23); panel.add(Check1); JCheckBox Check2 = new JCheckBox("Dise\u00F1o gr\u00E1fico"); Check2.setBounds(20, 208, 170, 23); panel.add(Check2); JCheckBox Check3 = new JCheckBox("Adimistraci\u00F3n"); Check3.setBounds(20, 234, 170, 23); panel.add(Check3); JSeparator separator_1 = new JSeparator();

separator_1.setBounds(10, 285, 180, 2); panel.add(separator_1); JLabel lblNewLabel_2 = new JLabel("Horas dedicadas en el ordenador"); lblNewLabel_2.setBounds(10, 308, 214, 14); panel.add(lblNewLabel_2); JSlider slider = new JSlider(); slider.setMaximum(24); slider.setMinimum(1); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { lblNewLabel_3.setText(String.valueOf(slider.getValue())); } }); slider.setBounds(35, 347, 155, 26); panel.add(slider); JButton btnNewButton = new JButton("Generar"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JRadioButton[] rad = {Radio1, Radio2, Radio3}; JCheckBox[] check = {Check1, Check2, Check3}; String info = "Tu sistema operativo favorito es: "; for (int i = 0; i