Topic: 实现,JFrame上一JButton,点击JBtton来放大JFrame

  Print this page

1.实现,JFrame上一JButton,点击JBtton来放大JFrame Copy to clipboard
Posted by: 234aini
Posted on: 2004-11-06 11:59

我要实现标题所说的功能,但是参数去却没有办法传递,
请问,怎么样才可以将事件处理程序中的参数传出来呢?

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class jframe {

public static void main(String[] args) {
JFrame jj=new JFrame("adjust");
int size=200;
JButton big=new JButton("BIG");
JButton small=new JButton("SMALL");
big.addActionListener(new ActionListener(){
protected int i=1;
public void actionPerformed(ActionEvent e)
{

System.out.println(i++);
}
});
small.addActionListener(new ActionListener(){
protected int j=20;
public void actionPerformed(ActionEvent e)
{

System.out.println(j++);
}
});
jj.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
});
jj.getContentPane().add(big,"North");
jj.getContentPane().add(small,"South");
jj.setSize(size,size);
jj.setVisible(true);
}
}

2.Re:实现,JFrame上一JButton,点击JBtton来放大JFrame [Re: 234aini] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-11-06 12:04

你想怎样传参数呢,举个例子或说明白。

3.Re:实现,JFrame上一JButton,点击JBtton来放大JFrame [Re: 234aini] Copy to clipboard
Posted by: 234aini
Posted on: 2004-11-06 12:04

我想用i,j来作为方法jj.setsize()的参数,怎么样传递呢?

4.Re:实现,JFrame上一JButton,点击JBtton来放大JFrame [Re: 234aini] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-11-06 12:14

你把 i、j 提到内部类的外面,比如放在main的开头。

public void static main(String[] args){
int i,j;
....
...
}

5.Re:实现,JFrame上一JButton,点击JBtton来放大JFrame [Re: 234aini] Copy to clipboard
Posted by: 234aini
Posted on: 2004-11-06 12:16

C:\eclipse\workspace\net\jframe.java:36: local variable i is accessed from within inner class; needs to be declared final
        System.out.println(i++);
^
C:\eclipse\workspace\net\jframe.java:45: local variable i is accessed from within inner class; needs to be declared final
        System.out.println(i--);
^
2 errors

Process completed.

实验了。报错。上面是错误

6.Re:实现,JFrame上一JButton,点击JBtton来放大JFrame [Re: 234aini] Copy to clipboard
Posted by: 234aini
Posted on: 2004-11-06 12:17

修改后的代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class jframe {

  public static void main(String[] args) {
    JFrame jj=new JFrame("adjust");
    
     int i=200;
    
    JButton big=new JButton("BIG");
    JButton small=new JButton("SMALL");
    big.addActionListener(new ActionListener(){
      
      public void actionPerformed(ActionEvent e)
      {
        
        System.out.println(i++);
            
        }
    });
    small.addActionListener(new ActionListener(){
    
      public void actionPerformed(ActionEvent e)
      {
        
        System.out.println(i--);
      }
    });
    jj.addWindowListener(new WindowAdapter()
    {
      public void WindowClosing(WindowEvent e)
      {
        System.exit(0);
   }
   });  
    jj.getContentPane().add(big,"North");
    jj.getContentPane().add(small,"South");
    jj.setSize(i,i);
    jj.setVisible(true);
  }
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923