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