java大作业 下面是我的代码,求大神帮忙添加一个重新开始的按钮并实现这个按钮的功能

packagedictionary;importjava.awt.Dimension;importjava.awt.FlowLayout;importjava.awt.e... package dictionary;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class yingyu extends JFrame implements ActionListener{
private JTextField word;
private JLabel label;
private JButton next;
private int index = 0;
private String[] words;
private int score = 0;
public yingyu(){
words = new String[] {"苹果", "香蕉", "狗", "猫", "水"};
word = new JTextField();
Dimension size = new Dimension(60, 30);
word.setPreferredSize(size );
label = new JLabel(words[0]);

next = new JButton("下一道");
next.addActionListener(this);
this.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
this.add(label);
this.add(word);
this.add(next);
this.setBounds(100, 100, 300, 300);
this.setVisible(true);

}public static void main(String[] args){
new yingyu();
}
public void reload(){
label.setText(words[index]);
repaint();
}
展开
 我来答
艾来木尼亚孜
推荐于2016-09-20 · 超过76用户采纳过TA的回答
知道小有建树答主
回答量:124
采纳率:0%
帮助的人:104万
展开全部
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class yingyu extends JFrame {
private JTextField word;
private JLabel label;
private JButton next;
private JButton restart;
private int index = 0;
int i=1;
private String[] words;
private int score = 0;
public yingyu(){
words = new String[] {"苹果", "香蕉", "狗", "猫", "水"};
word = new JTextField();
Dimension size = new Dimension(60, 30);
word.setPreferredSize(size );
label = new JLabel(words[0]);
next = new JButton("下一道");
restart = new JButton("重新开始");
next.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(i<words.length){
label.setText(words[i]);
i++;}
else {i=1;
label.setText(words[0]);}

}

});
restart.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

label.setText(words[0]);
i=1;

}

});

setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
add(label);
add(word);
add(next);
add(restart);
setBounds(100, 100, 300, 300);
setVisible(true);

}
public static void main(String[] args){
yingyu yingyu =new yingyu();
}
public void reload(){
label.setText(words[index]);
repaint();
}

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式