記事本物件導向系統實務

Download Report

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

記事本
物件導向系統實務
JTEXTCOMPONENT 文字元件
2020/4/24
JTextField :文字方塊
 JPasswordField:密碼欄位
 JTextArea:文字區域

JTextField
JComponent
JPasswordField
JTextComponent
JTextArea
2
JTEXTFIELD與JPASSWORDFIELD
2020/4/24
JTetField:輸入或顯示一行可捲動的文字內容
 JPasswordField:與JTextField相同,只是輸入的
文字用替代字元顯示
 建構子:









JTextField()
JTextField(String) //String是欄位初值
JTextField(int) //int 是欄位寬度
JTextField(String, int)
JPasswordField()
JPasswordField(String)
JPasswordField(int)
JPasswordField(String, int)
3
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()
刪除欄位的傾聽者物件
2020/4/24
方法
4
範例1: 文字方塊
import java.awt.*;
2.
import java.awt.event.*;
3.
import javax.swing.*;
4.
import javax.swing.event.*;
5.
class TextWin
6.
{
7.
public static void main(String [] args)
8.
{
9.
MyWin win = new MyWin("文字區練習");
10.
win.setSize(200, 200);
11.
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12.
win.setVisible(true);
}
13.
14.
}
15.
class MyWin extends JFrame
implements ActionListener
16.
17.
{
nameLabel = new JLabel("使用者帳號: ");
2.
textField = new JTextField(10);
3.
textField.addActionListener(this);
4.
passwordLabel = new JLabel("使用者密碼: ");
5.
passwordField = new JPasswordField(10);
6.
passwordField.addActionListener(this);
7.
passwordResult = new JLabel("尚未輸入資料!");
8.
textFieldPanel = new JPanel(new FlowLayout());
9.
textFieldPanel.add(nameLabel);
10.
textFieldPanel.add(textField);
11.
textFieldPanel.add(passwordLabel);
12.
textFieldPanel.add(passwordField);
13.
textFieldPanel.add(passwordResult);
14.
c.add(textFieldPanel);
15.
}
16.
public void actionPerformed(ActionEvent e)
17.
{
if(e.getSource() == textField)
{
18.
textField.setFocus(false);
19.
passwordField.setFocus(true);
20.
if(passwordField.getText().equals(""))
18.
private Container c;
19.
private JPanel textFieldPanel ;
20.
private JLabel nameLabel;
21.
private JTextField textField;
22.
private JLabel passwordLabel;
24.
23.
private JPasswordField passwordField;
25.
24.
private JLabel passwordResult;
26.
if(e.getSource() == passwordField)
27.
{
passwordResult.setText("密碼尚未輸入,使用者是"+textField.getText());
21.
25.
MyWin(String title)
28.
26.
{
29.
27.
28.
super(title);
c = getContentPane();
else
22.
23.
30.
passwordResult.setText("使用者是" + textField.getText() + " , 密碼是" +
passwordField.getText());
}
if(textField.getText().equals(""))
passwordResult.setText("使用者帳號尚未輸入,密碼是" + passwordField.getText());
else
passwordResult.setText("使用者是" + textField.getText() + " , 密碼是" +
passwordField.getText());
}
31.
}
32.
33.
2020/4/24
1.
1.
}
5
JTEXTAREA文字區域
2020/4/24
JTextArea文字區域:可以輸入或顯示多行文字
 使用”\n”或”\n\r”或”\r”符號換行(視作業系統而定)
 建構子:





