Sei sulla pagina 1di 3

import import import public green};

java.awt.*; java.awt.event.*; javax.swing.*; class TesteJMenu extends JFrame{ private Color colorValue[] = {Color.black, Color.blue, Color.red, Color.

private JRadioButtonMenuItem itensDasCores[],fontes[]; private JCheckBoxMenuItem itensDosEstilos[]; private JLabel texto; private ButtonGroup umaUnicaFonte, umaUnicaCor; private int estilo; private icon carta = new ImageIcon("figuras/carta.gif"); private Icon figura1 = new ImageIcon("figuras/logo.gif"); public TesteJMenu(){ super("Testando JMenus"); Container container = getContentPane(); container.setLayout(new FlowLayout()); JMenu menuArquivo = new JMenu("Arquivo"); ItemFigura1.setMnemonic('F'); itemFigura1.addActionListener(); new ActionListener(){ public void actionPerformed(ActionEvent evento){ inserir(); } }; menuArquivo.add(itemFigura1); JMenuItem itemSair = new JMenuItem("Sair"); itemSair.setMnemonic('S'); itemSair.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent evento){ System.exit(0); } }); menuArquivo.add(itemSair); JMenu menuAjuda = new JMenu("Ajuda"); menuAjuda.setMnemonic('H'); JMenuItem itemUniversitarios = new JMenuItem("Aluno"); itemUniversitarios.setMnemonic('U'); itemUniversitarios.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent evento){ JOptionPane.showMessageDialog( TesteJMenu.this, "Bem vindo a Pre para Cursos", "Ajuda", JOptionPane.PLAI N_MESSAGE); } }); menuAjuda.add(itemUniversitarios); JMenuItem itemCartas = new JMenuItem("Cartas"); itemCartas.setMnemonic('C'); itemCartas.addActionListener(){ new ActionListener(){ public void actionPerformed(ActionEvent event){ JOptionPane.showMessageDialog(

TesteJMenu.this, "Prepara Cursos", "Cartas", JOptionPane.PLAIN_MESSAG E, carta); } }; menuAjuda.add(itemCartas); JMenuBar barra = new JMenuBar(); setJMenuBar(barra); barra.add(menuArquivo); barra.add(menuAjuda); JMenu formatMenu = new JMenu("Formatar"); formatMenu.setMnemonic('r'); String cores[] = {"Preto", "Azul", "Vermelho", "Verde"}; JMenu menuCor = new JMenu("Cor"); menuCor.setMnemonic('C'); ItensDasCores= new JRadioButtonMenuItem[cores.legth]; umaUnicaCor = new ButtonGroup(); TratadorDeItens trat = new TratadorDeItens(); for (int count = 0;count < cores.length; count++){ itensDasCores[count] = new JRadioButtonMenuItem( cores[count]); menuCor.add(itensDasCores[count]); umaUnicaCor.add(itensDasCores[count]); itensDasCores[count].addActionListener(trat); } itensDasCores[0].setSelected(true); formatMenu.add(menuCor); formatMenu.addSeparator(); String nomeDasFontes[] = {"Serif", "Monospaced", "SansSe rif"}; JMenu menuFonte = newFonte = new JMenu("Fonte"); menuFonte.setMnemonic('n'); fontes = new JRadioButtonMenuItem[3]; umaUnicaFonte = new ButtonGroup(); for (int count = 0; count < fontes.legth; count++){ fontes[count] = new JRadioButtonMenuItem(nomeDas Fontes[count]); menuFonte.add(fontes[count]); umaUnicaFonte.add(fontes[count]); fontes[count].addActionListener(trat); } fontes[0].setSelected(true); menuFonte.addSeparator(); String estiloNames[] = {"Bold", "Italic"}; itensDosEstilos = new JCheckBoxMenuItem[estiloNames.leng th]; TratadorDeEstilo estiloHandler = new TratadorDeEstilo(); For(int count = 0; count < estiloNames.length; count++){ itensDosEstilos[count] = new JCheckBoxMenuItem(e stiloNames[count]); menuFonte.add(itensDosEstilos[count]); itensDosEstilos[count].addItemListener(estiloHan dler); } formatMenu.add(menuFonte); barra.add(formatMenu); texto = new JLabel("Prepara Cursos", SwingConstants.CENT

ER); texto.setForeground(colorValues[0]); texto.setFont(new Font("TimesRoman", Font.PLAIN, 72)); container.setBackground(Color.white); container.add(texto); setSize(700, 500); setVisible(true); } public void inserir(){ JLabel labelFigura1 = new JLabel(); labelFigura1.setIcon(figura1); this.getContentPane().add(labelFigura1); this.repaint(); this.show(); } public static void main(String args[]){ TesteJMenu application = new TesteJMenu(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E); } private class TratadorDeItens implements ActionListener{ public void actionPerformed(ActionEvent evento){ for(int count = 0; count < itensDasCores.length; count++) if(itensDasCores[count].isSelected()){ texto.setForeground(colorValues[ count]); break; } for(int count = 0; count < fontes.length; count+ +){ texto.setFont(new Font(fontes[count].get Text(), estilo, 72)); break; } repaint(); } private class TratadorDeEstilo implements ItemListener{ public void itemStateChanged(ItemEvent item){ estilo = 0; If(itensDosEstilos[0].isSelected()); estilo += Font.BOLD; if(itensDosEstilos[1].isSelected()); estilo += Font.ITALIC; texto.setFont(new Font(texto.getFont().g etName(), estilo, 72)); repaint(); } } }