1个回答
展开全部
先要新建一个JPanel主面板,然后设置边距
jPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); // 设置边距
具体实例
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class JPanelTest {
public static void main(String[] args) {
JFrame jFrame=new JFrame("JPanel面板测试");
JPanel jPanel=new JPanel();
jPanel.setLayout(new GridLayout(3, 2, 10, 10));
jPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); // 设置边距
jFrame.add(jPanel);
JLabel jl1=new JLabel("用户名:");
JTextField jtf=new JTextField();
JLabel jl2=new JLabel("密码:");
JPasswordField jpf=new JPasswordField();
JButton jb1=new JButton("登陆");
JButton jb2=new JButton("重置");
jPanel.add(jl1);
jPanel.add(jtf);
jPanel.add(jl2);
jPanel.add(jpf);
jPanel.add(jb1);
jPanel.add(jb2);
jFrame.setLocation(400, 200);
jFrame.setSize(250, 150);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
运行效果图
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询