我正在为学校做一个项目,我需要将数据从文本框传输到标签'数据'?我该怎么办?这是我的代码,这个项目的工作是在该课程的前一个实验室完成的,因此听众下的代码不适用于本实验。我只需要帮助" EDlistener"。我尝试了但是得到了" java:89:错误:无法接受的陈述",请回答我的想法,我只是一个初学者。感谢。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class panelFinal extends JPanel
{
private JLabel label2;
private JLabel labelData, labelMean, LabelStan; //field
private JTextField box; //field
private JCheckBox click;
private int dataNumber;
private String dataStr[] = new String [50];
private int data1[] = new int [50];
private int data2[] = new int [51];
private String dataString;
public panelFinal()
{
setLayout (null);
JButton EnterData = new JButton ("Enter Data");
EnterData.addActionListener (new EDListener());
EnterData.setBounds(10,10,100,40);
add(EnterData);
JButton Statistics = new JButton ("Statistics");
Statistics.addActionListener (new SListener ());
Statistics.setBounds(120,10,100,40);
add(Statistics);
JButton Graphdata = new JButton ("Graph data");
Graphdata.addActionListener (new GListener ());
Graphdata.setBounds(230,10,100,40);
add(Graphdata);
JButton quit = new JButton ("Quit");
quit.addActionListener (new quitListener ());
quit.setBounds(340,10,100,40);
add(quit);
box = new JTextField ("", 60);
box.setHorizontalAlignment (SwingConstants.LEFT);
box.setBounds(10,55,450,22);
add(box);
labelData = new JLabel ("Data:");
labelData.setHorizontalAlignment (SwingConstants.LEFT);
labelData.setForeground(Color.blue);
labelData.setFont(new Font("Serif", Font.BOLD, 20));
labelData.setBounds(10,80,200,20);
add(labelData);
labelMean = new JLabel ("Mean:");
labelMean.setHorizontalAlignment (SwingConstants.LEFT);
labelMean.setForeground(Color.blue);
labelMean.setFont(new Font("Serif", Font.BOLD, 20));
labelMean.setBounds(10,115,200,20);
add(labelMean);
LabelStan = new JLabel ("Standard Deviation:");
LabelStan.setHorizontalAlignment (SwingConstants.LEFT);
LabelStan.setForeground(Color.blue);
LabelStan.setFont(new Font("Serif", Font.BOLD, 20));
LabelStan.setBounds(10,145,200,20);
add(LabelStan);
}
private class EDListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String test;
test = box.getText();
if ( test.equals("") );
return;
String dataString = "Data:";
dataStr = box.getText().split("\\a");
dataNumber = dataStr.length;
for (int i=0; i<=dataNumber-1; i++)
{
data1[i] = Integer.parseInt(dataStr[i]);
if (data1[i] < 50)
data1[i] = 50;
dataString = dataString + dataStr[i];
if (i != dataNumber-1)
dataString = dataString + ", ";
}
labelData.setText(dataString);
repaint();
}
}
答案 0 :(得分:0)
您可以在文本字段上使用getText()方法将文本作为字符串使用,并在标签上使用setText(String)来设置它。