PHP字串長度找子字串與取代字串的常用函數

PHP在字串的操作上,有非常多的相關函數可以使用,我們在這裡只介紹經常會使用到的字串操作函數。像是如何取得字串的長度、在某一個字裏面尋找子字串首次出現的位置、以及字串的取代函數...等等。這些都是在編寫程式的時候,頻繁被程式設計師使用到的基礎函數。 取得字串長度 <?...

2017年12月24日 星期日

使用GridBagLayout將每個元件放入每一格子裡面

package test.swing.layout;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestGridLayout extends JFrame {
 
 private JPanel contentPane;

 public static void main(String[] args) {
  TestGridLayout frame = new TestGridLayout();
  frame.setVisible(true);
 }
 
 public TestGridLayout() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
  setContentPane(contentPane);
  
  //將Layout設定為GridLayout會把視窗畫面切割成長、宽相同大小的方格
  contentPane.setLayout(new GridLayout(2, 0, 10, 5));  //(列數,欄數,左右間距,上下間距)
  
  JButton firstBtn = new JButton("按鈕 1");
  contentPane.add(firstBtn);
  
  JButton secondBtn = new JButton("按鈕 2");
  contentPane.add(secondBtn);
  
  JButton thirthBtn = new JButton("按鈕 3");
  contentPane.add(thirthBtn); 
  
  JButton forthBtn = new JButton("按鈕 4");
  contentPane.add(forthBtn); 
 }

}

執行結果

運用FlowLayout來依序排列元件,超出列宽往下一列放置元件


package test.swing.layout;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestFlowLayout extends JFrame {
	
	private JPanel contentPane;

	public static void main(String[] args) {
		TestFlowLayout frame = new TestFlowLayout();
		frame.setVisible(true);

	}
	
	public TestFlowLayout() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
		setContentPane(contentPane);
		
		//將Layout設定為由左至右的排列components之間的關係,超出面板宽度的元件往下列遞補
		contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));   //(置左,左右間距,上下間距)
		
		JButton firstBtn = new JButton("按鈕 1");
		contentPane.add(firstBtn);
		
		JButton secondBtn = new JButton("按鈕 2");
		contentPane.add(secondBtn);
		
		JButton thirdBtn = new JButton("按鈕 3");
		contentPane.add(thirdBtn);
		
		JButton forthBtn = new JButton("按鈕 4");
		contentPane.add(forthBtn);
		
		JButton fifthBtn = new JButton("按鈕 5");
		contentPane.add(fifthBtn);
		
		JButton sixthBtn = new JButton("按鈕 6");
		contentPane.add(sixthBtn);		
	}

}

執行結果

使用GridLayout將元件放入每個相同大小的格子中


package test.swing.layout;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestGridLayout extends JFrame {
	
	private JPanel contentPane;

	public static void main(String[] args) {
		TestGridLayout frame = new TestGridLayout();
		frame.setVisible(true);
	}
	
	public TestGridLayout() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
		setContentPane(contentPane);
		
		//將Layout設定為GridLayout會把視窗畫面切割成長、宽相同大小的方格
		contentPane.setLayout(new GridLayout(2, 0, 10, 5));  //(列數,欄數,左右間距,上下間距)
		
		JButton firstBtn = new JButton("按鈕 1");
		contentPane.add(firstBtn);
		
		JButton secondBtn = new JButton("按鈕 2");
		contentPane.add(secondBtn);
		
		JButton thirthBtn = new JButton("按鈕 3");
		contentPane.add(thirthBtn);	
		
		JButton forthBtn = new JButton("按鈕 4");
		contentPane.add(forthBtn);	
	}

}

執行結果

運用BorderLayout以線框來放置元件


package test.swing.layout;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestBorderLayout extends JFrame {
	
	private JPanel contentPane;

	public static void main(String[] args) {
		TestBorderLayout frame = new TestBorderLayout();
		frame.setVisible(true);
	}
	
	public TestBorderLayout() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
		setContentPane(contentPane);
		
		//依據東、西、南、北、中的方位來放置components
		contentPane.setLayout(new BorderLayout());	
		
		JButton eastBtn = new JButton("東方");
		contentPane.add(eastBtn, BorderLayout.EAST);
		
		JButton westBtn = new JButton("西方");
		contentPane.add(westBtn, BorderLayout.WEST);
		
		JButton southBtn = new JButton("南方");
		contentPane.add(southBtn, BorderLayout.SOUTH);
		
		JButton northBtn = new JButton("北方");
		contentPane.add(northBtn, BorderLayout.NORTH);	
		
		JButton centerBtn = new JButton("中間");
		contentPane.add(centerBtn, BorderLayout.CENTER);	
		
	}

}

