我的导师给了我们2个班级的代码。一个设置JFrame
,另一个设置类,用于制作红色按钮并计算按下的次数。他希望我们添加代码来制作蓝色按钮,编辑程序来计算红色按钮和蓝色按钮被单独按下的次数。然后他还希望我们添加一条消息;比如说,如果红色获胜,他希望我们在JFrame
红色获胜的同时添加一个声明,以及蓝色获胜或平局。代码如下,请帮助我!
//*********************************************************
// 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);
}
}
}
}
答案 0 :(得分:1)
在构造函数中,您有条件逻辑来显示谁赢了。问题是在可以接受任何用户输入之前运行构造函数;所以,这个逻辑不会被动态调用。
移动逻辑以显示谁正在赢得actionPerformed
,以这种方式更新投票计数,然后检查是否需要更改“谁赢了”显示元素。
---编辑更新---
就“他们都更新”错误而言,这可能是因为您正在更新actionPerformed
方法中的两个计数器。可能你希望从你的行动中“读取”一些字段,并且只更新一个字段。
目前,您粘贴的代码无法编译,因为Java中不允许使用空条件;所以,我想你无论你上次遵守什么,都会通过递增红色和蓝色计数器来处理动作。