为什么Java中可以在不指定类或对象的情况下调用setLayout()?

发布于 2024-11-19 10:10:47 字数 920 浏览 1 评论 0原文

这是 Java Sound Demo。我的印象是 Java 中的方法总是从对象或类中调用。 setLayout() 方法从何而来?

public class MidiSynth extends JPanel implements ControlContext {
    // Fields omitted

    public MidiSynth() {
        setLayout(new BorderLayout());

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        EmptyBorder eb = new EmptyBorder(5,5,5,5);
        BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);
        CompoundBorder cb = new CompoundBorder(eb,bb);
        p.setBorder(new CompoundBorder(cb,eb));
        JPanel pp = new JPanel(new BorderLayout());
        pp.setBorder(new EmptyBorder(10,20,10,5));
        pp.add(piano = new Piano());
        p.add(pp);
        p.add(controls = new Controls());
        p.add(new InstrumentsTable());

        add(p);
    }

This is the beginning of the MidiSynth class from the Java Sound Demo. I was under the impression that methods in Java were always called from an object or a class. Where does the setLayout() method come from here?

public class MidiSynth extends JPanel implements ControlContext {
    // Fields omitted

    public MidiSynth() {
        setLayout(new BorderLayout());

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        EmptyBorder eb = new EmptyBorder(5,5,5,5);
        BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);
        CompoundBorder cb = new CompoundBorder(eb,bb);
        p.setBorder(new CompoundBorder(cb,eb));
        JPanel pp = new JPanel(new BorderLayout());
        pp.setBorder(new EmptyBorder(10,20,10,5));
        pp.add(piano = new Piano());
        p.add(pp);
        p.add(controls = new Controls());
        p.add(new InstrumentsTable());

        add(p);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风柔一江水 2024-11-26 10:10:48

MidiSynth extends JPanel

因此,setLayout 调用实际上是在调用 this.setLayout

MidiSynth extends JPanel

So the setLayout call is actually invoking this.setLayout

幽蝶幻影 2024-11-26 10:10:48

实际上,因为您的类 MidiSynth 继承了 JPanel 这意味着您可以像处理 MidiSynth 类的本地方法一样处理所有 JPanel 的方法。

Actually, because your class MidiSynth inherits JPanel that means you can deal with all JPanel's methods as you deal with local methods of MidiSynth class.

养猫人 2024-11-26 10:10:48

此外,setLayout() 是在 java.awt.Container

Additionally, setLayout() is defined in java.awt.Container

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文