Java 通过类创建按钮

Java 通过类创建按钮,java,swing,class,jbutton,Java,Swing,Class,Jbutton,我是Java新手,试图通过我的类创建一个按钮,它有一个带参数的方法。但是,当我创建两个类实例时,它只显示一个按钮,即最新的按钮。你能告诉我我在这里犯了什么错误吗 我的班级档案 public class CreateButton { int posx; int posy; int buttonWidth; int buttonHeight; public void newButton(int x, int y, int w, int h) {

我是Java新手,试图通过我的类创建一个按钮,它有一个带参数的方法。但是,当我创建两个类实例时,它只显示一个按钮,即最新的按钮。你能告诉我我在这里犯了什么错误吗

我的班级档案

public class CreateButton {
    int posx;
    int posy;
    int buttonWidth;
    int buttonHeight;
    public void newButton(int x, int y, int w, int h) {
        posx = x;
        posy = y;
        buttonWidth = w;
        buttonHeight = h;
        JFrame f = new JFrame();
        JButton b = new JButton("Test");
        b.setBounds(posx, posy, buttonWidth, buttonHeight);

        f.add(b);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
我的档案

class Project {
    public static void main(String[] args) {
        CreateButton myButton1 = new CreateButton();
        CreateButton myButton2 = new CreateButton();
        myButton1.newButton(50, 200, 100, 50);
        myButton2.newButton(100, 250, 100, 50);
    }
}

实际上,您的代码创建了两个按钮,但是
JFrame
窗口彼此上方,因此您只能看到一个。移动窗口,您将看到两个按钮

要将按钮添加到同一帧,需要从
createButton()
方法中删除
JFrame
的创建,并将其作为参数添加到函数中。在
main()
-方法中创建框架

正如其他人已经评论的那样,类的名称有些“不标准”。如果您计划在那里创建其他小部件,更好的名称应该是
ButtonFactory
UIFactory
。不管怎样,请考虑<代码> CurATeButoNo.()/>代码>方法返回初始化按钮,而不是将其添加到 JFrase>代码>中。这样,您将获得灵活性,并且可以更轻松地在单元测试中测试创建的按钮的参数。如果按钮自动添加到帧中,则实现起来要复杂得多

import javax.swing.*;

public class CreateButton {


    public static class UIFactory {

        public JButton newButton(int posx, int posy, int buttonWidth, int buttonHeight) {
            JButton b = new JButton("Test");
            b.setBounds(posx, posy, buttonWidth, buttonHeight);

            return b;
        }

        public JFrame newFrame(int width, int height) {
            JFrame f = new JFrame();
            f.setSize(width, height);
            f.setLayout(null);

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            return f;
        }

        public JFrame mainWindow() {
            JFrame f = newFrame(400, 400);
            f.add(newButton(50, 200, 100, 50));
            f.add(newButton(100, 250, 100, 50));
            return f;
        }
    }

    public static void main(String[] args) {
        UIFactory ui = new UIFactory();
        JFrame main  = ui.mainWindow();
        main.setVisible(true);

        JFrame win2 = ui.newFrame(150, 150);
        win2.setLocation(400, 400);
        JButton b2;
        win2.add(b2 = ui.newButton(50, 50, 100, 50));
        b2.addActionListener( e -> win2.setVisible(false) );
        win2.setVisible(true);
    }
}

对于一个不仅创建按钮而且还创建框架的类来说,这似乎是一个奇怪的名称。为什么不创建一个按钮呢?一般来说,如果你为你的类选择的名称是动词短语,那么你的设计肯定是错误的(或者你的命名,或者你对你正在做的事情的理解)。方法应该是“动词”。类应该是“对象”。在任何情况下,按钮都是在自己的框架中创建的。当你合上那个框的时候,你没看到后面另一个按钮的框吗?是的,我看到了。谢谢1)屏幕上应该有两个画面,在完全相同的位置。您是否尝试移动可视框架以显示下面的框架?2)
f.setLayout(空)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与…的布局填充和边框一起使用。。。。3) 使用缩进代码行和代码块的逻辑和一致形式。缩进的目的是使代码的流程更易于遵循!4) 提示:添加@realponsum(或任何人,
@
很重要)以通知此人新的评论。