我正在尝试使用ActionListener设置TextFields数组的文本。我已经设置了主类,并设置了ActionListener类。我相信我的问题是在我的主类中使用for循环生成多个JTextField来设置ActionListener,但我不确定。这是我的主要课程: 包lab5.pkg2;
import java.awt.BorderLayout;
import java.awt.Panel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Lab52
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
Panel nPanel = new Panel();
frame.add(nPanel, BorderLayout.NORTH);
Panel sPanel = new Panel();
frame.add(sPanel, BorderLayout.SOUTH);
JTextField[] tf = new JTextField[10];
for(int k = 0; k < tf.length; k++)
{
tf[k] = new JTextField(4);
nPanel.add(tf[k]);
}
RandListener listen = new RandListener(tf);
JButton bRand = new JButton("Randomize");
bRand.setActionCommand("bRand");
bRand.addActionListener(listen);
sPanel.add(bRand);
JButton bMaxMin = new JButton("Max, Min");
bRand.setActionCommand("bMaxMin");
bRand.addActionListener(listen);
sPanel.add(bMaxMin);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是我的ActionListener类:
class RandListener implements ActionListener
{
private final JTextField[] tf = new JTextField[10];
public RandListener(JTextField[] tF)
{
tf[0] = tF[0];
tf[1] = tF[1];
tf[2] = tF[2];
tf[3] = tF[3];
tf[4] = tF[4];
tf[5] = tF[5];
tf[6] = tF[6];
tf[7] = tF[7];
tf[8] = tF[8];
tf[9] = tF[9];
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("bRand"))
{
for(int k = 0; k < 10; k++)
{
Random rNum = new Random();
tf[k].setText(rNum.toString());
}
}
if(e.getActionCommand().equals("bMaxMin"))
{
}
else
{
JOptionPane.showMessageDialog(null, "Error");
}
}
}
答案 0 :(得分:0)
代码似乎现在正在运行;)我在代码中写了一些注释,其中包含了我已经改变的内容。
import java.awt.BorderLayout;
import java.awt.Panel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Lab52
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
Panel nPanel = new Panel();
frame.add(nPanel, BorderLayout.NORTH);
Panel sPanel = new Panel();
frame.add(sPanel, BorderLayout.SOUTH);
JTextField[] tf = new JTextField[10];
for(int k = 0; k < tf.length; k++)
{
tf[k] = new JTextField(4);
nPanel.add(tf[k]);
}
RandListener listen = new RandListener(tf);
JButton bRand = new JButton("Randomize");
bRand.setActionCommand("bRand");
bRand.addActionListener(listen);
sPanel.add(bRand);
JButton bMaxMin = new JButton("Max, Min");
bMaxMin.setActionCommand("bMaxMin"); // These 2 lines were refering to bRand, not to bMaxMin
bMaxMin.addActionListener(listen);
sPanel.add(bMaxMin);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
您的Listener类有一些简化 - 在构造函数中 - 在bRand的if中我生成一个随机整数,我不确定这是不是你的目标。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class RandListener implements ActionListener
{
private final JTextField[] tF;
public RandListener(JTextField[] tF)
{
this.tF = tF; // I simplified your code here
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("bRand"))
{
for(int k = 0; k < tF.length; k++)
{
Random rNum = new Random();
tF[k].setText(rNum.nextInt() + ""); // Not sure if you wanted a random number..
}
}
if(e.getActionCommand().equals("bMaxMin"))
{
}
else
{
JOptionPane.showMessageDialog(null, "Error");
}
}
}