[请教]为什么有些方法可以直接写,不用加对象名或类名

smalldeer 2004-10-06 09:36:52
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class testpanel
extends JFrame {
JPanel jp1;
JPanel jp2;
Container cp = getContentPane();

JButton b1 = new JButton("1");
JButton b5 = new JButton("2");

public testpanel() {
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.remove(jp1);
cp.add(jp2);
setVisible(true); ?????????????????????????
repaint(); ?????????????????????????
}
});

b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.remove(jp2);
cp.add(jp1);
setVisible(true);
repaint();
}
});

jp1 = new JPanel();
jp1.setLayout(new BorderLayout());
jp1.add(BorderLayout.CENTER, b1);

jp2 = new JPanel();
jp2.setLayout(new BorderLayout());

jp2.add(BorderLayout.CENTER, img);

cp.add(jp1);

setSize(500, 500);
setVisible(true);
}

public static void main(String[] args) {
testpanel app = new testpanel();
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}


-------------------------------------------------------------------------------------
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.remove(jp1);
cp.add(jp2);
setVisible(true); ?????????????????????????
repaint(); ?????????????????????????
}
});

到底哪个对象被setVisible(true);
和被repaint()了
请教了,谢谢
...全文
255 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
边城狂人 2004-10-07
  • 打赏
  • 举报
回复
加上 this 通不过是因为不知道是内部类的 this 还是外部类的 this
可以加上类名来标识,如下面这个程序

/*
* @(#) Test.java
* Created on 2004-9-21
* Created by James Fancy
*/
package jamesfancy;

/**
* @author James Fancy
*/
public final class Test {

public static interface MyTest {
public void test();
}

public void testit() {
new MyTest() {
public void test() {
System.out.println(this.getClass().getName());
System.out.println(Test.this.getClass().getName());
}
}.test();
}

public static void main(String[] args) {
Test test = new Test();
test.testit();
}

}


输出结果是:
jamesfancy.Test$1
jamesfancy.Test
serf 2004-10-06
  • 打赏
  • 举报
回复
testpanel的
smalldeer 2004-10-06
  • 打赏
  • 举报
回复
楼上正解
谢谢各位。
ladofwind 2004-10-06
  • 打赏
  • 举报
回复
setVisible(true);
repaint();
是操作testpanel对象,
内部类有访问创建它的对象的实现
smalldeer 2004-10-06
  • 打赏
  • 举报
回复
上面说的,应该不是我想要的

和静态方法无关
naxin 2004-10-06
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class testpanel
extends JFrame {
JPanel jp1;
JPanel jp2;
Container cp = getContentPane();

JButton b1 = new JButton("1");
JButton b5 = new JButton("2");

public testpanel() {
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.remove(jp1);
cp.add(jp2);
setVisible(true);
repaint();
}
});

b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.remove(jp2);
cp.add(jp1);
setVisible(true);
repaint();
}
});

jp1 = new JPanel();
jp1.setLayout(new BorderLayout());
jp1.add(BorderLayout.CENTER, b1);

jp2 = new JPanel();
jp2.setLayout(new BorderLayout());

// jp2.add(BorderLayout.CENTER, img);

cp.add(jp1);
setSize(500, 500);
setVisible(true);
}

public static void main(String[] args) {
testpanel app = new testpanel();
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
xq_zz 2004-10-06
  • 打赏
  • 举报
回复
一般来说一个方法是静态的方法可以不用实例化对象来调用此方法,静态的方法可以调用静态的方法,或者静态的成员,内部类也可以是静态的,也不用实例化对象的。
打个比方吧,我们常用的system.out.println()方法其实是一个道理。
他不用实例化任何对象就可以调用,system是类,out是一个静态的成员,也就是一个静态的内部名,println()是内部类的一个方法,所以可以直接调用。


class System1
{
static o out1 = new o();
static class o
{
void print1()
{
System.out.println("out");
}
}
}

public class a
{
public static void main(String args[])
{
System1.out1.print1();
}
}

上面是我做的一个仿照system.out.println()做的,可以参考参考~~~~~~~
如果我上面说的有什么错误,还请大家多多纠正~~~~~~~~
smalldeer 2004-10-06
  • 打赏
  • 举报
回复
但+上this 编译通不过的
边城狂人 2004-10-06
  • 打赏
  • 举报
回复
直接写方法名称调用方法的,实际上是调用的当前类实例对象自己的方法,也就是省略了 this.的
如 setVisible(true)
就等同于
this.setVisible(true)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