运行我的代码时收到以下错误。
错误:不兼容的类型:double无法转换为JTextField
JGrasp指出以下问题。
Programming6_4.Java:143:错误:不兼容的类型:double不能 转换为JTextField averageScoreTF =(test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4); ^ 1错误
不确定为什么会标记' +'作为无法转换的项目。 任何帮助将不胜感激。
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Programming6_4 extends JFrame
{
//declares variables
private JLabel blankGrid1L, blankGrid2L, blankGrid3L, scoreL, weightL, testOneL, testTwoL, testThreeL, testFourL, averageScoreL;
private JTextField testOneTF, testTwoTF, testThreeTF, testFourTF, testOnewTF, testTwowTF, testThreewTF, testFourwTF, averageScoreTF;
private JButton calculateScoreB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
//declare window size
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
public Programming6_4()
{
//name labels
blankGrid1L = new JLabel ("", SwingConstants.CENTER);
blankGrid2L = new JLabel ("", SwingConstants.CENTER);
blankGrid3L = new JLabel ("", SwingConstants.CENTER);
scoreL = new JLabel ("Score", SwingConstants.CENTER);
weightL = new JLabel ("Weight", SwingConstants.CENTER);
testOneL = new JLabel ("Test One Score: ", SwingConstants.CENTER);
testTwoL = new JLabel ("Test Two Score: ", SwingConstants.CENTER);
testThreeL = new JLabel ("Test Three Score: ", SwingConstants.CENTER);
testFourL = new JLabel ("Test Four Score: ", SwingConstants.CENTER);
averageScoreL = new JLabel ("Average Weighted Test Score: ", SwingConstants.CENTER);
//create text fields 10 characters - Test Scores
testOneTF = new JTextField(10);
testTwoTF = new JTextField(10);
testThreeTF = new JTextField(10);
testFourTF = new JTextField(10);
//create text fields 10 charaters - Weights
testOnewTF = new JTextField(10);
testTwowTF = new JTextField(10);
testThreewTF = new JTextField(10);
testFourwTF = new JTextField(10);
averageScoreTF = new JTextField(10);
//create buttons and handler
calculateScoreB = new JButton ("Calculate");
cbHandler = new CalculateButtonHandler();
calculateScoreB.addActionListener(cbHandler);
exitB = new JButton ("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//set grid size
Container pane = getContentPane();
pane.setLayout(new GridLayout (7, 3));
//add elements to grid spaced by line
pane.add (blankGrid1L);
pane.add (scoreL);
pane.add (weightL);
pane.add (testOneL);
pane.add (testOneTF);
pane.add (testOnewTF);
pane.add (testTwoL);
pane.add (testTwoTF);
pane.add (testTwowTF);
pane.add (testThreeL);
pane.add (testThreeTF);
pane.add (testThreewTF);
pane.add (testFourL);
pane.add (testFourTF);
pane.add (testFourwTF);
pane.add (averageScoreL);
pane.add (averageScoreTF);
pane.add (blankGrid2L);
pane.add (calculateScoreB);
pane.add (blankGrid3L);
pane.add (exitB);
//set window title, window, and display
setTitle ("Weighted Test Average Calculation");
setSize (WIDTH, HEIGHT);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
//setLayout (new Programming6_4());
}
//activates ActionListener
private class CalculateButtonHandler implements ActionListener
//Main calculation program
{
public void actionPerformed (ActionEvent e) //public static void main (String[] args)
{
//declare varibles in
double test1, test2, test3, test4, wght1, wght2, wght3, wght4, avgscore /*avg1, avg2, avg3, avg4*/;
//takes text entered by user and converts from string to decimal
test1 = Double.parseDouble(testOneTF.getText());
wght1 = Double.parseDouble(testOnewTF.getText());
test2 = Double.parseDouble(testTwoTF.getText());
wght2 = Double.parseDouble(testTwowTF.getText());
test3 = Double.parseDouble(testThreeTF.getText());
wght3 = Double.parseDouble(testThreewTF.getText());
test4 = Double.parseDouble(testFourTF.getText());
wght4 = Double.parseDouble(testFourwTF.getText());
//calculations needed to get weighted average
//avg1 = test1 * wght1;
//avg2 = test2 * wght2;
//avg3 = test3 * wght3;
//avg4 = test4 * wght4;
//avgscore = avg1 + avg2 + avg3 + avg4;
averageScoreTF = (test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4);
averageScoreTF.setText("" + avgscore);
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
public static void maine (String[]args)
{
Programming6_4 testObject = new Programming6_4();
}
}
答案 0 :(得分:2)
更改此行:
averageScoreTF = (test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4);
为:
avgscore = (test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4);
由于averageScoreTF
不是数字类型(就像错误消息所说的那样),因此无法为averageScoreTF
分配双精度。
答案 1 :(得分:0)
你显然很困惑 - 双数是一个数字,jtextfield是一个GUI小部件。 您很可能希望将您的号码打印到文本字段中进行演示。