This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
swing:text_fields [2015/05/10 17:41] gthanos [Scroll Panes (Scroll Bars)] |
swing:text_fields [2018/03/27 19:40] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Text Fields, Text Areas & Scroll Panes (Scroll Bars) ====== | ||
| - | |||
| - | <WRAP info 70% round center> | ||
| - | Σε αυτή την ενότητα θα εξετάσουμε τα πεδία κειμένου text field και text area στο οποία το περιεχόμενο κείμενο έχει ενιαία μορφοποίηση, | ||
| - | </ | ||
| - | |||
| - | Ένα text field (υλοποιείται από την κλάση [[http:// | ||
| - | |||
| - | <code java> | ||
| - | int NUMBER_OF_CHAR = 20; | ||
| - | JTextField textField = new JTextField(NUMBER_OF_CHAR); | ||
| - | or | ||
| - | JTextField textField = new JTextField(" | ||
| - | </ | ||
| - | |||
| - | Οι παραπάνω ορισμοί σημαίνουν τα εξής: | ||
| - | - Δημιουργούμε ένα text field στο οποίο θα πρέπει να είναι ορατοί τουλάχιστον 20 χαρακτήρες (μπορεί να είναι και περισσότεροι). | ||
| - | - Ο 2ος κατασκευαστής δημιουργεί το παραπάνω text field που περιέχει το String **" | ||
| - | |||
| - | Εκτός του component JTextField υπάρχει και το component [[http:// | ||
| - | |||
| - | Σε αναλογία με το text field, ένα text area (υλοποιείται από την κλάση [[http:// | ||
| - | |||
| - | <code java> | ||
| - | int NUMBER_OF_CHAR = 20; | ||
| - | int NUMBER_OF_ROWS = 5; | ||
| - | JTextArea textArea = new JTextArea(NUMBER_OF_ROWS, | ||
| - | OR | ||
| - | JTextArea textArea = new JTextArea(" | ||
| - | </ | ||
| - | |||
| - | <WRAP tip 80% center round > | ||
| - | Τόσο η κλάση **JTextArea** όσο και η κλάση **JTextField** είναι απόγονοι της κλάσης [[http:// | ||
| - | </ | ||
| - | |||
| - | ===== Μέθοδοι της κλάσης JTextComponent ===== | ||
| - | |||
| - | ==== set/get text content ==== | ||
| - | |||
| - | Μπορείτε να λάβετε ή να θέσετε το περιεχόμενο ενός text field ή text area μέσα από τις μεθόδους '' | ||
| - | <code java> | ||
| - | String text = textField.getText(); | ||
| - | textField.setText(text + " | ||
| - | </ | ||
| - | |||
| - | Εάν θέλετε να λάβετε το κείμενο που έχει πιθανόν μαρκάρει ο χρήστης μπορείτε να χρησιμοποιήσετε την μέθοδο | ||
| - | <code java> | ||
| - | public String getSelectedText(); | ||
| - | </ | ||
| - | |||
| - | ==== Editable or Non-editable field ==== | ||
| - | |||
| - | Μπορείτε να ορίσετε ένα text component να είναι non-editable ως εξής: | ||
| - | <code java> | ||
| - | textArea.setEditable(false); | ||
| - | textArea.setEditable(true); | ||
| - | </ | ||
| - | |||
| - | ==== Λάβετε την θέση του κέρσορα ή θέστε την θέση αυτή ==== | ||
| - | |||
| - | <code java> | ||
| - | int pos = textArea.getCaretPosition(); | ||
| - | textArea.setCaretPosition(textArea.getText().length()-10);// | ||
| - | </ | ||
| - | |||
| - | ==== Μαρκάρετε περιεχόμενο ==== | ||
| - | |||
| - | <code java> | ||
| - | textArea.setCaretPosition(textArea.getText().length()-10);// | ||
| - | textArea.moveCaretPosition(10); | ||
| - | |||
| - | OR | ||
| - | |||
| - | textArea.select(textArea.getText().length()-10, | ||
| - | </ | ||
| - | |||
| - | ==== Αλλάξτε το χρώμα των χαρακτήρων ή το χρώμα στο background ==== | ||
| - | |||
| - | Η αλλαγή στο χρώμα των χαρακτήρων μπορεί να γίνει μέσα από την μέθοδο '' | ||
| - | <code java> | ||
| - | textArea.setForeground(Color.RED); | ||
| - | </ | ||
| - | |||
| - | Αντίστοιχα, | ||
| - | <code java> | ||
| - | textArea.setBackground(Color.BLUE); | ||
| - | </ | ||
| - | |||
| - | ===== Μέθοδοι των κλάσεων JTextField και JTextArea ===== | ||
| - | |||
| - | Είναι πιθανό σε ένα text field ή text area να θέλετε να ορίσετε την δική σας γραμματοσειρά. Αυτό μπορείτε να το κάνετε με την βοήθεια της κλάσης [[http:// | ||
| - | |||
| - | <code java> | ||
| - | void setFont(Font f); | ||
| - | </ | ||
| - | |||
| - | ==== Line Wrap σε text area ==== | ||
| - | |||
| - | Σας δίνεται η δυνατότητα να ορίσετε σε ένα text area να αναδιπλώνει τις γραμμές του περιεχομένου προσαρμοζόμενο στο μέγεθος διαθέσιμων στηλών. Η default συμπεριφορά του text area είναι να μην κάνει αναδίπλωση γραμμών. Εάν δεν γίνεται αναδίπλωση, | ||
| - | <code java> | ||
| - | textArea.setLineWrap(true); | ||
| - | textArea.setWrapStyleWord(true); | ||
| - | </ | ||
| - | |||
| - | ===== Scroll Panes (Scroll Bars) ===== | ||
| - | |||
| - | Όταν ορίζετε ένα text area ορίζετε ένα ελάχιστο εύρος γραμμών και στηλών στις οποίες εκτείνεται το συγκεκριμένο text area, π.χ. | ||
| - | <code java> | ||
| - | JTextArea textArea = new JTextArea(NUMBER_OF_ROWS, | ||
| - | </ | ||
| - | Ο χρήστης μπορεί να εισάγει κείμενο που εκτείνεται έξω από τα παραπάνω όρια. Εάν δεν έχετε ορίσει την ιδιότητα της αναδίπλωσης γραμμών στο text area, το κείμενο που βρίσκεται εκτός των παραπάνω ορίων μπορεί να φανεί μόνο αν ορίσετε | ||
| - | |||
| - | Scroll bars ορίζονται από το component [[http:// | ||
| - | |||
| - | {{ : | ||
| - | |||
| - | Για να ορίσετε ένα text area μέσα σε ένα JScrollPane απαιτείται μία δήλωση της μορφής | ||
| - | <code java> | ||
| - | JScrollPane scolledTextArea = new JScrollPane(textArea); | ||
| - | </ | ||
| - | |||
| - | Σε κάθε JScrollPane object ορίζεται την συμπεριφορά του οριζόντιου ScrollBar από την παρακάτω μέθοδο οι οποία λαμβάνει ως ορίσματα τις τιμές που ακολουθούν. | ||
| - | <code java> | ||
| - | public void setHorizontalScrollBarPolicy(int policy) | ||
| - | |||
| - | // επιτρεπόμενες τιμές | ||
| - | JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS | ||
| - | JScrollPane.HORIZONTAL_SCROLLBAR_NEVER | ||
| - | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED | ||
| - | </ | ||
| - | |||
| - | Αντίστοιχα για το κάθετο ScrollBar | ||
| - | <code java> | ||
| - | public void setVerticalScrollBarPolicy(int policy) | ||
| - | |||
| - | // επιτρεπόμενες τιμές | ||
| - | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS | ||
| - | JScrollPane.VERTICAL_SCROLLBAR_NEVER | ||
| - | JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED | ||
| - | </ | ||
| - | |||
| - | ===== Παραδείγματα ===== | ||
| - | |||
| - | ==== Παράδειγμα 1ο ==== | ||
| - | |||
| - | Παράδειγμα χρήσης των παραπάνω components δίνεται παρακάτω (__απλουστευμένο__ από το [[http:// | ||
| - | |||
| - | <code java TextSamplerDemo.java> | ||
| - | package components; | ||
| - | |||
| - | import javax.swing.*; | ||
| - | import javax.swing.text.*; | ||
| - | |||
| - | import java.awt.*; | ||
| - | import java.awt.event.*; | ||
| - | |||
| - | import java.net.URL; | ||
| - | import java.io.IOException; | ||
| - | |||
| - | public class TextSamplerDemo extends JPanel | ||
| - | | ||
| - | private String newline = " | ||
| - | protected static final String textFieldString = " | ||
| - | protected static final String passwordFieldString = " | ||
| - | |||
| - | protected JLabel actionLabel; | ||
| - | |||
| - | public TextSamplerDemo() { | ||
| - | setLayout(new BorderLayout()); | ||
| - | |||
| - | //Create a regular text field. | ||
| - | JTextField textField = new JTextField(10); | ||
| - | textField.setActionCommand(textFieldString); | ||
| - | textField.addActionListener(this); | ||
| - | |||
| - | //Create a password field. | ||
| - | JPasswordField passwordField = new JPasswordField(10); | ||
| - | passwordField.setActionCommand(passwordFieldString); | ||
| - | passwordField.addActionListener(this); | ||
| - | |||
| - | //Create some labels for the fields. | ||
| - | JLabel textFieldLabel = new JLabel(textFieldString + ": "); | ||
| - | textFieldLabel.setLabelFor(textField); | ||
| - | JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": "); | ||
| - | passwordFieldLabel.setLabelFor(passwordField); | ||
| - | |||
| - | //Create a label to put messages during an action event. | ||
| - | actionLabel = new JLabel(" | ||
| - | actionLabel.setBorder(BorderFactory.createEmptyBorder(10, | ||
| - | |||
| - | //Lay out the text controls and the labels. | ||
| - | JPanel textControlsPane = new JPanel(); | ||
| - | GridBagLayout gridbag = new GridBagLayout(); | ||
| - | GridBagConstraints c = new GridBagConstraints(); | ||
| - | |||
| - | textControlsPane.setLayout(gridbag); | ||
| - | |||
| - | JLabel[] labels = {textFieldLabel, | ||
| - | JTextField[] textFields = {textField, passwordField }; | ||
| - | addLabelTextRows(labels, | ||
| - | |||
| - | c.gridwidth = GridBagConstraints.REMAINDER; | ||
| - | c.anchor = GridBagConstraints.WEST; | ||
| - | c.weightx = 1.0; | ||
| - | textControlsPane.add(actionLabel, | ||
| - | textControlsPane.setBorder( | ||
| - | BorderFactory.createCompoundBorder( | ||
| - | BorderFactory.createTitledBorder(" | ||
| - | BorderFactory.createEmptyBorder(5, | ||
| - | |||
| - | //Create a text area. | ||
| - | JTextArea textArea = new JTextArea( | ||
| - | "This is an editable JTextArea. " + | ||
| - | "A text area is a \" | ||
| - | "which means that although it can display text " + | ||
| - | "in any font, all of the text is in the same font." | ||
| - | ); | ||
| - | textArea.setFont(new Font(" | ||
| - | textArea.setLineWrap(true); | ||
| - | textArea.setWrapStyleWord(true); | ||
| - | JScrollPane areaScrollPane = new JScrollPane(textArea); | ||
| - | areaScrollPane.setVerticalScrollBarPolicy( | ||
| - | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | ||
| - | areaScrollPane.setPreferredSize(new Dimension(250, | ||
| - | areaScrollPane.setBorder( | ||
| - | BorderFactory.createCompoundBorder( | ||
| - | BorderFactory.createCompoundBorder( | ||
| - | BorderFactory.createTitledBorder(" | ||
| - | BorderFactory.createEmptyBorder(5, | ||
| - | areaScrollPane.getBorder())); | ||
| - | |||
| - | //Put everything together. | ||
| - | JPanel leftPane = new JPanel(new BorderLayout()); | ||
| - | leftPane.add(textControlsPane, | ||
| - | | ||
| - | leftPane.add(areaScrollPane, | ||
| - | | ||
| - | |||
| - | add(leftPane, | ||
| - | } | ||
| - | |||
| - | private void addLabelTextRows(JLabel[] labels, | ||
| - | JTextField[] textFields, | ||
| - | GridBagLayout gridbag, | ||
| - | Container container) { | ||
| - | GridBagConstraints c = new GridBagConstraints(); | ||
| - | c.anchor = GridBagConstraints.EAST; | ||
| - | int numLabels = labels.length; | ||
| - | |||
| - | for (int i = 0; i < numLabels; i++) { | ||
| - | c.gridwidth = GridBagConstraints.RELATIVE; | ||
| - | c.fill = GridBagConstraints.NONE; | ||
| - | c.weightx = 0.0; // | ||
| - | container.add(labels[i], | ||
| - | |||
| - | c.gridwidth = GridBagConstraints.REMAINDER; | ||
| - | c.fill = GridBagConstraints.HORIZONTAL; | ||
| - | c.weightx = 1.0; | ||
| - | container.add(textFields[i], | ||
| - | } | ||
| - | } | ||
| - | |||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | String prefix = "You typed \""; | ||
| - | if (textFieldString.equals(e.getActionCommand())) { | ||
| - | JTextField source = (JTextField)e.getSource(); | ||
| - | actionLabel.setText(prefix + source.getText() + " | ||
| - | } else if (passwordFieldString.equals(e.getActionCommand())) { | ||
| - | JPasswordField source = (JPasswordField)e.getSource(); | ||
| - | actionLabel.setText(prefix + new String(source.getPassword()) | ||
| - | + " | ||
| - | } | ||
| - | } | ||
| - | |||
| - | /** | ||
| - | * Create the GUI and show it. For thread safety, | ||
| - | * this method should be invoked from the | ||
| - | * event dispatch thread. | ||
| - | */ | ||
| - | private static void createAndShowGUI() { | ||
| - | //Create and set up the window. | ||
| - | JFrame frame = new JFrame(" | ||
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | |||
| - | //Add content to the window. | ||
| - | frame.add(new TextSamplerDemo()); | ||
| - | |||
| - | //Display the window. | ||
| - | frame.pack(); | ||
| - | frame.setVisible(true); | ||
| - | } | ||
| - | |||
| - | public static void main(String[] args) { | ||
| - | //Schedule a job for the event dispatching thread: | ||
| - | //creating and showing this application' | ||
| - | SwingUtilities.invokeLater(new Runnable() { | ||
| - | public void run() { | ||
| - | // | ||
| - | UIManager.put(" | ||
| - | createAndShowGUI(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | ==== Παράδειγμα 2ο ==== | ||
| - | |||
| - | Το 2ο παράδειγμα είναι από το βιβλίο του Savitch. | ||
| - | <code java ScollBarDemo.java> | ||
| - | import javax.swing.*; | ||
| - | import java.awt.*; | ||
| - | import java.awt.event.*; | ||
| - | |||
| - | public class ScrollBarDemo extends JFrame implements ActionListener { | ||
| - | public static final int WIDTH = 600; | ||
| - | public static final int HEIGHT = 400; | ||
| - | public static final int LINES = 15; | ||
| - | public static final int CHAR_PER_LINE = 30; | ||
| - | private JTextArea memoDisplay; | ||
| - | private String memo1; | ||
| - | private String memo2; | ||
| - | |||
| - | public static void main(String[] args) { | ||
| - | ScrollBarDemo gui = new ScrollBarDemo(); | ||
| - | gui.setVisible(true); | ||
| - | } | ||
| - | | ||
| - | public ScrollBarDemo() { | ||
| - | super(" | ||
| - | setSize(WIDTH, | ||
| - | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | JPanel buttonPanel = new JPanel(); | ||
| - | buttonPanel.setBackground(Color.LIGHT_GRAY); | ||
| - | buttonPanel.setLayout(new FlowLayout()); | ||
| - | JButton memo1Button = new JButton(" | ||
| - | memo1Button.addActionListener(this); | ||
| - | buttonPanel.add(memo1Button); | ||
| - | JButton memo2Button = new JButton(" | ||
| - | memo2Button.addActionListener(this); | ||
| - | buttonPanel.add(memo2Button); | ||
| - | JButton clearButton = new JButton(" | ||
| - | clearButton.addActionListener(this); | ||
| - | buttonPanel.add(clearButton); | ||
| - | JButton get1Button = new JButton(" | ||
| - | get1Button.addActionListener(this); | ||
| - | buttonPanel.add(get1Button); | ||
| - | JButton get2Button = new JButton(" | ||
| - | get2Button.addActionListener(this); | ||
| - | buttonPanel.add(get2Button); | ||
| - | add(buttonPanel, | ||
| - | JPanel textPanel = new JPanel(); | ||
| - | textPanel.setBackground(Color.BLUE); | ||
| - | |||
| - | memoDisplay = new JTextArea(LINES, | ||
| - | memoDisplay.setBackground(Color.WHITE); | ||
| - | JScrollPane scrolledText = new JScrollPane(memoDisplay); | ||
| - | scrolledText.setHorizontalScrollBarPolicy( | ||
| - | JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); | ||
| - | scrolledText.setVerticalScrollBarPolicy( | ||
| - | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | ||
| - | textPanel.add(scrolledText); | ||
| - | add(textPanel, | ||
| - | } | ||
| - | | ||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | String actionCommand = e.getActionCommand(); | ||
| - | if (actionCommand.equals(" | ||
| - | memo1 = memoDisplay.getText(); | ||
| - | else if (actionCommand.equals(" | ||
| - | memo2 = memoDisplay.getText(); | ||
| - | else if (actionCommand.equals(" | ||
| - | memoDisplay.setText("" | ||
| - | else if (actionCommand.equals(" | ||
| - | memoDisplay.setText(memo1); | ||
| - | else if (actionCommand.equals(" | ||
| - | memoDisplay.setText(memo2); | ||
| - | else | ||
| - | memoDisplay.setText(" | ||
| - | } | ||
| - | | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||
| - | |||