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

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

顯示具有 Swing 標籤的文章。 顯示所有文章
顯示具有 Swing 標籤的文章。 顯示所有文章

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);
 }

}

執行結果


2017年12月23日 星期六

建立一個JTextField文字欄位


package test.swing.component;

import java.awt.FlowLayout;

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

public class TestJTextField extends JFrame {
 
 private JPanel contentPane;

 public static void main(String[] args) {
  TestJTextField frame = new TestJTextField();
  frame.setVisible(true);
 }
 
 public TestJTextField(){
  setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(10,10,10,10));   //(上,左,下,右)
  setContentPane(contentPane);
  
  //將Layout設定為由左至右的排列components之間的關係,超出面板宽度的元件往下列遞補
  contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));   //(置左,左右間距,上下間距)
  
  JTextField nameTF = new JTextField("請輸入資料...");
  nameTF.setColumns(15);
  
  contentPane.add(nameTF);
  
 }

}

建立一個JTextArea文字區塊



package test.swing.component;

import java.awt.FlowLayout;

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

public class TestJTextArea extends JFrame {
 
 private JPanel contentPane;

 public static void main(String[] args) {
  TestJTextArea frame = new TestJTextArea();
  frame.setVisible(true);
 }
 
 public TestJTextArea(){
  setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(10,10,10,10));   //(上,左,下,右)
  setContentPane(contentPane);
  
  //將Layout設定為由左至右的排列components之間的關係,超出面板宽度的元件往下列遞補
  contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));   //(置左,左右間距,上下間距)
  
  JTextArea descTA = new JTextArea(5,30);  
  
  contentPane.add(descTA);  
  
 }

}

建立一個Spinner下拉選單


package test.swing.component;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class TestJSpinner extends JFrame {

 public static void main(String[] args) {
  try{
   //設置系統預設的theme
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      
      TestJSpinner frame = new TestJSpinner();
      frame.setVisible(true);
      
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }

 public TestJSpinner(){  
  setTitle("Test JSpinner for version 1.0.1");
  //setIconImage(Toolkit.getDefaultToolkit().getImage(TestJTable.class.getResource("/deviantart_48x48.png")));
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 500);  //(左邊界,上邊界,宽度,長度)
  JPanel contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));  //(上,左,下,右)
  setContentPane(contentPane);
  
  //設置Layout的樣式來呈現component之間在Panel上的位置關係
  contentPane.setLayout(new BorderLayout());  
  
  JPanel jpane = new JPanel();
  
  // 加入資料到Spinner
        String[] portal = {"Google","Yahoo","PChome"};
        SpinnerModel portalSM = new SpinnerListModel(portal);
        
        JSpinner portalSP = new JSpinner(portalSM);
        
  // 設定Spinner的宽度和高度
  Dimension d1 = portalSP.getPreferredSize();      
     d1.width = 120;
     d1.height = 30;
     portalSP.setPreferredSize(d1);
     
     // 修改文字欄位的外觀  
   JSpinner.ListEditor editor = new JSpinner.ListEditor(portalSP);
   JTextField tf = editor.getTextField();  
   tf.setHorizontalAlignment(JTextField.CENTER);  
   tf.setFont(new Font("微軟正黑體", Font.PLAIN, 14));  
   portalSP.setEditor(editor);
   
   jpane.add(portalSP);
  
  contentPane.add(jpane, BorderLayout.CENTER);  
 }
}

建立一個JPopupMenu彈出選單


package test.swing;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class JPopupMenuSample {

 public static void main(final String args[]) {
     JFrame frame = new JFrame("JPopupMenu Example");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     // 建立彈出選單
     JPopupMenu popupMenu = new JPopupMenu("Title");

     // 剪下
     JMenuItem cutMenuItem = new JMenuItem("Cut");
     popupMenu.add(cutMenuItem);

     // 貼上
     JMenuItem copyMenuItem = new JMenuItem("Copy");
     popupMenu.add(copyMenuItem);
     
     // 貼上
     JMenuItem pasteMenuItem = new JMenuItem("Paste");
     pasteMenuItem.setEnabled(false);
     popupMenu.add(pasteMenuItem);

     // 加入分隔線
     popupMenu.addSeparator();

     // 尋找
     JMenuItem findMenuItem = new JMenuItem("Find");
     popupMenu.add(findMenuItem);
     JButton label = new JButton("按右鍵-彈出選單");
     frame.add(label);
     label.setComponentPopupMenu(popupMenu);

     frame.setSize(350, 250);
     frame.setVisible(true);
   }

}

建立一個Combox下拉選單




package test.swing.component;

import java.awt.FlowLayout;

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

public class TestJButton extends JFrame {
 
 private JPanel contentPane;

 public static void main(String[] args) {
  TestJButton frame = new TestJButton();
  frame.setVisible(true);
 }
 
 public TestJButton(){
  setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(10,10,10,10));   //(上,左,下,右)
  setContentPane(contentPane);
  
  //將Layout設定為由左至右的排列components之間的關係,超出面板宽度的元件往下列遞補
  contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));   //(置左,左右間距,上下間距)
  
  JButton finishBtn = new JButton("完成按鈕");
  
  contentPane.add(finishBtn);
  
  
 }

}