java swing怎么控制按钮的位置啊?
发布网友
发布时间:2022-05-01 11:36
我来回答
共1个回答
热心网友
时间:2023-10-10 03:45
你可以将容器的整体布局设置为FlowLayout,然后在FlowLayout中添加一个Box布局,Box再添加3个部分,分别是表格、标签和按钮,两个按钮可以在用一个再新建一个Box对象并添加进去,下面是代码:
import javax.swing.*;
import java.awt.*;
public class test extends JFrame{
public test()
{
setSize(300,200);
setLocationRelativeTo(null); //使窗体居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setTitle("用户信息");
String str[] = {"属性","信息"};
Object str2[][]={{"姓名",""},{"职工号",""},{"身份证号",""},{"性别",""},{"出生年月",""}};
JTable table = new JTable(str2,str);
JButton Button1 = new JButton("修改信息");
JButton Button2 = new JButton("修改密码");
JLabel Label = new JLabel("用户:",JLabel.CENTER);
JScrollPane scrollpane = new JScrollPane(table);
Box box = Box.createVerticalBox();
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(Button1);
buttonBox.add(Button2);
box.add(table);
box.add(Label);
box.add(buttonBox);
add(box);
}
public static void main(String []args)
{
test Test = new test();
Test.setVisible(true);
}
}