/** * Adds a popup menu. * * @param label the label * @param items the menu items * @param defaultItem the menu item initially selected */ public void addChoice(String label, String[] items, String defaultItem) { String label2 = label; if (label2.indexOf('_') != -1) label2 = label2.replace('_', ' '); Label theLabel = makeLabel(label2); c.gridx = 0; c.gridy = y; c.anchor = GridBagConstraints.EAST; c.gridwidth = 1; if (choice == null) { choice = new Vector(4); defaultChoiceIndexes = new Vector(4); c.insets = getInsets(5, 0, 5, 0); } else c.insets = getInsets(0, 0, 5, 0); grid.setConstraints(theLabel, c); add(theLabel); Choice thisChoice = new Choice(); thisChoice.addKeyListener(this); thisChoice.addItemListener(this); for (int i = 0; i < items.length; i++) thisChoice.addItem(items[i]); if (defaultItem != null) thisChoice.select(defaultItem); else thisChoice.select(0); c.gridx = 1; c.gridy = y; c.anchor = GridBagConstraints.WEST; grid.setConstraints(thisChoice, c); add(thisChoice); choice.addElement(thisChoice); int index = thisChoice.getSelectedIndex(); defaultChoiceIndexes.addElement(new Integer(index)); if (Recorder.record || macro) saveLabel(thisChoice, label); y++; }
public Channels() { super("Channels"); if (instance != null) { instance.toFront(); return; } WindowManager.addWindow(this); instance = this; GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); int y = 0; c.gridx = 0; c.gridy = y++; c.gridwidth = 1; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; int margin = 32; if (IJ.isMacOSX()) margin = 20; c.insets = new Insets(10, margin, 10, margin); choice = new Choice(); for (int i = 0; i < modes.length; i++) choice.addItem(modes[i]); choice.select(0); choice.addItemListener(this); add(choice, c); CompositeImage ci = getImage(); int nCheckBoxes = ci != null ? ci.getNChannels() : 3; if (nCheckBoxes > CompositeImage.MAX_CHANNELS) nCheckBoxes = CompositeImage.MAX_CHANNELS; checkbox = new Checkbox[nCheckBoxes]; for (int i = 0; i < nCheckBoxes; i++) { checkbox[i] = new Checkbox("Channel " + (i + 1), true); c.insets = new Insets(0, 25, i < nCheckBoxes - 1 ? 0 : 10, 5); c.gridy = y++; add(checkbox[i], c); checkbox[i].addItemListener(this); } c.insets = new Insets(0, 15, 10, 15); c.fill = GridBagConstraints.NONE; c.gridy = y++; moreButton = new Button(moreLabel); moreButton.addActionListener(this); add(moreButton, c); update(); pm = new PopupMenu(); for (int i = 0; i < menuItems.length; i++) addPopupItem(menuItems[i]); add(pm); addKeyListener(IJ.getInstance()); // ImageJ handles keyboard shortcuts setResizable(false); pack(); if (location == null) { GUI.center(this); location = getLocation(); } else setLocation(location); show(); }
/** Création de l'intérieur de la fenêtre */ private void initConteneur() { // le fond fond = getToolkit().getImage("./images/preference/bomber_fond.gif"); tracker = new MediaTracker(this); tracker.addImage(fond, 0); try { tracker.waitForID(0); } catch (Exception a) { System.out.println("Erreur lors chargement de l'image: initConteneur() Preference"); a.printStackTrace(); } // le layout manager GridBagLayout layout = new GridBagLayout(); setLayout(layout); GridBagConstraints c = new GridBagConstraints(); c.weightx = 10; c.weighty = 10; c.ipadx = 10; c.ipady = 10; c.fill = GridBagConstraints.BOTH; // label "Joueurs:" Label lbl_joueurs = new Label("Joueurs:"); lbl_joueurs.setAlignment(Label.CENTER); lbl_joueurs.setFont(new Font("SansSerif", Font.BOLD, 16)); lbl_joueurs.setBackground(Color.black); lbl_joueurs.setForeground(Color.red); c.gridx = 0; c.gridy = 0; c.gridwidth = 6; c.gridheight = 1; c.insets = new Insets(20, 10, 10, 10); c.anchor = GridBagConstraints.SOUTH; layout.setConstraints(lbl_joueurs, c); add(lbl_joueurs); // choix de couleur du joueur ayant l'identifiant 0 choix_couleur0 = new Choice(); choix_couleur0.addItem("rouge"); choix_couleur0.addItem("bleu"); choix_couleur0.addItemListener(this); choix_couleur0.setBackground(Color.red); choix_couleur0.setForeground(Color.black); c.gridx = 0; c.gridy = 1; c.gridwidth = 2; c.gridheight = 1; c.insets = new Insets(10, 10, 5, 20); c.anchor = GridBagConstraints.EAST; layout.setConstraints(choix_couleur0, c); add(choix_couleur0); // nom du joueur ayant l'identifiant 0 txt_joueur0 = new TextField(noms[0]); c.gridx = 2; c.gridy = 1; c.gridwidth = 4; c.gridheight = 1; c.insets = new Insets(10, 10, 5, 20); c.anchor = GridBagConstraints.WEST; layout.setConstraints(txt_joueur0, c); add(txt_joueur0); // choix de couleur du joueur ayant l'identifiant 1 choix_couleur1 = new Choice(); choix_couleur1.addItem("bleu"); choix_couleur1.addItem("rouge"); choix_couleur1.addItemListener(this); choix_couleur1.setBackground(Color.blue); choix_couleur1.setForeground(Color.black); c.gridx = 0; c.gridy = 2; c.gridwidth = 2; c.gridheight = 1; c.insets = new Insets(10, 10, 20, 20); c.anchor = GridBagConstraints.EAST; layout.setConstraints(choix_couleur1, c); add(choix_couleur1); // nom du joueur ayant l'identifiant 1 txt_joueur1 = new TextField(noms[1]); c.gridx = 2; c.gridy = 2; c.gridwidth = 4; c.gridheight = 1; c.insets = new Insets(10, 10, 30, 20); c.anchor = GridBagConstraints.WEST; layout.setConstraints(txt_joueur1, c); add(txt_joueur1); // label "Nombre De Vies:" Label lbl_nbvies = new Label("Nombre De Vies:"); lbl_nbvies.setAlignment(Label.CENTER); lbl_nbvies.setFont(new Font("SansSerif", Font.BOLD, 14)); lbl_nbvies.setBackground(Color.black); lbl_nbvies.setForeground(Color.yellow); c.gridx = 0; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(20, 10, 5, 10); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(lbl_nbvies, c); add(lbl_nbvies); // un choix possible cbg_vies = new CheckboxGroup(); Checkbox cb; for (int i = 1; i <= 5; i++) { if (i == nb_vies) cb = new Checkbox("" + i, cbg_vies, true); else cb = new Checkbox("" + i, cbg_vies, false); c.gridx = i; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(20, 10, 5, 10); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(cb, c); add(cb); } // for // label "Nombre De Victoires:" lbl_victoires = new Label(); lbl_victoires.setAlignment(Label.CENTER); lbl_victoires.setFont(new Font("SansSerif", Font.BOLD, 14)); lbl_victoires.setText("Nombre De Victoires: " + nb_victoires); lbl_victoires.setBackground(Color.black); lbl_victoires.setForeground(Color.green); c.gridx = 0; c.gridy = 6; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(20, 10, 5, 10); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(lbl_victoires, c); add(lbl_victoires); // choix du nombre de victoires entre un 1 et 9 sb_victoires = new Scrollbar(Scrollbar.HORIZONTAL, 1, 1, 1, 10); sb_victoires.setUnitIncrement(1); sb_victoires.setBlockIncrement(2); sb_victoires.addAdjustmentListener(this); sb_victoires.setValue(nb_victoires); c.gridx = 1; c.gridy = 6; c.gridwidth = 5; c.gridheight = 1; c.insets = new Insets(20, 10, 5, 10); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(sb_victoires, c); add(sb_victoires); // tirage aléatoire des niveaux cb_aleatoire = new Checkbox("Niveaux Aléatoires", aleatoire); cb_aleatoire.setFont(new Font("SansSerif", Font.BOLD, 14)); cb_aleatoire.setBackground(Color.black); cb_aleatoire.setForeground(Color.magenta); c.gridx = 0; c.gridy = 8; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(20, 12, 5, 10); c.anchor = GridBagConstraints.EAST; layout.setConstraints(cb_aleatoire, c); add(cb_aleatoire); // label "FPS:" nombre de frames par seconde lbl_fps = new Label(); lbl_fps.setAlignment(Label.CENTER); lbl_fps.setFont(new Font("SansSerif", Font.BOLD, 14)); lbl_fps.setText("FPS: " + fps); lbl_fps.setBackground(Color.black); lbl_fps.setForeground(Color.orange); c.gridx = 0; c.gridy = 10; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(20, 10, 5, 10); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(lbl_fps, c); add(lbl_fps); // choix du nombre de frames par seconde sb_fps = new Scrollbar(Scrollbar.HORIZONTAL, 1, 1, 1, 51); sb_fps.setUnitIncrement(1); sb_fps.setBlockIncrement(10); sb_fps.addAdjustmentListener(this); sb_fps.setValue(fps); c.gridx = 1; c.gridy = 10; c.gridwidth = 5; c.gridheight = 1; c.insets = new Insets(20, 10, 5, 10); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(sb_fps, c); add(sb_fps); // bouton de confirmation button_ok = new Button("Ok"); c.gridx = 0; c.gridy = 12; c.gridwidth = 6; c.gridheight = 1; c.insets = new Insets(20, 5, 20, 5); c.anchor = GridBagConstraints.CENTER; layout.setConstraints(button_ok, c); add(button_ok); button_ok.addActionListener(this); } // initConteneur()
/** Create the whole GUI, and set up event listeners */ public AllComponents(String title) { super(title); // set frame title. // Arrange to detect window close events this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Set a default font this.setFont(new Font("SansSerif", Font.PLAIN, 12)); // Create the menubar. Tell the frame about it. MenuBar menubar = new MenuBar(); this.setMenuBar(menubar); // Create the file menu. Add to menubar. Menu file = new Menu("File"); menubar.add(file); // Create two items for the file menu, setting their label, shortcut, // action command and listener. Add them to File menu. // Note that we use the frame itself as the action listener MenuItem open = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O)); open.setActionCommand("open"); open.addActionListener(this); file.add(open); MenuItem quit = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q)); quit.setActionCommand("quit"); quit.addActionListener(this); file.add(quit); // Create Help menu; add an item; add to menubar // Display the help menu in a special reserved place. Menu help = new Menu("Help"); menubar.add(help); menubar.setHelpMenu(help); // Create and add an item to the Help menu MenuItem about = new MenuItem("About", new MenuShortcut(KeyEvent.VK_A)); about.setActionCommand("about"); about.addActionListener(this); help.add(about); // Now that we've done the menu, we can begin work on the contents of // the frame. Assign a BorderLayout manager with margins for this frame. this.setLayout(new BorderLayout(10, 10)); // Create two panels to contain two columns of components. Use our custom // ColumnLayout layout manager for each. Add them on the west and // center of the frame's border layout Panel column1 = new Panel(); column1.setLayout(new ColumnLayout(5, 10, 2, ColumnLayout.LEFT)); this.add(column1, "West"); Panel column2 = new Panel(); column2.setLayout(new ColumnLayout(5, 10, 2, ColumnLayout.LEFT)); this.add(column2, "Center"); // Create a panel to contain the buttons at the bottom of the window // Give it a FlowLayout layout manager, and add it along the south border Panel buttonbox = new Panel(); buttonbox.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 10)); this.add(buttonbox, "South"); // Create pushbuttons and add them to the buttonbox Button okay = new Button("Okay"); Button cancel = new Button("Cancel"); buttonbox.add(okay); buttonbox.add(cancel); // Handle events on the buttons ActionListener buttonlistener = new ActionListener() { public void actionPerformed(ActionEvent e) { textarea.append("You clicked: " + ((Button) e.getSource()).getLabel() + "\n"); } }; okay.addActionListener(buttonlistener); cancel.addActionListener(buttonlistener); // Now start filling the left column. // Create a 1-line text field and add to left column, with a label TextField textfield = new TextField(15); column1.add(new Label("Name:")); column1.add(textfield); // Handle events on the TextField textfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { textarea.append("Your name is: " + ((TextField) e.getSource()).getText() + "\n"); } }); textfield.addTextListener( new TextListener() { public void textValueChanged(TextEvent e) { textarea.append("You have typed: " + ((TextField) e.getSource()).getText() + "\n"); } }); // Create a dropdown list or option menu of choices Choice choice = new Choice(); choice.addItem("red"); choice.addItem("green"); choice.addItem("blue"); column1.add(new Label("Favorite color:")); column1.add(choice); // Handle events on this choice choice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { textarea.append("Your favorite color is: " + e.getItem() + "\n"); } }); // Create checkboxes, and group them in a CheckboxGroup to give them // "radio button" behavior. CheckboxGroup checkbox_group = new CheckboxGroup(); Checkbox[] checkboxes = new Checkbox[3]; checkboxes[0] = new Checkbox("vanilla", checkbox_group, false); checkboxes[1] = new Checkbox("chocolate", checkbox_group, true); checkboxes[2] = new Checkbox("strawberry", checkbox_group, false); column1.add(new Label("Favorite flavor:")); for (int i = 0; i < checkboxes.length; i++) column1.add(checkboxes[i]); // Handle events on the checkboxes ItemListener checkbox_listener = new ItemListener() { public void itemStateChanged(ItemEvent e) { textarea.append( "Your favorite flavor is: " + ((Checkbox) e.getItemSelectable()).getLabel() + "\n"); } }; for (int i = 0; i < checkboxes.length; i++) checkboxes[i].addItemListener(checkbox_listener); // Create a list of choices. List list = new List(4, true); list.addItem("Java"); list.addItem("C"); list.addItem("C++"); list.addItem("Smalltalk"); list.addItem("Lisp"); list.addItem("Modula-3"); list.addItem("Forth"); column1.add(new Label("Favorite languages:")); column1.add(list); // Handle events on this list list.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { textarea.append("Your favorite languages are: "); String[] languages = ((List) e.getItemSelectable()).getSelectedItems(); for (int i = 0; i < languages.length; i++) { if (i > 0) textarea.append(","); textarea.append(languages[i]); } textarea.append("\n"); } }); // Create a multi-line text area in column 2 textarea = new TextArea(6, 40); textarea.setEditable(false); column2.add(new Label("Messages")); column2.add(textarea); // Create a scrollpane that displays portions of a larger component ScrollPane scrollpane = new ScrollPane(); scrollpane.setSize(300, 150); column2.add(new Label("Scrolling Window")); column2.add(scrollpane); // Create a custom MultiLineLabel with a really big font and make it // a child of the ScrollPane container String message = "/*************************************************\n" + " * AllComponents.java *\n" + " * Written by David Flanagan *\n" + " * Copyright (c) 1997 by O'Reilly & Associates *\n" + " * *\n" + " *************************************************/\n"; MultiLineLabel biglabel = new MultiLineLabel(message); biglabel.setFont(new Font("Monospaced", Font.BOLD + Font.ITALIC, 24)); scrollpane.add(biglabel); }