Java中的ActionListeners
好的,我有两个类,它们之间并没有真正的联系。 一个是图形,另一个是输入(通过终端使用扫描仪)。我想用JTextField替换扫描仪,但我很难做到这一点 我在这里有点迷路了 这是类GUIJava中的ActionListeners,java,Java,好的,我有两个类,它们之间并没有真正的联系。 一个是图形,另一个是输入(通过终端使用扫描仪)。我想用JTextField替换扫描仪,但我很难做到这一点 我在这里有点迷路了 这是类GUI //Constructor to create the UI components public UnoGraphics() { //JButtons---------------------------- viewCards = new JButton("Move Card"); input
//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-----
}
}
}
公共类CommandLinePlayer扩展了播放器
{
//私有字符串输入文本;
//建造师
公共命令行播放器(字符串名称)
{
//超级aname来自超级级玩家;
超级(阿纳姆);
}
//命令行播放器也可以说uno。这使用扫描器(从键盘读取用户输入),并且应该输入响应
公共布尔值sayUno()
{
System.out.println(“您想说uno吗?是还是否”);
扫描仪扫描=新扫描仪(System.in);
字符串yes=scan.next();
//返回响应
//如果用户键入yes并忽略大小写,则返回yes
返回yes.equalsIgnoreCase(“yes”);
}
//此方法是int类型的choose card接受int类型card的一个参数
//命令行版本(命令行上的普通播放器)
//此方法从您的手上接收卡并将其“发送”到控制器
受保护的int chooseCard(卡顶卡)
{
//显示手
System.out.println(“\n她是topCard:“+topCard”);
System.out.println(“你的手有:”);
//循环玩家(commandlineplayer)的手牌并打印出玩家的牌。索引可以从0开始,但1将是第一张牌
对于(int index=0;index
以下是您需要的相关代码块,您应该能够自己将它们集成到代码中
public class Controller {
public void startMethod() {
final UIClass myUI = new UIClass();
myUI.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
handleUIInformation(myUI);
}
}
}
private void handleUIInformation(UIClass myUI) {
String textval = myUI.textField.getText();
// here you do whatever you want with the text
}
}
public class UIClass {
JButton button;
JTextField textField;
public UIClass() {
button = new JButton();
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
button.doClick();
}
}
});
}
public void addActionListener(ActionListener al) {
button.addActionListener(al);
}
}
你能缩小你的问题范围吗?你在做什么?用JTextField(input)替换扫描器并将其添加到我的GUI(UnoGraphics())。什么是“扫描器”?将一张纸数字化后从OCR输入的文本?对不起,扫描仪扫描=新扫描仪(System.in);扫描仪从输入流中读取文本,例如stdin、文件和套接字。我得到了它。感谢您的帮助,我是GUI新手,希望了解更多关于它们的信息。再次感谢您使用ActionListener我们不必使用.actionPerformed(ActionEvent)?我在这里遇到一个错误myUI.addActionListener()它说我必须使用actionPerformed(ActionEvent)