尝试在 Java GUI 中更改元素的边框和位置时出错

Error when trying to change borders and the position of an element in Java GUI

我正在尝试使用 Java 创建一个按钮,但 JButon 的位置和大小的更改无法以这种方式工作。我对大小使用 setbounds,对位置使用 Borderlayout。谁能告诉我如何以正确的方式更改 Jbutton 的大小和位置?

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;
import javax.imageio.*;
public class Back extends JFrame{


    private JButton r;

    public Back(){


         super("title");
         setLayout(new FlowLayout());
         r=new JButton ("ROLL THE DICE");
         add(r,BorderLayout.SOUTH); 
         r.setBounds(100, 100, 20, 70);

         thehandler hand=new thehandler();//konstruktori i handler merr nje instance te Background
         r.addActionListener(hand);

    }
     private class thehandler implements ActionListener{

         public void actionPerformed(ActionEvent event) {

         }
         }
    public static void main(String[] args) {

         Back  d = new Back() ;

         d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         d.getContentPane().setBackground(Color.GREEN);
         d.setSize(700,500);

         d.setVisible(true);    


    }

}

有一种方法,但是一种非常非常非常 糟糕的方法。

您可以使用:

setLayout(null);

而不是:

setLayout(new FlowLayout());

你得到你想要的。

但是,我建议您不要使用它。缺点是您必须手动设置所有组件的位置。我添加此 仅供参考