Ho questo problema ho creato un menu e "catturati" gli eventi a seconda del JMenuItem selezionato,ora se clicco sul menu Help come da codice allegato dovrebbe visualizzare un JDialog con delle istruzioni,ma una volta cliccato non compare nulla
codice:import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * * @author * */ public class GameMenu extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private JPanel firstPanel; private JMenuBar bar; private JMenu title; private JMenu menuHelp; private JMenu options; private JMenuItem newGame; private JMenuItem exit; private GUI game; private JRadioButtonMenuItem buttonOn; private JRadioButtonMenuItem buttonOff; private ButtonGroup group; private JOptionPane helpPane; private String longMessage = "Sposta il cursore sui due pezzi che vuoi spostare\n" + " utilizzando le frecce della tastiera,\n" + " per cambiare la posizione dei pezzi\n" +" clicca sulla barra spaziatrice\n"; public GameMenu() { super("Tetris Attack"); super.setJMenuBar(createMenuBar()); this.setDefaultCloseOperation(EXIT_ON_CLOSE); firstPanel = new JPanel(); super.getContentPane().add(firstPanel, BorderLayout.CENTER); this.setExtendedState(MAXIMIZED_BOTH); this.setVisible(true); } protected JMenuBar createMenuBar(){ //Create the menu bar = new JMenuBar(); title = new JMenu("Game"); newGame = new JMenuItem("New Game"); options = new JMenu("Options"); exit = new JMenuItem("Exit"); menuHelp = new JMenu("Help"); buttonOn = new JRadioButtonMenuItem("Music:ON"); buttonOff = new JRadioButtonMenuItem("Music:OFF"); group = new ButtonGroup(); group.add(buttonOn); group.add(buttonOff); options.add(buttonOn); options.add(buttonOff); title.add(newGame); title.add(options); title.addSeparator(); title.add(exit); bar.add(title); bar.add(menuHelp); //Listeners newGame.addActionListener(this); options.addActionListener(this); exit.addActionListener(this); menuHelp.addActionListener(this); return bar; } @Override public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source==newGame){//Upload the game and dispose the menu's frame game = new GUI(); this.setVisible(false); } else if(source==options){ JTextArea textArea = new JTextArea(6, 25); textArea.setText(longMessage); textArea.setEditable(false); // wrap a scrollpane around it JScrollPane scrollPane = new JScrollPane(textArea); // display them in a message dialog JOptionPane.showMessageDialog(GameMenu.this, scrollPane); } else if(source==exit){ System.exit(0); } else{ } } public static void main(String[] args) { new GameMenu(); } }

Rispondi quotando

