Download Resumen_Clase_11

Document related concepts
no text concepts found
Transcript
Prof. Gueorgi Khatchatourov
UEA: LABORATORIO DE PROGRAMACION ORIENTADA A OBJETOS
(1151072)
Resumen de Clase(virtual, durante el paro) #11
Fecha: 16/10/14
Tema "Ventanas de la interfaz gráfica de usuario" (parte 2).
Analisis del proyecto 'choice'
1. Clase abtsracta Component. Recuerde que su primer uso fue en p.1 de
http://newton.uam.mx/xgeorge/lab_prog_o_o/14_O/Resumen_Clase_6.doc
Un extracto de la documentación oficial:
/////
http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html
java.awt
//////
Class Component
java.lang.Object
java.awt.Component
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable
Direct Known Subclasses:
Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar,
TextComponent
---------------------------------------------------------------------public abstract class Component
extends Object
implements ImageObserver, MenuContainer, Serializable
A component is an object having a graphical representation that can be
displayed on the screen and that can interact with the user. Examples of
components are the buttons, checkboxes, and scrollbars of a typical graphical
user interface.
The Component class is the abstract superclass of the nonmenu-related Abstract
Window Toolkit components. Class Component can also be extended directly to
create a lightweight component. A lightweight component is a component that is
not associated with a native window. On the contrary, a heavyweight component
is associated with a native window. The isLightweight() method may be used to
distinguish between the two kinds of the components.
Lightweight and heavyweight components may be mixed in a single component
hierarchy. However, for correct operating of such a mixed hierarchy of
components, the whole hierarchy must be valid. When the hierarchy gets
invalidated, like after changing the bounds of components, or adding/removing
components to/from containers, the whole hierarchy must be validated afterwards
by means of the Container.validate() method invoked on the top-most invalid
container of the hierarchy.
//////////////////////////////////
2. Llevan attención al uso en el pryecto 'choice' de una integración de varias técnicas:
'extended'
'implements ActionListener'
abstract class Component con sus subclasses (ver la lista de métodos disponibles
para objeto 'frame')
3.Analicen clases
JLabel,JCheckBox, JRadioButton, JComboBox
4. En proyecto "choice" ¿A parte de tres fuentes a seleccionar, Serif, SansSerif, y
Monospaced, el usuario final tiene opción de asignar algúin otro?
5. Lean mis comentarios a continuación al archivo "FontViewerFrame.java" del proyecto
'choice'; ¿Cuál línea del código es finalmente responsable de la modificación del tipo de fuente
después de que el programa obtuvo la información completa sobre la selección hecha por el
usuario?
6. Tarea de "interfaz gráfica de usuario. Combinar los proyectos
'choice', 'menu', y 'slider' de la manera siguiente:
-- su ventana debe simultaneamente proporcionar al usuario ambos
canales de la modificación del texto: tanto aquellos que
proporciona 'choice' como los de 'menu';
-- también en misma ventana deben ser implementados los controles
del proyecto 'slider', sin embargo aplicables no al color del
fondo de la ventana sino al propio texto.
//////////////////////////////////
package ed3_ch18_choice;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
java.awt.BorderLayout;
java.awt.Font;
java.awt.GridLayout;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
javax.swing.ButtonGroup;
javax.swing.JButton;
javax.swing.JCheckBox;
javax.swing.JComboBox;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JPanel;
javax.swing.JRadioButton;
javax.swing.border.EtchedBorder;
javax.swing.border.TitledBorder;
public class FontViewerFrame extends JFrame
{
/**
Constructs the frame.
*/
public FontViewerFrame() // constructor de marco de la interfaz
// gráfica solo asigna un tipo de distribución, introduce
// listener, y indica el método setSampleFont(); como
// responsable de detalles particulares
{
// Construct text sample
sampleField = new JLabel("Big Java"); //subclass of Component
add(sampleField, BorderLayout.CENTER); //a JFrame method
//¿que significa *.CENTER con respecto de posibles elementos
// introducidos luego?
// This listener is shared among all components
class ChoiceListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
setSampleFont();
//actualice la ventana
//de la interfaz gráfica despues de un evento
}
}
listener = new ChoiceListener();
createControlPanel(); //indica detalización de la ventana interfaz
// gráfica mediante una serie de paneles
setSampleFont(); // realiza presentación inicial de la interfaz gráfica
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
/**
Creates the control panel to change the font.
*/
public void createControlPanel()
{
JPanel facenamePanel = createComboBox();
JPanel sizeGroupPanel = createCheckBoxes();
JPanel styleGroupPanel = createRadioButtons();
// Line up component panels
JPanel controlPanel = new JPanel();
// controlPanel será panel-contenedora de otros tres paneles
controlPanel.setLayout(new GridLayout(3, 1));
controlPanel.add(facenamePanel);
controlPanel.add(sizeGroupPanel);
controlPanel.add(styleGroupPanel);
// Add panels to content pane
add(controlPanel, BorderLayout.SOUTH);
//¿cómo va a cambiar ventana con
*.NORTH?
}
/**
Creates the combo box with the font style choices.
@return the panel containing the combo box
*/
public JPanel createComboBox()
{
facenameCombo = new JComboBox();
facenameCombo.addItem("Serif");
facenameCombo.addItem("SansSerif");
facenameCombo.addItem("Monospaced");
facenameCombo.setEditable(true);
facenameCombo.addActionListener(listener);
JPanel panel = new JPanel();
panel.add(facenameCombo);
return panel;
}
/**
Creates the check boxes for selecting bold and italic styles.
@return the panel containing the check boxes
*/
public JPanel createCheckBoxes()
{
italicCheckBox = new JCheckBox("Italic");
italicCheckBox.addActionListener(listener);
boldCheckBox = new JCheckBox("Bold");
boldCheckBox.addActionListener(listener);
JPanel panel = new JPanel();
panel.add(italicCheckBox);
panel.add(boldCheckBox);
panel.setBorder
(new TitledBorder(new EtchedBorder(), "Style"));
return panel;
}
/**
Creates the radio buttons to select the font size
@return the panel containing the radio buttons
*/
public JPanel createRadioButtons()
{
smallButton = new JRadioButton("Small");
smallButton.addActionListener(listener);
mediumButton = new JRadioButton("Medium");
mediumButton.addActionListener(listener);
largeButton = new JRadioButton("Large");
largeButton.addActionListener(listener);
largeButton.setSelected(true);
// Add radio buttons to button group
ButtonGroup group = new ButtonGroup();
group.add(smallButton);
group.add(mediumButton);
group.add(largeButton);
JPanel panel = new JPanel();
panel.add(smallButton);
panel.add(mediumButton);
panel.add(largeButton);
panel.setBorder
(new TitledBorder(new EtchedBorder(), "Size"));
return panel;
}
/**
Gets user choice for font name, style, and size
and sets the font of the text sample.
*/
public void setSampleFont()
{
// Get font name
String facename
= (String) facenameCombo.getSelectedItem();
// Get font style
int style = 0;
if (italicCheckBox.isSelected())
style = style + Font.ITALIC;
if (boldCheckBox.isSelected())
style = style + Font.BOLD;
// Get font size
int size = 0;
final int SMALL_SIZE = 24;
final int MEDIUM_SIZE = 36;
final int LARGE_SIZE = 48;
if (smallButton.isSelected())
size = SMALL_SIZE;
else if (mediumButton.isSelected())
size = MEDIUM_SIZE;
else if (largeButton.isSelected())
size = LARGE_SIZE;
// Set font of text field
sampleField.setFont(new Font(facename, style, size));
sampleField.repaint();
}
private
private
private
private
private
private
private
private
JLabel sampleField;
JCheckBox italicCheckBox;
JCheckBox boldCheckBox;
JRadioButton smallButton;
JRadioButton mediumButton;
JRadioButton largeButton;
JComboBox facenameCombo;
ActionListener listener;
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
}