Java 投票计数器错误
我的老师给了我们2节课的代码。一个设置为JFrame,另一个设置为class,用于制作红色按钮并计算其被按下的次数。他希望我们添加代码来制作一个蓝色按钮,编辑程序来计算红色按钮和蓝色按钮分别被按下的次数。然后他还希望我们添加一条消息;比如说如果瑞德赢了,他想让我们在JFrame上加上一句话“瑞德赢了”,再加上蓝赢或者平局。代码在下面,请帮助我Java 投票计数器错误,java,swing,Java,Swing,我的老师给了我们2节课的代码。一个设置为JFrame,另一个设置为class,用于制作红色按钮并计算其被按下的次数。他希望我们添加代码来制作一个蓝色按钮,编辑程序来计算红色按钮和蓝色按钮分别被按下的次数。然后他还希望我们添加一条消息;比如说如果瑞德赢了,他想让我们在JFrame上加上一句话“瑞德赢了”,再加上蓝赢或者平局。代码在下面,请帮助我 //********************************************************* // VoteCounter.ja
//*********************************************************
// VoteCounter.java
//
// Demonstrates a graphical user interface and event
// listeners to tally votes for two candidates, Red and Blue.
// [modified from a lab assignment in the Lewis/Loftis lab manual]
//*********************************************************
import javax.swing.JFrame;
public class VoteCounter
{
//----------------------------------------------
// Creates the main program frame.
//----------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("Vote Counter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new VoteCounterPanel());
frame.pack();
frame.setVisible(true);
}
}
public class VoteCounterPanel extends JPanel
{
private int votesForRed;
private JButton red;
private JLabel labelRed;
//**********************
//Added Blue Varriables
//**********************
private int votesForBlue;
private JButton blue;
private JLabel labelBlue;
//************************
//Added Winner Varriables
//************************
private int Winning;
private JLabel labelWinning;
private int vote;
//----------------------------------------------
// Constructor: Sets up the GUI.
//----------------------------------------------
public VoteCounterPanel()
{
votesForRed = 0;
red = new JButton("Vote for Red");
red.addActionListener(new VoteButtonListener());
labelRed = new JLabel("Votes for Red: " + votesForRed);
add(red);
add(labelRed);
setPreferredSize(new Dimension(300, 40));
setBackground(Color.cyan);
//***************************************************
// Represents a listener for button push (action) events
//***************************************************
votesForBlue = 0;
blue = new JButton("Vote for Blue");
blue.addActionListener(new VoteButtonListener());
labelBlue = new JLabel("Votes for Blue: " + votesForBlue);
add(blue);
add(labelBlue);
setPreferredSize(new Dimension(300, 70));
setBackground(Color.cyan);
//******************************************************
//Added code for winning
//******************************************************
if(votesForBlue > votesForRed)
{
labelWinning = new JLabel("Blue is winning");
setPreferredSize(new Dimension(300, 100));
setBackground(Color.cyan);
}
else if(votesForRed > votesForBlue)
{
labelWinning = new JLabel("Red is winning");
setPreferredSize(new Dimension(300, 100));
setBackground(Color.cyan);
}
else
{
labelWinning = new JLabel("It's a tie");
setPreferredSize(new Dimension(300, 100));
setBackground(Color.cyan);
}
}
private class VoteButtonListener implements ActionListener
{
//----------------------------------------------
// Updates the appropriate vote counter when a
// button is pushed for one of the candidates.
//----------------------------------------------
public void actionPerformed(ActionEvent event)
{
vote = ActionEvent;
if()
{
votesForRed++;
labelRed.setText("Votes for Red: " + votesForRed);
}
else
{
votesForBlue++;
labelBlue.setText("Votes for Blue: " + votesForBlue);
}
}
}
}
在构造函数中,您有条件逻辑来显示谁赢了。问题是,在接受任何用户输入之前都会运行构造函数;因此,该逻辑不会被动态调用 将显示谁获胜的逻辑移动到已执行的操作中,以更新投票计数,然后检查是否需要更改“谁获胜”显示元素 --为更新而编辑-- 就它们都更新错误而言,这可能是因为您正在更新actionPerformed方法中的两个计数器。很可能你想从你的行为中读出一些字段,并且只更新一个
当前,粘贴的代码将无法编译,因为Java中不允许使用空条件;所以,我想不管你最后一次遵守了什么,你都会通过增加红色和蓝色计数器来处理操作。错误是什么?谁获胜的逻辑在构造函数中,它只会被调用一次,在用户输入之前更新who's winning display元素,并且在接受输入之后从不更新这些元素。哦,很抱歉:P两个错误,一个,蓝色和红色按钮上的计数器同时添加,尝试添加if-else语句,但我不熟悉actionListener。其次,我无法显示“谁是赢家”消息