gpt4 book ai didi

java - JTextField 在给出输入之前被读取

转载 作者:行者123 更新时间:2023-11-30 07:07:41 25 4
gpt4 key购买 nike

我做了一个简单的尝试来制作一个商店计费应用程序。现在一切都很好,但我现在遇到了一个问题。原始源代码太大,因此可能会造成困惑。所以我制作了一个示例代码来描述我的问题。这是代码。

package experiement;

import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.awt.event.KeyListener;

public class Experiment extends JFrame implements ActionListener,KeyListener {

ArrayList<JTextField> fruitsindex;
ArrayList<JLabel>fruits;
int serial =0;
public Experiment(){
fruitsindex=new ArrayList<JTextField>();
fruits=new ArrayList<JLabel>();
addKeyListener(this);
fruits.add(new JLabel("Apple"));
fruits.add(new JLabel("Orange"));
fruits.add(new JLabel("Mango"));
fruits.add(new JLabel("Pomegranate"));
fruits.add(new JLabel("Guava"));
fruits.add(new JLabel("Papapya"));
fruits.add(new JLabel("Banana"));

setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
Experiment e=new Experiment();
}

@Override
public void actionPerformed(ActionEvent e) {}

@Override
public void keyTyped(KeyEvent e) {}

@Override
public void keyPressed(KeyEvent e) {
int c=e.getKeyCode();
switch(c){
case KeyEvent.VK_ENTER:
fruitsindex.add(new JTextField(15));
for(int i=0;i<fruitsindex.size();i++){
fruitsindex.get(i).setBounds(30,20*i,50,25);
getContentPane().add(fruitsindex.get(i));
}
getContentPane().repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent e){}
}

因此,当我按 Enter 键时,会创建一个新的 JTextField,我可以在其中输入 fruits ArrayList 的索引。但我想创建 JLabel 显示与 JTextField 中键入的索引相对应的水果名称。有什么办法可以完成这个任务吗?

最佳答案

请参阅以下内容并根据需要要求澄清:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Experiment extends JFrame {

ArrayList<JTextField> fruitsindex;
ArrayList<String>fruits; //use to keep fruit name
int serial =0;

private JTextField userInput;
private JLabel fruitName;

public Experiment(){

fruits=new ArrayList<String>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
fruits.add("Pomegranate");
fruits.add("Guava");
fruits.add("Papapya");
fruits.add("Banana");

//initialize user input JtextFiield
userInput = new JTextField(2);
userInput.setPreferredSize(new Dimension(15,40));
userInput.setToolTipText("Enter fruit index (0-6)");
//add key listener
userInput.addKeyListener(new KeyListener() {

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}

@Override
public void keyPressed(KeyEvent e) {
int c=e.getKeyCode();
if(c == KeyEvent.VK_ENTER) {
processInput();
}
}

});
add(userInput, BorderLayout.WEST);

//initialize fruit name label
fruitName = new JLabel("Enter fruit index");
fruitName.setPreferredSize(new Dimension(140,40));
add(fruitName, BorderLayout.EAST);

pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

private void processInput() {

//error msg will be over written if input is valid
fruitName.setText("Wrong input");

try {
String inputString = userInput.getText();
int inputInt = Integer.valueOf(inputString);
if((inputInt >=0) && (inputInt < fruits.size())) {

fruitName.setText(fruits.get(inputInt));
userInput.setText(""); //clear input

}
} catch (NumberFormatException ex) {
ex.printStackTrace();
}

repaint();
}

public static void main(String[] args) {
//no need to keep a reference if you don't use it
//Experiment e=
new Experiment();
}
}

关于java - JTextField 在给出输入之前被读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com