简体   繁体   English

JButton没有出现

[英]JButtons not appearing

I am attempting to write a simple program for now but the actual JButton is not appearing for some reason, here is my code below. 我现在尝试编写一个简单的程序,但是由于某种原因实际的JButton没出现,这是下面的代码。

import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
    public static void main(String[] args) {
        JFrame window = new JFrame("Shoes");
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.setResizable(true);
        window.setSize(400,500);
        window.setVisible(true);
        window.setLocationRelativeTo(null);
        JButton welcome = new JButton("Click here");
        welcome.setLocation(100,100);
        welcome.setVisible(true);
            // doesn't work, but is there another way to make it so?
    //add(welcome);
    }
}

You created the button, but did not add it. 您创建了按钮,但是没有添加它。

You have to add it to the window . 您必须将其添加到window Simply using add(welcome) will add it to your frame, which you extend, but not to the window in which you want it to show. 只需使用add(welcome)将其添加到您要扩展的框架中,而不添加到您希望其显示的window中。

Instead of: 代替:

add(welcome);

Just do: 做就是了:

window.add(welcome);

I would also like to mention that the reason the code on your commented out section didnt work, is because you're extending JFrame. 我还要提及的是,您注释掉部分中的代码不起作用的原因是因为您正在扩展JFrame。

When you extend JFrame, you inherit all of the methods JFrame has. 扩展JFrame时,将继承JFrame拥有的所有方法。 That includes add() . 这包括add() However, when you use this.add() you are adding the compononent to you Test Object (which is also a JFrame), not your window JFrame. 但是,当您使用this.add() ,将组件添加到测试对象(也是JFrame)中,而不是window JFrame中。

To add to the window you would use window.add(welcome); 要添加到窗口,您可以使用window.add(welcome);

To stop these weird confusions in the future I would also change public class Test extends JFrame to public class Test 为了将来避免这些奇怪的混乱,我还将更改public class Test extends JFramepublic class Test

you extended JFrame yet in your code you use another JFrame that you created JFrame window = new JFrame("Shoes"); 你扩展JFrame但在你的代码中使用另一个JFrame所创建JFrame window = new JFrame("Shoes"); , this is why add(welcome); ,这就是为什么add(welcome); is not working for you... since its trying to add the JButton to this instance of your Test class (which is not visible) and not the window you have created. 不适用于您...因为它试图将JButton添加this Test class实例(不可见),而不是您创建的window

You have 2 ways of solving this: 您有两种解决方法:

The first one as mentioned by @Hackerdarshi is to add the button to the window you created. @Hackerdarshi提到的第一个是将按钮添加到您创建的window like : window.add(welcome); 如: window.add(welcome);

The second way is to make use of your extension of the JFrame class (otherwhise why extend at all) and call all the methods on the window using this instance of your Test class : 第二种方法是利用JFrame class扩展(否则为什么还要扩展),并使用Test class this实例在window上调用所有方法:

public Test() {
    super("Shoes");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(true);
    setSize(400,500);
    setVisible(true);
    setLocationRelativeTo(null);
    JButton welcome = new JButton("Click here");
    welcome.setLocation(100,100);
    welcome.setVisible(true);
        // this will work since `this` instance is set to visible
    add(welcome);
}

NOTE that to set the buttons location like: welcome.setLocation(100,100); 注意,设置按钮位置的方式如下: welcome.setLocation(100,100); you should use a null Layout 您应该使用null Layout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM