gpt4 book ai didi

java - 错误: incompatible types: double cannot be converted to JTextField

转载 作者:行者123 更新时间:2023-11-30 03:43:44 26 4
gpt4 key购买 nike

我在运行代码时收到以下错误。

error: incompatible types: double cannot be converted to JTextField

JGrasp 指出以下问题。

Programming6_4.Java:143: error: incompatible types: double cannot be converted to JTextField averageScoreTF = (test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4); ^ 1 error

不知道为什么它将“+”标记为无法转换的项目。任何帮助将不胜感激。

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();
}
}

最佳答案

更改此行:

averageScoreTF = (test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4);

至:

avgscore = (test1 * wght1 + test2 * wght2 + test3 * wght3 + test4 * wght4);

您不能将 double 分配给 averageScoreTF,因为 averageScoreTF 不是数字类型(就像错误消息所述)。

关于java - 错误: incompatible types: double cannot be converted to JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249118/

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