Presentation is loading. Please wait.

Presentation is loading. Please wait.

記事本 物件導向系統實務.

Similar presentations


Presentation on theme: "記事本 物件導向系統實務."— Presentation transcript:

1 記事本 物件導向系統實務

2 JTEXTCOMPONENT 文字元件 JTextField :文字方塊 JPasswordField:密碼欄位
2018/11/30 JTextField :文字方塊 JPasswordField:密碼欄位 JTextArea:文字區域 JComponent JTextComponent JTextField JPasswordField JTextArea

3 JTEXTFIELD與JPASSWORDFIELD
2018/11/30 JTetField:輸入或顯示一行可捲動的文字內容 JPasswordField:與JTextField相同,只是輸入的 文字用替代字元顯示 建構子: JTextField() JTextField(String) //String是欄位初值 JTextField(int) //int 是欄位寬度 JTextField(String, int) JPasswordField() JPasswordField(String) JPasswordField(int) JPasswordField(String, int)

4 JTEXTFIELD與JPASSWORDFIELD常用的方法
說明 void setText(String) 設定JTextField欄位內容 String getText() 取得JTextField欄位內容 String getPassword() 取得JPasswordField欄位內容 void setEditable(boolean) 設定欄位是否可以編輯 boolean isEditable() 檢查欄位是否可以編輯 void setColumns(int) 設定欄位寬度 int getColumns() 取得欄位寬度 void setHorizontalAlignment(int) JTextField.LEFT:字元靠左 int getHorizontalAlignment() 取得文字內容的對齊方式 void setEchoChar(char) 設定JPasswordField的替代字元 char getEchoChar() 取得JPasswordField的替代字元 void setActionCommand(String) 設定欄位對應的命令字串 String getActionCommand() 取得欄位對應的命令字串 void addActionListener(ActionListener) 新增傾聽者,當按下enter執行動作 void removeActionListener() 刪除欄位的傾聽者物件 2018/11/30

5 範例1: 文字方塊 2018/11/30 nameLabel = new JLabel("使用者帳號: ");
textField = new JTextField(10); textField.addActionListener(this); passwordLabel = new JLabel("使用者密碼: "); passwordField = new JPasswordField(10); passwordField.addActionListener(this); passwordResult = new JLabel("尚未輸入資料!"); textFieldPanel = new JPanel(new FlowLayout()); textFieldPanel.add(nameLabel); textFieldPanel.add(textField); textFieldPanel.add(passwordLabel); textFieldPanel.add(passwordField); textFieldPanel.add(passwordResult); c.add(textFieldPanel); } public void actionPerformed(ActionEvent e) { if(e.getSource() == textField) { textField.setFocus(false); passwordField.setFocus(true); if(passwordField.getText().equals("")) passwordResult.setText("密碼尚未輸入,使用者是"+textField.getText()); else passwordResult.setText("使用者是" + textField.getText() + " , 密碼是" + passwordField.getText()); if(e.getSource() == passwordField) { if(textField.getText().equals("")) passwordResult.setText("使用者帳號尚未輸入,密碼是" + passwordField.getText()); import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class TextWin { public static void main(String [] args) MyWin win = new MyWin("文字區練習"); win.setSize(200, 200); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } class MyWin extends JFrame implements ActionListener private Container c; private JPanel textFieldPanel ; private JLabel nameLabel; private JTextField textField; private JLabel passwordLabel; private JPasswordField passwordField; private JLabel passwordResult; MyWin(String title) super(title); c = getContentPane(); 2018/11/30

6 JTEXTAREA文字區域 JTextArea文字區域:可以輸入或顯示多行文字
2018/11/30 JTextArea文字區域:可以輸入或顯示多行文字 使用”\n”或”\n\r”或”\r”符號換行(視作業系統而定) 建構子: JTextArea() JTextArea(String) JTextArea(int, int) JTextArea(String, int,int)

7 JTEXTAREA常見的方法 方法 說明 void append(String) 新增內容到TextArea最後
void insert(String, int) 在int的位置插入String void replaceRange(String int,int) 從第1個int 到第2個取代成String String getText() 取得TextArea的內容 void setFont(Font) 設定顯示文字為Font物件 void setRows(int) 設定int列 int getRows() 取得欄位的列數 void setColumns(int) 設定欄寬 int getColumns() 取得欄寬 int getLineCount() 取得行數 void setLineWrap(boolean) 設定是否自動換行 boolean getLineWrap() 取得是否自動換行 void setTabSize(int) 設定Tab代表幾個字元 int getTabSize() 取得Tab代表幾個字元 2018/11/30

8 範例2:文字區域 2018/11/30 MyWin(String title) { super(title);
c = getContentPane(); originalTextArea = new JTextArea("輸入文字", 10, 5); copyTextArea = new JTextArea(10, 5); button = new JButton("輸入完成,複製"); button.addActionListener(this); textAreaPanel = new JPanel(new GridLayout(3,1)); textAreaPanel.add(originalTextArea); textAreaPanel.add(button); textAreaPanel.add(copyTextArea); c.add(textAreaPanel); } public void actionPerformed(ActionEvent e) if(e.getSource() == button) copyTextArea.setText(originalTextArea.getText()); //JTextArea練習 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class TextAreaWin { public static void main(String [] args) MyWin win = new MyWin("文字區練習"); win.setSize(400, 400); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } class MyWin extends JFrame implements ActionListener private Container c; private JPanel textAreaPanel ; private JTextArea originalTextArea; private JButton button; private JTextArea copyTextArea; 2018/11/30

9 JSCROLLBAR捲動軸元件 JScrollBar元件繼承Jcomponent元件 建構子: JScrollBar()
2018/11/30 JScrollBar元件繼承Jcomponent元件 建構子: JScrollBar() JScrollBar(int) JScrollBar(int, int, int, int, int) 第1個int是捲軸的方向:JScrollBar.VERTICAL,JScrollBar.HORIZONTAL 第2個int是初值 第3個int是可見區域 第4,5個int是最大值和最小值

10 JSCROLLPANE水平和垂直捲軸 JScrollPane可以為元件同時加上水平和垂直的捲軸 建構子: JScrollPane()
2018/11/30 JScrollPane可以為元件同時加上水平和垂直的捲軸 建構子: JScrollPane() JScrollPane(Component )

11 範例3: 加上捲軸 2018/11/30 MyWin(String title) { super(title);
c = getContentPane(); originalPanel = new JPanel(new BorderLayout()); originalTextArea = new JTextArea("輸入文字", 10, 5); scroll1 = new JScrollPane(originalTextArea); originalPanel.add(scroll1, BorderLayout.CENTER); copyPanel = new JPanel(new BorderLayout()); copyTextArea = new JTextArea("waiting",10, 5); scroll2 = new JScrollPane(copyTextArea); copyPanel.add(scroll2, BorderLayout.CENTER); buttonPanel = new JPanel(); button = new JButton("輸入完成,複製"); button.addActionListener(this); buttonPanel.add(button); textAreaPanel = new JPanel(new GridLayout(3,1)); textAreaPanel.add(originalPanel); textAreaPanel.add(buttonPanel); textAreaPanel.add(copyPanel); c.add(textAreaPanel); } public void actionPerformed(ActionEvent e) { if(e.getSource() == button) copyTextArea.setText(originalTextArea.getText()); 範例3: 加上捲軸 //JTextArea練習+JScrollPane import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class TextAreaScrollWin { public static void main(String [] args) { MyWin win = new MyWin("文字區練習"); win.setSize(400, 400); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } class MyWin extends JFrame implements ActionListener { private Container c; private JPanel textAreaPanel ; private JPanel originalPanel; private JPanel copyPanel; private JPanel buttonPanel; private JTextArea originalTextArea; private JButton button; private JTextArea copyTextArea; private JScrollPane scroll1; private JScrollPane scroll2; 2018/11/30

12 JRadioButtonMenuItem
視窗功能表 2018/11/30 JComponent AbstractButton JMenuItem JCheckBoxMenuItem JRadioButtonMenuItem JMenu JMenuBar JPopupMenu JToolBar JSeparator

13 JPOPUPMENU彈出式選單元件 2018/11/30


Download ppt "記事本 物件導向系統實務."

Similar presentations


Ads by Google