Java Swing - Elemental functions/methods/components

package TODO; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JColorChooser; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.SwingConstants; public class Graphics implements ActionListener, PropertyChangeListener{ JFrame ventana; JPanel panelPrincipal; public Graphics(String titulo, int tamX, int tamY, int posX, int posY, boolean maxi){ ventana = new JFrame(titulo); ventana.setSize(tamX, tamY); ventana.setLocation(posX, posY); ventana.setJMenuBar(crearMenuBar()); ventana.setContentPane(crearPanelVentana()); //ventana.getContentPane().add(crearToolBar(),BorderLayout.NORTH); //accCut.setEnabled(false); ventana.setVisible(true); if (maxi) ventana.setExtendedState(JFrame.MAXIMIZED_BOTH); ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } JMenuBar mBar; JMenu menu1; JMenuItem item1; JMenuItem item2; JMenuItem item3; //AbstractAction accCut,accPaste,accCopy; /* private void crearAcciones() { accCut = new MiAccion ("Cut", new ImageIcon("iconos/editcut.png"), "Cortar", KeyEvent.VK_C); accCopy = new MiAccion ("Copy",new ImageIcon("iconos/editcopy.png"),"Copiar",KeyEvent.VK_P); } // Añadir a ToolBar. boton =(JButton) toolBar.add(new JButton (accCut)); // Añadir al menú simple. opcionMenu = menuEdit.add (accCut); */ public JMenuBar crearMenuBar(){ mBar = new JMenuBar(); mBar.add(crearMenu1()); return mBar; } public JMenu crearMenu1(){ menu1 = new JMenu("Menú"); item1 = menu1.add("Item 1"); item2 = menu1.add("Item 2"); item3 = menu1.add("Item 3"); //item1.setIcon(new ImageIcon(".png")); item1.setActionCommand("item1"); item2.setActionCommand("item2"); item3.setActionCommand("item3"); item1.addActionListener(this); item2.addActionListener(this); item3.addActionListener(this); return menu1; } /* private JToolBar crearToolBar() { JToolBar toolBar = new JToolBar(); toolBar.setBorder(BorderFactory.createRaisedBevelBorder()); JButton boton; boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/window_new.png"))); boton.setActionCommand("Open"); boton.addActionListener(this); boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/kgpg.png"))); boton.setActionCommand("Close"); boton.addActionListener(this); toolBar.addSeparator(new Dimension (20,0)); boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/editcopy.png"))); boton.setActionCommand("Copy"); boton.addActionListener(this); boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/editcut.png"))); boton.setActionCommand("cut"); boton.addActionListener(this); boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/editpaste.png"))); boton.setActionCommand("paste"); boton.addActionListener(this); boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/editdelete.png"))); boton.setActionCommand("delete"); boton.addActionListener(this); toolBar.add(Box.createHorizontalGlue()); boton =(JButton) toolBar.add(new JButton (new ImageIcon("icons/exit.png"))); boton.setActionCommand("salir"); boton.addActionListener(this); return toolBar; } */ public Container crearPanelVentana(){ panelPrincipal = new JPanel(new GridLayout(4, 1, 10, 0)); panelPrincipal.add(crearPanel_1()); panelPrincipal.add(crearPanel_2()); panelPrincipal.add(crearPanel_3()); panelPrincipal.add(crearPanel_4()); return panelPrincipal; } JPanel panel_1; JTextField field_1; JButton boton_1; JLabel label_1; public Container crearPanel_1(){ panel_1 = new JPanel(new GridLayout(1, 3, 10, 0)); panel_1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red), BorderFactory.createTitledBorder("Título"))); panel_1.add(crearLabel_1()); panel_1.add(crearField_1()); panel_1.add(crearBoton_1()); return panel_1; } public Component crearBoton_1(){ boton_1 = new JButton("Copiar"); boton_1.addActionListener(this); boton_1.setActionCommand("botonCopia"); return boton_1; } public Component crearField_1(){ field_1 = new JTextField(); field_1.setForeground(Color.RED); field_1.setHorizontalAlignment(JLabel.CENTER); field_1.addActionListener(this); field_1.setActionCommand("fieldCopia"); return field_1; } public Component crearLabel_1(){ label_1 = new JLabel(); label_1.setFont(new Font("arial",Font.BOLD,84)); label_1.setForeground(Color.RED); label_1.setHorizontalAlignment(JLabel.CENTER); return label_1; } JPanel panel_2; JLabel label_2; JButton boton_2; JOptionPane prompt_2; public Container crearPanel_2(){ panel_2 = new JPanel(new GridLayout(1, 2, 10, 0)); panel_2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red), BorderFactory.createTitledBorder("Título"))); panel_2.add(crearField_2()); panel_2.add(crearBoton_2()); return panel_2; } public Component crearField_2(){ label_2 = new JLabel(); return label_2; } public Component crearBoton_2(){ boton_1 = new JButton("Cambiar color"); boton_1.addActionListener(this); boton_1.setActionCommand("botonColor"); return boton_1; } JPanel panel_3; ButtonGroup group = new ButtonGroup(); JRadioButton rBoton_31; JRadioButton rBoton_32; JCheckBox check_31; JCheckBox check_32; public Container crearPanel_3(){ panel_3 = new JPanel(new GridLayout(1, 4, 10, 0)); panel_3.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red), BorderFactory.createTitledBorder("Título"))); panel_3.add(crearRadioBoton(rBoton_31, "Opción 1")); panel_3.add(crearRadioBoton(rBoton_32, "Opción 2")); panel_3.add(crearCheckBox(check_31, "Opción 1")); panel_3.add(crearCheckBox(check_32, "Opción 2")); return panel_3; } public Component crearRadioBoton(JRadioButton radioBoton, String titulo){ radioBoton = new JRadioButton(titulo); radioBoton.setFont(new Font("arial",Font.BOLD,20)); radioBoton.setForeground(Color.BLACK); radioBoton.setHorizontalAlignment(JLabel.CENTER); group.add(radioBoton); return radioBoton; } public Component crearCheckBox(JCheckBox checkBox, String titulo){ checkBox = new JCheckBox(titulo); checkBox.setFont(new Font("arial",Font.BOLD,20)); checkBox.setForeground(Color.BLACK); checkBox.setHorizontalAlignment(JLabel.CENTER); return checkBox; } JPanel panel_4; JLabel label_4_user; JLabel label_4_pass; JTextField field_4; JTextField pass_4; JButton boton_4; final String userF = "urko"; final String passF = "urko"; public Container crearPanel_4(){ panel_4 = new JPanel(new GridLayout(3, 2, 10, 0)); panel_4.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red), BorderFactory.createTitledBorder("Título"))); panel_4.add(crearLabel(label_4_user, "Usuario (urko): ")); panel_4.add(crearField_4()); panel_4.add(crearLabel(label_4_user, "Contraseña (urko): ")); panel_4.add(crearPassField_4()); panel_4.add(crearBoton_4()); return panel_4; } public Component crearLabel(JLabel label, String titulo){ label = new JLabel(titulo, SwingConstants.RIGHT); return label; } public Component crearField_4(){ field_4 = new JTextField(); return field_4; } public Component crearPassField_4(){ pass_4 = new JTextField(); return pass_4; } public Component crearBoton_4(){ boton_4 = new JButton("Acceder"); boton_4.addActionListener(this); boton_4.setActionCommand("botonAcceder"); return boton_4; } JPanel panel_5; JList<Objeto> list_5; DefaultListModel<Objeto> modelo; JComboBox combo_5; JFileChooser chooser_5; JSplitPane split; JToolBar tBar; JScrollPane scroll; JProgressBar pBar; /* private Component crearPanelProgressBar() { JPanel panel = new JPanel (new FlowLayout (10)); startButton = new JButton("Start"); startButton.setActionCommand("start"); startButton.addActionListener(this); progressBar = new JProgressBar(SwingConstants.HORIZONTAL,0, 100); progressBar.setPreferredSize (new Dimension (450,30)); progressBar.setValue(0); progressBar.setStringPainted(true); panel.add(startButton); panel.add(progressBar); return panel; } */ @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("botonCopia")){ label_1.setText(field_1.getText()); } if(e.getActionCommand().equals("botonColor")){ Color color_2 = JColorChooser.showDialog(null, "Elije un color", label_2.getBackground()); label_2.setBackground(color_2); label_2.setOpaque(true); prompt_2 = new JOptionPane(); JOptionPane.showMessageDialog(prompt_2,"Ha cambiado el color.", "Aviso",JOptionPane.WARNING_MESSAGE); } if(e.getActionCommand().equals("botonAcceder")){ String user, pass; user = field_4.getText(); pass = pass_4.getText(); if (user.equals(userF)){ if (pass.equals(passF)) JOptionPane.showMessageDialog(prompt_2,"Datos correctos", "Bienvenido",JOptionPane.INFORMATION_MESSAGE); else JOptionPane.showMessageDialog(prompt_2,"Datos incorrectos", "Error",JOptionPane.ERROR_MESSAGE); } else JOptionPane.showMessageDialog(prompt_2,"Datos incorrectos", "Error",JOptionPane.ERROR_MESSAGE); } } @Override public void propertyChange(PropertyChangeEvent evt) { // TODO Auto-generated method stub } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.