如何在ComboBox中使用ActionListener

时间:2015-03-06 07:44:25

标签: java button actionlistener



package mainpanel;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.*;

public class MainPanel extends JFrame implements ActionListener {
  String[] deck = null;
  String[] discard = null;
  Player p1;
  Player p2;
  Player p3;
  public String[] playerList = new String[3];
  JPanel panes = new JPanel();
  JPanel playerStatPane = new JPanel();
  public MainPanel() throws FileNotFoundException {

    File masterList = new File("H:/mainPanel/masterList.txt");
    File deckFile = new File("H:/mainPanel/deck.txt");
    File discardFile = new File("H:/mainPanel/discard.txt");
    File player1 = new File("H:/mainPanel/Chip.txt");
    File player2 = new File("H:/mainPanel/Dale.txt");
    File player3 = new File("H:/mainPanel/Caleb.txt");
    deck = extractCards(deckFile);
    if (deck.length == 0) {
      deck = randomCards(extractCards(masterList));
    }
    discard = extractCards(discardFile);
    p1 = new Player(player1);
    p2 = new Player(player2);
    p3 = new Player(player3);
    setTitle("Bang!");
    getContentPane().add(panes);
    JLabel label1 = new JLabel();
    playerList[0] = p1.name;
    playerList[1] = p2.name;
    playerList[2] = p3.name;
    JComboBox comboBox = new JComboBox(playerList);
    panes.add(comboBox);
    comboBox.setSelectedIndex(2);
    comboBox.addActionListener(this);
    playerStatPane = createStats(p1);
    panes.add(playerStatPane);
  }

  public static void main(String[] args) throws FileNotFoundException {
    MainPanel mainScreen = new MainPanel();
    mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainScreen.pack();
    mainScreen.setVisible(true);
  }
  /**public static String[] randomCards(String[] unshuffledCards) {
    int[] tempInts = randomInts(unshuffledCards.length);
    String[] shuffledCards = new String[unshuffledCards.length];
    for (int i = 0; i < tempInts.length; i++) {
      shuffledCards[i] = unshuffledCards[tempInts[i] - 1];
    }
    return shuffledCards;
  }
  public static int[] randomInts(int x) {
    Random numGen = new Random();
    int index = 0;
    boolean repeat = false;
    int[] numberList = new int[x];
    numberList[numberList.length - 1] = 0;
    while (numberList[numberList.length - 1] == 0) {
      int newInt = numGen.nextInt(x) + 1;
      for (int i = 0; i < numberList.length; i++) {
        if (newInt == numberList[i]) {
          repeat = true;
        }
      }
      if (repeat == false) {
        numberList[index] = newInt;
        index++;
      }
      repeat = false;
    }
    return numberList;
  }
  public String[] extractCards(File a) throws FileNotFoundException {
    ArrayList < String > cardsInHand = new ArrayList < String > ();
    Scanner scanner = new Scanner(a);
    while (scanner.hasNextLine()) {
      cardsInHand.add(scanner.nextLine());
    }
    return cardsInHand.toArray(new String[cardsInHand.size()]);
  }
  public void draw(Player a) {
    a.addCard(deck[0]);
    if (deck.length == 1) {
      deck = discard;
      discard = new String[0];
    } else {
      String[] tempDeck = deck;
      deck = new String[deck.length - 1];
      for (int i = 1; i < deck.length; i++) {
        deck[i - 1] = tempDeck[i];
      }
    }
  }*/
  public JPanel createStats(Player a) {
    JPanel stat = new JPanel(new FlowLayout());
    JComboBox comboBox = new JComboBox(a.cardsInHand);
    JLabel label1 = new JLabel("Select card:");
    JButton discardButton = new JButton("discard");
    JButton drawButton = new JButton("draw");
    stat.add(label1);
    stat.add(comboBox);
    stat.add(drawButton);
    stat.add(discardButton);
    return stat;
  }
  public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox) e.getSource();
    String name = (String) cb.getSelectedItem();
    if (name == "Chip") {
      playerStatPane = createStats(p1);
    } else if (name == "Dale") {
      playerStatPane = createStats(p2);
    } else {
      playerStatPane = createStats(p3);

    }
  }



}
&#13;
&#13;
&#13;

呃,我第一次尝试使用ActionListener。我读了几个问题和答案,但我不明白。 非常感谢帮助。我试图通过在组合框中切换玩家名称来切换玩家的信息。 然后我试图获得丢弃并绘制按钮工作。我已经有了discard和draw方法,我只需要学习actionListener的东西。谢谢!

1 个答案:

答案 0 :(得分:0)

您正在重新定位playerStatPane变量,但这并不意味着panes中的旧值已被替换。试试这个:

public void actionPerformed(ActionEvent e) {
    panes.remove(playerStatPane);
    JComboBox cb = (JComboBox) e.getSource();
    String name = (String) cb.getSelectedItem();
    if (name.equals("Chip")) {
      playerStatPane = createStats(p1);
    } else if (name.equals("Dale")) {
      playerStatPane = createStats(p2);
    } else {
      playerStatPane = createStats(p3);
    }
    panes.add(playerStatPane);
  }