简体   繁体   中英

How to use ActionListener in ComboBox

 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); } } } 

Errr, I'm trying to use ActionListener for the first time. I read a couple other question and answers, but I don't understand. Help would be very appreciated. I'm trying to switch information of players by switching player name in the combo box. Then I'm trying to get the discard and draw button to work. I have the discard and draw method already, I just need to learn the actionListener thing. Thanks!

You are reassiging the playerStatPane variable, but that does not mean the old value is replaced in panes . Try this:

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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM