예제 #1
0
파일: SwingView.java 프로젝트: nvans/Games
  /** Creating buttons with coordinates and action listeners */
  private MyButton createButton() {
    // The new buttons counters -
    // will be translates to coordinates
    if (counterX >= size) {
      counterX = 0;
      counterY++;
    }

    MyButton button = new MyButton();

    // set buttons coordinates
    button.x = counterX++;
    button.y = counterY;

    // The buttons properties adjustment
    button.setPreferredSize(new Dimension(50, 50));
    button.setFont(new Font("Dialog", Font.PLAIN, 72));

    // Action listener
    button.addActionListener(
        e -> {
          game.move(new Move(button.x, button.y));
          game.isGameOver();
          button.setText(game.getField()[button.x][button.y] > 0 ? "X" : "O");
          button.setEnabled(false);
          statusStr.setText(game.getState().toString());

          // If the game is over
          // disable all unselected buttons
          if (game.getState().equals(GameState.X_WINS)
              || game.getState().equals(GameState.O_WINS)) {

            for (Component b : gameField.getComponents()) {
              b.setEnabled(false);
            }
          }
        });

    // System.out.println("button " + button.x + " " + button.y);

    return button;
  }
예제 #2
0
  int showOpenDialog(final boolean sa) {

    dialog = new JDialog(frame, JDialog.ModalityType.APPLICATION_MODAL);
    // dialog.setUndecorated(false);

    saveAs = sa;

    dialog.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            dialog.dispose();
          }
        });

    panel = new JPanel();
    panel.setLayout(new BorderLayout());

    driver.curDiag.filterOptions[0] = fCParms.title;
    cBox = new MyComboBox(driver.curDiag.filterOptions);
    cBox.setMaximumRowCount(2);
    cBox.addMouseListener(this);
    cBox.setSelectedIndex(driver.allFiles ? 1 : 0);

    order = new Vector<Component>(9);
    order.add(text);
    order.add(butParent);
    order.add(butNF);
    order.add(panel); // just a place-holder - will be filled in by
    // buildList
    order.add(text2);
    order.add(butCopy);
    order.add(butOK);
    order.add(cBox);
    order.add(butDel);
    order.add(butCancel);

    text.setEditable(true);
    text.setEnabled(true);

    // text.getDocument().addDocumentListener(this);

    text2.setEditable(true);
    text2.setEnabled(true);
    text2.setRequestFocusEnabled(true);
    // text2.addKeyListener(this);
    // text2.getDocument().addDocumentListener(this);
    text2.setPreferredSize(new Dimension(100, driver.fontHeight + 2));

    text3.setEditable(false);
    text3.setEnabled(true);
    // text3.setRequestFocusEnabled(true);
    text3.setFont(driver.fontg.deriveFont(Font.ITALIC));
    // text3.setPreferredSize(new Dimension(100, driver.fontHeight + 2));

    String s = (saveAs) ? "Save or Save As" : "Open File";
    comp = new MyFileCompare();
    renderer = new ListRenderer(driver);

    if (fCParms == driver.curDiag.fCPArr[DrawFBP.DIAGRAM]) dialog.setTitle(s);
    else {
      if (fCParms == driver.curDiag.fCPArr[DrawFBP.GENCODE])
        fCParms.prompt =
            "Specify file name for generated code - for diagram: " + driver.curDiag.title + ".drw";

      dialog.setTitle(fCParms.prompt);
      if (fCParms == driver.curDiag.fCPArr[DrawFBP.CLASS]) listShowingJarFile = listHead;
    }

    enterAction = new EnterAction();
    copyAction = new CopyAction();
    cancelAction = new CancelAction();
    deleteAction = new DeleteAction();

    parentAction = new ParentAction();
    newFolderAction = new NewFolderAction();

    butParent.setAction(parentAction);
    butParent.setText("Parent Folder");
    butParent.setMnemonic(KeyEvent.VK_P);

    butNF.setAction(newFolderAction);
    butNF.setMnemonic(KeyEvent.VK_N);
    butNF.setText("New Folder");

    // butOK.setAction(okAction);
    butOK.setAction(enterAction);
    butCopy.setAction(copyAction);
    butCancel.setAction(cancelAction);
    butDel.setAction(deleteAction);

    butParent.setRequestFocusEnabled(true);
    butNF.setRequestFocusEnabled(true);
    butCopy.setRequestFocusEnabled(true);

    text.addMouseListener(this);
    text2.addMouseListener(this);

    panel.setPreferredSize(new Dimension(600, 600));

    text.setFocusTraversalKeysEnabled(false);
    butParent.setFocusTraversalKeysEnabled(false);
    butNF.setFocusTraversalKeysEnabled(false);
    text2.setFocusTraversalKeysEnabled(false);
    butOK.setFocusTraversalKeysEnabled(false);
    butDel.setFocusTraversalKeysEnabled(false);
    butCancel.setFocusTraversalKeysEnabled(false);
    butCopy.setFocusTraversalKeysEnabled(false);

    butParent.setEnabled(true);
    butNF.setEnabled(true);
    butOK.setEnabled(true);
    // butCopy.setEnabled(saveAs);
    butCopy.setEnabled(true);
    butCancel.setEnabled(true);
    butDel.setEnabled(true);

    KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
    panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(escape, "CLOSE");

    panel.getActionMap().put("CLOSE", cancelAction);

    JLabel label = new JLabel("Current folder: ");
    label.setFont(driver.fontg);

    Box box0 = new Box(BoxLayout.Y_AXIS);
    Box box1 = new Box(BoxLayout.X_AXIS);

    box1.add(label);

    box1.add(Box.createRigidArea(new Dimension(12, 0)));
    box1.add(text);
    box1.add(Box.createRigidArea(new Dimension(6, 0)));

    box1.add(butParent);
    // butParent.addActionListener(this);
    box1.add(Box.createRigidArea(new Dimension(6, 0)));

    // butNF.addActionListener(this);
    box1.add(butNF);

    box0.add(Box.createRigidArea(new Dimension(0, 20)));
    box0.add(box1);

    box0.add(Box.createRigidArea(new Dimension(0, 20)));
    panel.add(box0, BorderLayout.NORTH);

    text.setFont(label.getFont());
    text.addActionListener(this);
    text2.addActionListener(this);

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;

    JPanel pan2 = new JPanel();

    pan2.setLayout(gridbag);
    // c.fill = GridBagConstraints.BOTH;

    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.0;

    JLabel lab1 = new JLabel("File name: ");
    gridbag.setConstraints(lab1, c);
    pan2.add(lab1);

    c.gridx = 1;
    c.weightx = 0.0;
    JLabel lab5 = new JLabel("  ");
    gridbag.setConstraints(lab5, c);
    pan2.add(lab5);

    c.gridx = 2;

    c.weightx = saveAs ? 0.1 : 1.0;
    c.gridwidth = saveAs ? 1 : 3;
    // c.ipadx  = saveAs ? -20: 0;
    gridbag.setConstraints(text2, c);
    pan2.add(text2);

    if (saveAs) {
      c.gridx = 3;
      c.weightx = 0.0;
      c.gridwidth = 1;
      JLabel lab6 = new JLabel("   Suggestion: ");
      gridbag.setConstraints(lab6, c);
      pan2.add(lab6);

      c.gridx = 4;
      c.weightx = 0.9;
      // c.ipadx = 20;
      gridbag.setConstraints(text3, c);
      pan2.add(text3);
      text3.setBackground(Color.WHITE);
      Dimension dim = text3.getPreferredSize();
      text3.setPreferredSize(new Dimension(driver.fontWidth * 25, dim.height));
    }

    c.gridx = 5;
    c.weightx = 0.0;
    JLabel lab7 = new JLabel("  ");
    gridbag.setConstraints(lab7, c);
    pan2.add(lab7);

    c.gridx = 6;
    c.weightx = 0.0;

    if (saveAs) {
      c.gridwidth = 1;
      gridbag.setConstraints(butCopy, c);
      pan2.add(butCopy);

      c.gridx = 7;
      c.weightx = 0.0;

      c.gridwidth = 1;
    } else c.gridwidth = 2;
    gridbag.setConstraints(butOK, c);
    pan2.add(butOK);

    c.weightx = 0.0;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;

    JLabel lab2 = new JLabel("Files of type:");
    gridbag.setConstraints(lab2, c);
    pan2.add(lab2);

    c.gridx = 1;
    c.weightx = 0.0;
    JLabel lab8 = new JLabel("  ");
    gridbag.setConstraints(lab8, c);
    pan2.add(lab8);

    c.gridx = 2;
    c.weightx = 1.0;
    c.gridwidth = 3;
    gridbag.setConstraints(cBox, c);
    pan2.add(cBox);
    cBox.addActionListener(this);

    c.gridx = 5;
    c.weightx = 0.0;
    c.gridwidth = 1;
    JLabel lab9 = new JLabel("  ");
    gridbag.setConstraints(lab9, c);
    pan2.add(lab9);

    c.gridx = 6;
    c.weightx = 0.0;
    gridbag.setConstraints(butDel, c);
    pan2.add(butDel);

    c.gridx = 7;
    c.weightx = 0.0;
    gridbag.setConstraints(butCancel, c);
    pan2.add(butCancel);

    butOK.setText("OK");
    butOK.setFont(driver.fontg.deriveFont(Font.BOLD));
    butCancel.setText("Cancel");
    butDel.setText("Delete");
    butCopy.setText(saveAs ? "Use suggested name" : "");

    JLabel lab3 = new JLabel();
    lab3.setPreferredSize(new Dimension(500, 30));
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 4;
    c.weightx = 1.0;
    gridbag.setConstraints(lab3, c);
    pan2.add(lab3);

    cBox.addActionListener(this);

    // cBox.setUI(new BasicComboBoxUI());
    cBox.setRenderer(new ComboBoxRenderer());

    Dimension dim = new Dimension(1000, 800);
    dialog.setPreferredSize(dim);

    dialog.setFocusTraversalKeysEnabled(false);
    text.addKeyListener(this);
    text2.addKeyListener(this);
    butParent.addKeyListener(this); // needed to service tab keys
    butNF.addKeyListener(this); // needed to service tab keys
    butOK.addKeyListener(this); // needed to service tab keys
    cBox.addKeyListener(this); // needed to service tab keys
    butDel.addKeyListener(this); // needed to service tab keys
    butCancel.addKeyListener(this); // needed to service tab keys
    butCopy.addKeyListener(this); // needed to service tab keys
    cBox.setFocusTraversalKeysEnabled(false);
    mtp = new MyTraversalPolicy();
    setFocusTraversalPolicy(mtp);
    setFocusCycleRoot(false);

    showList();
    if (saveAs) {

      if (suggestedName != null && !(suggestedName.equals(""))) {
        File h = new File(suggestedName);
        listHead = h.getParent();
        text.setText(listHead);
        text2.setText(h.getName());
        text3.setText(h.getName());

        /*
        SwingUtilities.invokeLater(new Runnable() {
        	public void run() {
        		text2.requestFocusInWindow();
        		selComp = text2;
        		text2.setBackground(vLightBlue);
        	}
        });

        */

        text2.addAncestorListener(new RequestFocusListener(false));
        selComp = text2;
        // text2.setBackground(vLightBlue);
        // text2.setEditable(true);
      }

      if (driver.curDiag.title != null && driver.curDiag.diagFile != null) {
        s += " (current file: " + driver.curDiag.diagFile.getAbsolutePath() + ")";
      }
    } else {
      text.setText(listHead);
      /*
      SwingUtilities.invokeLater(new Runnable() {
      	public void run() {
      		list.requestFocusInWindow();
      		selComp = list;
      		// list.setBackground(vLightBlue);
      	}
      });
      */
      // list.addAncestorListener( new RequestFocusListener() );
      selComp = list;
    }

    panel.add(pan2, BorderLayout.SOUTH);
    dialog.add(panel);

    dialog.pack();
    dialog.setLocation(200, 100);
    frame.pack();

    dialog.setVisible(true);

    // if (!saveAs)
    // textBackground = Color.WHITE;

    frame.repaint();

    return result;
  }