您好,登录后才能下订单哦!
本篇文章为大家展示了弹簧布局管理器怎么在Java项目中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
由 SpringLayout 类实现的布局管理器称为弹簧布局管理器。利用该布局管理器管理组件,当改变窗体的大小时,能够在不改变组件间相对位置的前提下自动调整组件大小,使组件依旧布满整个窗体,从而保证了窗体的整体效果。下面,通过一个实例来看看弹簧布局管理器的使用方法和效果。
弹簧布局管理器以容器和组件的边缘为操作对象,通过为组件和容器边缘以及组件和组件边缘建立约束,实现对组件布局的管理。通过方法 putConstraint(String e1,Conponet c1,int pad,String e2,Componet c2)可以为各边之间建立约束,该方法的入口参数说明如下:
c1:需要参考的组件对象;
c2:需要参考的组件对象的具体需要参考的边;
e1:被参考的组件对象;
e2:被参考的组件对象的具体被参考的边;
pad:两条边之间的距离,即两个组件之间的间距。
有点绕,其实很好理解。
实例1:使用弹簧布局管理器。
本实例利用弹簧布局管理器实现窗体,在调整窗体的大小后,组件仍会布满整个窗体,并且组件间的相对位置并不会改变。源码:
import java.awt.Container; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.Spring; import javax.swing.SpringLayout; public class SpringLayout_3 extends JFrame { private static final long serialVersionUID = -6045918631932051025L; public SpringLayout_3() { // TODO Auto-generated constructor stub Container container = getContentPane(); SpringLayout springLayout = new SpringLayout(); container.setLayout(springLayout); JLabel topicLabel = new JLabel("主题 :"); JLabel contentLabel = new JLabel("内容 :"); final JTextField textField = new JTextField(30); JTextArea textArea = new JTextArea(3, 30); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(textArea); textArea.setLineWrap(true); final JButton confirmButton = new JButton("取消"); JButton cancelButton = new JButton("确认"); Spring st = Spring.constant(10); Spring st2 = Spring.constant(30); container.add(topicLabel); springLayout.putConstraint(SpringLayout.NORTH, topicLabel, st, SpringLayout.NORTH, container); springLayout.putConstraint(SpringLayout.WEST, topicLabel, st, SpringLayout.WEST, container); container.add(textField); springLayout.putConstraint(SpringLayout.WEST, textField, st2, SpringLayout.EAST, topicLabel); springLayout.putConstraint(SpringLayout.NORTH, textField, 0, SpringLayout.NORTH, topicLabel); springLayout.putConstraint(SpringLayout.EAST, textField, Spring.minus(st), SpringLayout.EAST, container); container.add(contentLabel); springLayout.putConstraint(SpringLayout.WEST, contentLabel, 0, SpringLayout.WEST, topicLabel); springLayout.putConstraint(SpringLayout.NORTH, contentLabel, st, SpringLayout.SOUTH, topicLabel); container.add(scrollPane); springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, contentLabel); springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, textField); springLayout.putConstraint(SpringLayout.EAST, scrollPane, Spring.minus(st), SpringLayout.EAST, container); container.add(confirmButton); springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, Spring.minus(st), SpringLayout.NORTH, confirmButton); springLayout.putConstraint(SpringLayout.EAST, confirmButton, Spring.minus(st), SpringLayout.EAST, container); springLayout.putConstraint(SpringLayout.SOUTH, confirmButton, Spring.minus(st), SpringLayout.SOUTH, container); container.add(cancelButton); springLayout.putConstraint(SpringLayout.EAST, cancelButton, Spring.minus(st), SpringLayout.WEST, confirmButton); springLayout.putConstraint(SpringLayout.NORTH, cancelButton, 0, SpringLayout.NORTH, confirmButton); // make the text field focused every time the window is activated addWindowFocusListener(new WindowAdapter() { @Override public void windowGainedFocus(WindowEvent e) { // TODO Auto-generated method stub textField.requestFocus(); } }); } public static void main(String[] args) { // TODO Auto-generated method stub SpringLayout_3 frame = new SpringLayout_3(); frame.setTitle("使用弹簧布局管理器"); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 600, 300); } }
上述内容就是弹簧布局管理器怎么在Java项目中使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。