1:
相对于窗口的具体位置
关键点:
JButton组件添加到JPanel时,如果想自己位置,需要对JPanel进行如下设置,才能自定义按钮位置
需要将组件添加到画板上去,才可以设置组件的相对具体位置
button1.setBounds(20, 200, 100, 100);//按钮的位置为(20,200),大小为(100,100)
import javax.swing.*;
import java.awt.*;
public class win extends JFrame{
public static void main(String []args)
{
win w = new win();
}
public win()
{
init();
this.setTitle("多个组件的位置相对于窗口的具体位置");
this.setBounds(100,200,500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void init()
{
JPanel panel1 = new JPanel();
panel1.setLayout(null);
JButton button1= new JButton("JButton1");
button1.setForeground(Color.BLUE);//设置按钮的颜色
button1.setBounds(20, 200,