有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java向JDesktopPane添加按钮

我有一个来自swing的问题,我有两个表单,第一个表单用于MDI,第二个表单用于登录, 我想当用户输入正确的密码和用户名时,用户可以登录到MDI的表单。MDI表单使用jform和登录表单使用jinternalframe,我定义了JDesktopPane并将登录添加到此,但我想使用jframe(MDI表单)中的按钮,当我将按钮添加到JDesktopPane时,它可以添加,但我无法设置按钮的位置。 请帮助我如何设置我的jinternalframe和按钮的位置

这是我的代码:

     Login frame = new Login();
    frame.setVisible(true); 
    desktop=new JDesktopPane();
    desktop.add(frame,BorderLayout.CENTER);
    setContentPane(desktop);
    frame.setSelected(true);  
    frame.setLocation(500, 200);

    FlowLayout flo = new FlowLayout();
    desktop.setLayout(flo);
    JButton buttonSaveCustumer = new JButton("Create Customer");
    buttonSaveCustumer.setLocation(70, 80);
    this.add(buttonSaveCustumer);

致意


共 (1) 个答案

  1. # 1 楼答案

    请参阅下面的方法,//在使用下面的方法之前,请不要忘记将layout属性设置为null

    public void setBounds(int x, int y, int width, int height)
    

    移动此组件并调整其大小。左上角的新位置由x和y指定,新尺寸由宽度和高度指定。 此方法会更改布局相关信息,从而使组件层次结构无效

    Parameters:
    x - the new x-coordinate of this component
    y - the new y-coordinate of this component
    width - the new width of this component
    height - the new height of this component