JTextArea()
JTextArea(String)
JTextArea(int, int)
JTextArea(String, int,int)
6
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代表幾個字元
2020/4/24
方法
7
範例2:文字區域
//JTextArea練習
2.
import java.awt.*;
3.
import java.awt.event.*;
4.
import javax.swing.*;
5.
import javax.swing.event.*;
6.
class TextAreaWin
7.
{
8.
public static void main(String [] args)
9.
{
10.
MyWin win = new MyWin("文字區練習");
11.
win.setSize(400, 400);
12.
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
14.
16.
}
class MyWin extends JFrame
implements ActionListener
17.
18.
19.
{
super(title);
3.
4.
5.
c = getContentPane();
6.
originalTextArea = new JTextArea("輸入文字", 10, 5);
7.
copyTextArea = new JTextArea(10, 5);
8.
button = new JButton("輸入完成,複製");
9.
button.addActionListener(this);
10.
textAreaPanel = new JPanel(new GridLayout(3,1));
11.
textAreaPanel.add(originalTextArea);
12.
textAreaPanel.add(button);
13.
textAreaPanel.add(copyTextArea);
14.
c.add(textAreaPanel);
15.
}
16.
public void actionPerformed(ActionEvent e)
17.
{
win.setVisible(true);
13.
15.
MyWin(String title)
2.
18.
if(e.getSource() == button)
19.
{
copyTextArea.setText(originalTextArea.getText());
20.
}
21.
{
private Container c;
20.
private JPanel textAreaPanel ;
21.
private JTextArea originalTextArea;
22.
private JButton button;
23.
private JTextArea copyTextArea;
}
22.
23.
2020/4/24
1.
1.
}
8
JSCROLLBAR捲動軸元件
2020/4/24
JScrollBar元件繼承Jcomponent元件
 建構子:

JScrollBar()
 JScrollBar(int)
 JScrollBar(int, int, int, int, int)
第1個int是捲軸的方向:JScrollBar.VERTICAL,
JScrollBar.HORIZONTAL
第2個int是初值
第3個int是可見區域
第4,5個int是最大值和最小值

9
JSCROLLPANE水平和垂直捲軸
2020/4/24
JScrollPane可以為元件同時加上水平和垂直的捲軸
 建構子:

JScrollPane()
 JScrollPane(Component )

10
1.
範例3: 加上捲軸
MyWin(String title)
{ super(title);
2.
3.
c = getContentPane();
//JTextArea練習+JScrollPane
2.
import java.awt.*;
4.
originalPanel = new JPanel(new BorderLayout());
3.
import java.awt.event.*;
5.
originalTextArea = new JTextArea("輸入文字", 10, 5);
4.
import javax.swing.*;
6.
scroll1 = new JScrollPane(originalTextArea);
5.
import javax.swing.event.*;
7.
originalPanel.add(scroll1, BorderLayout.CENTER);
6.
class TextAreaScrollWin
8.
copyPanel = new JPanel(new BorderLayout());
7.
{ public static void main(String [] args)
9.
copyTextArea = new JTextArea("waiting",10, 5);
10.
scroll2 = new JScrollPane(copyTextArea);
11.
copyPanel.add(scroll2, BorderLayout.CENTER);
12.
buttonPanel = new JPanel();
13.
button = new JButton("輸入完成,複製");
{ MyWin win = new MyWin("文字區練習");
8.
9.
win.setSize(400, 400);
10.
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11.
win.setVisible(true);
}
12.
13.
}
14.
button.addActionListener(this);
14.
class MyWin extends JFrame
15.
buttonPanel.add(button);
16.
textAreaPanel = new JPanel(new GridLayout(3,1));
17.
textAreaPanel.add(originalPanel);
15.
16.
implements ActionListener
{ private Container c;
17.
private JPanel textAreaPanel ;
18.
textAreaPanel.add(buttonPanel);
18.
private JPanel originalPanel;
19.
textAreaPanel.add(copyPanel);
19.
private JPanel copyPanel;
20.
c.add(textAreaPanel);
20.
private JPanel buttonPanel;
21.
}
21.
private JTextArea originalTextArea;
22.
private JButton button;
22.
public void actionPerformed(ActionEvent e)
23.
private JTextArea copyTextArea;
23.
{
24.
private JScrollPane scroll1;
25.
private JScrollPane scroll2;
if(e.getSource() == button)
copyTextArea.setText(originalTextArea.getText());
}
1.
2.
}
2020/4/24
1.
11
視窗功能表
2020/4/24
JCheckBoxMenuItem
AbstractButton
JMenuBar
JComponent
JMenuItem
JRadioButtonMenuItem
JMenu
JPopupMenu
JToolBar
JSeparator
12
JPOPUPMENU彈出式選單元件
2020/4/24
13