好吧,所以我有兩個類沒有真正相互關聯。 一個是輸入中的圖形(通過終端使用掃描儀)。我想用一個JTextField來代替掃描儀,但我有一個很難這樣做..Java中的ActionListeners
林有點丟在這裏....
這裏是一流的GUI
//Constructor to create the UI components
public UnoGraphics() {
//JButtons----------------------------
viewCards = new JButton("Move Card");
input = new JTextField(5);
//Creates a canvas and set the properties
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH,CANVAS_HEIGHT));
this.setContentPane(canvas);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//This is How I was thinking of implementing my input------------HERE---------------->
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText = input.getText();
}});//End ActionListener()
this.pack();
this.setTitle("Uno!");
this.setVisible(true);
}//End Constructor
//Custom drawing canvas (designed as inner class). This is were we draw/change the cards
class DrawCanvas extends JPanel {
// Custom drawing codes--------------
@Override
public void paintComponent(Graphics g) {
//Set the background to black
super.paintComponent(g);
setBackground(Color.black);
//cards being drawn-------------------
//buttons and text fields
add(viewCards);
add(input);
}//End PaintComponent()
}//End DrawCavas()
}//End Program
二班使用掃描儀的輸入:
public class CommandLinePlayer extends Player
{
//private String inputText;
// constructor
public CommandLinePlayer(String aname)
{
//super aname is from the super class player;
super(aname);
}
// command line player can also say uno. This uses scanner(reads user in puts from keyboard) and the response should be typed in
public boolean sayUno()
{
System.out.println("Would you like to say uno? Yes or No");
Scanner scan = new Scanner(System.in);
String yes = scan.next();
// returns response
//returns yes if the user types yes and ignores the case
return yes.equalsIgnoreCase("Yes");
}
//this method is the choose card of type int takes int one argument of type Card
// command line version (normal player on command line)
//this method takes in the card from your hand and "sends" it to the controller
protected int chooseCard(Card topCard)
{
// display hand
System.out.println("\nHere is the topCard: " + topCard);
System.out.println("Your hand has:");
// loops through the players(commandlineplayer) hand and prints out the players cards. Index could start at 0, but 1 would be the first card
for(int index = 0; index < numOfCards; index ++)
{
System.out.println("Card # " + index + ": " + hand[index]);
}
// choose Card prompts the player to match, or pick a card based on the index, and then press enter.
// if a card does not match the topcard, a key corresponding to any card can be pressed. This would automatically add a
// card to a players hand.
System.out.println("Play a card #. If you don't have a card to play, choose any card # to draw.");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
return num;
}
// this is the choose color method for the command line player but only if it is a wild card does this method takes place
// command line player can choose a cards color based on the options displayed on the screen.(System.out.println...statements)
public Card.Color chooseColor()
{
// choose a color using scanner
Scanner scanin = new Scanner(System.in);
System.out.println("Choose a color by pressing a number corresponding to your choice:");
System.out.println("Your options are 1.Red 2.Green 3.Yellow 4. Blue");
// the switch corresponds a number (color) to the cases, and returns a chosen card.
int color = 0;
color = scanin.nextInt();
switch (color)
{
case 1: System.out.println("The color you chose is: Red");
return Card.Color.Red;
case 2: System.out.println("The color you chose is: Green");
return Card.Color.Green;
case 3: System.out.println("The color you chose is: Yellow");
return Card.Color.Yellow;
case 4: System.out.println("The color you chose is: Blue");
return Card.Color.Blue;
default: System.out.println("NONE");
}
return Card.Color.None;
}
public class inputListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
//I Was Thinking something like this-----------HERE-----
}
}
}
你能縮小你的問題嗎?你有什麼困難? –
用JTextField(輸入)替換掃描儀並將其添加到我的GUI(UnoGraphics())。 – Tags
什麼是「掃描儀」?數字化一張紙後從OCR輸入文字? –