本篇文章為大家展示了彈簧布局管理器怎么在Java項(xiàng)目中使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
10年積累的做網(wǎng)站、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有虹口免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。由 SpringLayout 類(lèi)實(shí)現(xiàn)的布局管理器稱(chēng)為彈簧布局管理器。利用該布局管理器管理組件,當(dāng)改變窗體的大小時(shí),能夠在不改變組件間相對(duì)位置的前提下自動(dòng)調(diào)整組件大小,使組件依舊布滿整個(gè)窗體,從而保證了窗體的整體效果。下面,通過(guò)一個(gè)實(shí)例來(lái)看看彈簧布局管理器的使用方法和效果。
彈簧布局管理器以容器和組件的邊緣為操作對(duì)象,通過(guò)為組件和容器邊緣以及組件和組件邊緣建立約束,實(shí)現(xiàn)對(duì)組件布局的管理。通過(guò)方法 putConstraint(String e1,Conponet c1,int pad,String e2,Componet c2)可以為各邊之間建立約束,該方法的入口參數(shù)說(shuō)明如下:
c1:需要參考的組件對(duì)象;
c2:需要參考的組件對(duì)象的具體需要參考的邊;
e1:被參考的組件對(duì)象;
e2:被參考的組件對(duì)象的具體被參考的邊;
pad:兩條邊之間的距離,即兩個(gè)組件之間的間距。
有點(diǎn)繞,其實(shí)很好理解。
實(shí)例1:使用彈簧布局管理器。
本實(shí)例利用彈簧布局管理器實(shí)現(xiàn)窗體,在調(diào)整窗體的大小后,組件仍會(huì)布滿整個(gè)窗體,并且組件間的相對(duì)位置并不會(huì)改變。源碼:
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("內(nèi)容 :"); 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("確認(rèn)"); 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); } }