<?php // 取得server ip echo $_SERVER['SERVER_ADDR']; // 取得目前請求的PHP頁面的完整URI echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // 取得目前請求的PHP頁面的完整實體路徑 echo $_SERVER['SCRIPT_FILENAME']; // 取得網站的根目錄 echo $_SERVER['DOCUMENT_ROOT']; // 取得目前請求的PHP頁面的相對URI echo $_SERVER['REQUEST_URI']; echo $_SERVER['SCRIPT_NAME']; echo $_SERVER['PHP_SELF']; ?>
PHP字串長度找子字串與取代字串的常用函數
PHP在字串的操作上,有非常多的相關函數可以使用,我們在這裡只介紹經常會使用到的字串操作函數。像是如何取得字串的長度、在某一個字裏面尋找子字串首次出現的位置、以及字串的取代函數...等等。這些都是在編寫程式的時候,頻繁被程式設計師使用到的基礎函數。 取得字串長度 <?...
2017年12月25日 星期一
PHP使用$_SERVER取得主機IP與網址
PHP取得隨機時間的函數
<?php // 取得隨機時間的函數 function get_rand_time($st_time, $ed_time) { $nums = ($ed_time - $st_time)/(24*60*60); //echo '1 ~ ' . $nums . '<br/>'; $rd_num = rand(1,$nums); //$rand_time = $st_time + ($rd_num - 1) * 24*60*60; $rand_time = $st_time + ($rd_num - 1) * 24*60*60 + (rand(1,86399)); if($rand_time > time()) $rand_time = time(); //echo $rand_time . ' ('.$rd_num.') <br/>'; //echo date("Y-m-d H:i:s", $rand_time); return date("Y-m-d H:i:s", $rand_time); } // 執行函式呼叫 $ed_time = strtotime(date("Y-m-d")); $st_time = $ed_time - 30*24*60*60; get_rand_time($st_time, $ed_time); ?>
PHP日期DATE指令的操作
<?php // 取得現在時間時間戳記(Unix timestamp) echo time(); echo mktime(); // 格式化時間 echo date('Y-m-d H:i:s', '1461827400'); echo date("Y-m-d H:i:s"); // 字串轉時間 echo strtotime("10:30pm April 15 2014"); // 設定timestamp $timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000'); $dt = new DateTime(); $dt->setTimestamp($timestamp); // 設定預設的時區 date_default_timezone_set(); // 設定timezone和format $dtm = new DateTime(strtotime($time)); $dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE)); $date = $dtm->format('D, M dS'); $time = $dtm->format('g:i a'); ?>
PHP與SimpleXml使用CRUD
<?php // 建立SimpleXmlElement $config = new SimpleXmlElement(''); $config->setting1 = 'setting1 value'; $config->saveXML('config.xml'); // 讀取SimpleXmlElement $config = new SimpleXmlElement('config.xml'); echo $config->setting1; echo $config->asXml(); // 更新SimpleXmlElement $config->setting1 = 'new value'; $config->setting2 = 'setting2 value'; echo $config->asXml(); // 刪除SimpleXmlElement unset($config->setting1); $config->setting2 = NULL; echo $config->asXML(); unlink('config.xml'); ?>
PHP常用字串指令的使用
<?php $str = "電腦專家,是PRO訓練,有素的人才!"; // 字串轉小寫 echo strtolower($str); // 字串轉大寫 echo strtoupper($str); // 從左邊開始找PRO字串片段 echo strpos($str,'PRO'); // 從右邊開始找PRO字串片段 echo strrpos($str,'PRO'); // 取長度為5的子字串 echo substr($str, 5); // 字串取代 echo str_replace("PRO", "專業", $str); // 字串分割為陣列 $ss = explode(',',$str); ?>
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); } }
訂閱:
文章 (Atom)
-
Ant本身並非是一個Windows的程式,因此安裝過程需要一些手動安裝步驟。 1. 首先,到apache ant的官方網站 http://ant.apache.org/bindownload.cgi 下載ant的程式套件壓縮檔 2. 再來,將Ant程式套件的zi...
-
簡介 記得在前面 PHP上傳檔案範例 文章中,我們已經了解HTML form的檔案上傳的處理方式。在這篇文章我想要向大家介紹jQuery檔案上傳功能( jQuery Upload ),它是一個非常簡單、美觀、容易上手使用的JavaScript的函數庫,可以有效的提升網頁開發...
-
JAX-WS(Java API for XML Web Services)Web Service是一種由Oracle Sun發展的以XML為基礎的Web Service開發技術。 目前 已經涵概到Java EE5(含)以上的版本, 它可以讓使用者迅速開 發 Java Web Se...