java 中如何改变按钮数组中指定按钮的文字? 15
注释中的代码与本程序无关,是我拿来随便测试的。我想知道怎么更改那行代码(注释是这行代码删掉程序就不会出错)才能使程序实现如下功能:鼠标移到写着"Clickme!"的按钮上...
注释中的代码与本程序无关,是我拿来随便测试的。我想知道怎么更改那行代码(注释是这行代码删掉程序就不会出错)才能使程序实现如下功能:鼠标移到写着"Click me !"的按钮上,则会随机将另一个按钮的文本改成“Click me !”并将原来的按钮文本置空。新的Click me 按钮也会有上述行为。
public class trial extends JFrame implements MouseListener{
private String s1 = "Click me!";
private JButton[] btns;
/*JButton jbtOK= new JButton("ok"); */
public trial() {
int index = (int)(Math.random()*9);
this.setLayout(new GridLayout(3,3));
JButton[] btns = new JButton[9];
for (int i = 0; i<9;i++) {
if(i == index ) {btns[i] = new JButton(s1);/*btns[i].addMouseListener(this);*/}
else
btns[i] = new JButton();
btns[i].addMouseListener(this);
this.add(btns[i]);
}
/*jbtOK.addActionListener(this);
this.getContentPane().add(jbtOK);*/
this.setTitle("Catch me if you can !");
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/*public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jbtOK))
System.out.println("OK button is clicked");
}*/
public void mouseEntered(MouseEvent e) {
/*if (((JButton) (e.getSource())).getText().equals("Click m !"));
System.out.println("Mouse entered");*/
String s2 = ((JButton) (e.getSource())).getText() ;
if (s1.equals(s2)) {
int i = (int)(Math.random()*9);
((JButton) (e.getSource())).setText(" ");
btns[i].setText(s1);//这行代码删掉程序就不会出错
System.out.println("Mouse entered!");
}
/* System.out.println("你点击的按钮上的文字是"+((JButton) (e.getSource())).getText() );*/
}
public void mousePressed(MouseEvent e){
}
public void mouseRelease(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public static void main(String[] args) {
trial a = new trial();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
} 展开
public class trial extends JFrame implements MouseListener{
private String s1 = "Click me!";
private JButton[] btns;
/*JButton jbtOK= new JButton("ok"); */
public trial() {
int index = (int)(Math.random()*9);
this.setLayout(new GridLayout(3,3));
JButton[] btns = new JButton[9];
for (int i = 0; i<9;i++) {
if(i == index ) {btns[i] = new JButton(s1);/*btns[i].addMouseListener(this);*/}
else
btns[i] = new JButton();
btns[i].addMouseListener(this);
this.add(btns[i]);
}
/*jbtOK.addActionListener(this);
this.getContentPane().add(jbtOK);*/
this.setTitle("Catch me if you can !");
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/*public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jbtOK))
System.out.println("OK button is clicked");
}*/
public void mouseEntered(MouseEvent e) {
/*if (((JButton) (e.getSource())).getText().equals("Click m !"));
System.out.println("Mouse entered");*/
String s2 = ((JButton) (e.getSource())).getText() ;
if (s1.equals(s2)) {
int i = (int)(Math.random()*9);
((JButton) (e.getSource())).setText(" ");
btns[i].setText(s1);//这行代码删掉程序就不会出错
System.out.println("Mouse entered!");
}
/* System.out.println("你点击的按钮上的文字是"+((JButton) (e.getSource())).getText() );*/
}
public void mousePressed(MouseEvent e){
}
public void mouseRelease(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public static void main(String[] args) {
trial a = new trial();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询