執行結果 

使用CardLayout(或稱TAB)的方式來編排頁面元件


package test.swing.layout;

import java.awt.CardLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

public class TestCardLayout extends JFrame {
	
	private JPanel contentPane;

	public static void main(String[] args) {
		TestCardLayout frame = new TestCardLayout();
		frame.setVisible(true);		
	}
	
	public TestCardLayout(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
		setContentPane(contentPane);
		
		//設置Layout的樣式來呈現component之間在Panel上的位置關係
		contentPane.setLayout(new CardLayout(35, 10));  //左右間距,上下間距
		
		//加入Tab panel到Layout上面
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, "新Tab panel面板");
		
		//加入第一個tab panel
		JPanel tab1 = new JPanel();
		tab1.setBackground(Color.WHITE);
		tab1.setBorder(new EmptyBorder(20, 10, 30, 10));   //(上,左,下,右)
		tabbedPane.addTab("測試tab1", null, tab1, null);  //(名稱,圖示,元件,提示)
		
		JButton submitBtn = new JButton("提交");
		tab1.add(submitBtn);
		
		//加入第二個tab panel
		JPanel tab2 = new JPanel();
		tab2.setBackground(Color.WHITE);
		tab2.setBorder(new EmptyBorder(20, 10, 30, 10));   //(上,左,下,右)
		tabbedPane.addTab("測試tab2", null, tab2, null);  //(名稱,圖示,元件,提示)
		
		JLabel nameLabel = new JLabel("名稱: ");
		tab2.add(nameLabel);				
		
	}

}
執行結果 

運用BoxLayout來依序佈置每個元件


package test.swing.layout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestBoxLayout extends JFrame {
	
	private JPanel contentPane;

	public static void main(String[] args) {
		TestBoxLayout frame = new TestBoxLayout();
		frame.setVisible(true);
	}
	
	public TestBoxLayout() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 420, 300);   //(左邊界,上邊界,宽度,長度)
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
		setContentPane(contentPane);
		
		//將Layout設為BoxLayout時,components會依序並列或並行放置於面板上,而且超過視窗寬度時不會換行
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
		
		JButton firstBtn = new JButton("按鈕 1");
		contentPane.add(firstBtn);
		
		JButton secondBtn = new JButton("按鈕 2");
		contentPane.add(secondBtn);
		
		JButton thirdBtn = new JButton("按鈕 3");
		contentPane.add(thirdBtn);
		
		JButton forthBtn = new JButton("按鈕 4");
		contentPane.add(forthBtn);
		
		JButton fifthBtn = new JButton("按鈕 5");
		contentPane.add(fifthBtn);
		
		JButton sixthBtn = new JButton("按鈕 6");
		contentPane.add(sixthBtn);		
	}

}

執行結果 

使用AbsoluteLayout來佈置元件的相對位置


package test.swing.layout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestAbsoluteLayout extends JFrame {
 
 private JPanel contentPane;

 public static void main(String[] args) {
  TestAbsoluteLayout frame = new TestAbsoluteLayout();
  frame.setVisible(true);
 }
 
 public TestAbsoluteLayout(){
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));   //(上,左,下,右)
  setContentPane(contentPane);
  
  //當Layout設定為null代表以絕對路徑方式描述components之間的關係
  contentPane.setLayout(null);
  
  JButton btnNewButton = new JButton("提交");
  btnNewButton.setBounds(10, 10, 87, 23);   //(左邊界,上邊界,宽度,長度)
  contentPane.add(btnNewButton);
  
  JLabel lblNewLabel = new JLabel("顯示文字標籤!");
  lblNewLabel.setBounds(300, 237, 100, 15);   //(左邊界,上邊界,宽度,長度)
  contentPane.add(lblNewLabel);
 }

}

執行結果