弹簧布局管理器怎么在Java项目中使用-创新互联
本篇文章为大家展示了弹簧布局管理器怎么在Java项目中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
10年积累的做网站、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有虹口免费网站建设让你可以放心的选择与我们合作。由 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项目中使用-创新互联
文章路径:http://pwwzsj.com/article/esgig.html