This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
swing:borders [2018/04/13 10:28] |
swing:borders [2018/04/13 11:28] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Borders ====== | ||
| + | |||
| + | Η Java υποστηρίζει μία σειρά από κλάσεις που υλοποιούν το // | ||
| + | |||
| + | Πιο συγκεκριμένα οι κλάση javax.swing.BorderFactory μπορεί να παράγει τους παρακάτω βασικούς τύπους από borders (αναφέρουμε τους πιο βασικούς): | ||
| + | - **Empty Border:** Δημιουργεί ένα μη ορατό πλαίσιο γύρω από το αντικείμενο. Το πλαίσιο μπορεί να πιάνει χώρο παρά το γεγονός ότι είναι αόρατο. | ||
| + | - **Line Border:** Δημιουργεί ένα πλαίσιο που περιγράφεται από μία γραμμή. Μπορείτε να ορίσετε το χρώμα της γραμμής και το πάχος της σε κάθε πλευρά. | ||
| + | - **Bevelled Border:** Δημιουργεί ένα πλαίσιο που εμφανίζει την περιοχή μέσα σε αυτό λίγο πιο πάνω ή πιο κάτω από την γύρω περιοχή (δείτε το παρακάτω παρακάτω). | ||
| + | - **Titled Border:** Δημιουργεί ένα πλαίσιο που περιέχει και ένα //String// ως τίτλο πλαισίου. Συνήθως παράγεται από ένα υφιστάμενο αντικείμενο πλαισίου στο οποίο προστίθεται τίτλος. Μπορείτε να ορίσετε την θέση του τίτλου. | ||
| + | - **Dashed Border:** Δημιουργεί ένα πλαίσιο που περιέχει γραμμές διακεκομμένες. Μπορείτε να ορίσετε το χρώμα, το πάχος κάθε πλευράς, | ||
| + | - **Compound Border:** Δημιουργεί ένα πλαίσιο που αποτελεί συνδυασμό δύο υφιστάμενων πλαισίων, | ||
| + | |||
| + | Δείτε το παρακάτω παράδειγμα το οποίο εμφανίζει παραλλαγές από τους παραπάνω τύπους borders. Τα borders σε όλες τις περιπτώσεις του παραδείγματος περιβάλλουν ένα αντικείμενο τύπου javax.swing.JLabel που περιέχει κείμενο. | ||
| + | |||
| + | <code java BorderDemo.java> | ||
| + | import java.awt.*; | ||
| + | import java.awt.event.*; | ||
| + | import javax.swing.BorderFactory; | ||
| + | import javax.swing.border.Border; | ||
| + | import javax.swing.border.TitledBorder; | ||
| + | import javax.swing.border.EtchedBorder; | ||
| + | import javax.swing.ImageIcon; | ||
| + | import javax.swing.JTabbedPane; | ||
| + | import javax.swing.JLabel; | ||
| + | import javax.swing.JPanel; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.Box; | ||
| + | import javax.swing.BoxLayout; | ||
| + | |||
| + | /* | ||
| + | * BorderDemo.java requires the following file: | ||
| + | | ||
| + | */ | ||
| + | public class BorderDemo extends JPanel { | ||
| + | public BorderDemo() { | ||
| + | super(new GridLayout(1, | ||
| + | |||
| + | //Keep references to the next few borders, | ||
| + | //for use in titles and compound borders. | ||
| + | Border blackline, raisedetched, | ||
| + | | ||
| + | |||
| + | //A border that puts 10 extra pixels at the sides and | ||
| + | //bottom of each pane. | ||
| + | Border paneEdge = BorderFactory.createEmptyBorder(0, | ||
| + | |||
| + | blackline = BorderFactory.createLineBorder(Color.black); | ||
| + | raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); | ||
| + | loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); | ||
| + | raisedbevel = BorderFactory.createRaisedBevelBorder(); | ||
| + | loweredbevel = BorderFactory.createLoweredBevelBorder(); | ||
| + | empty = BorderFactory.createEmptyBorder(); | ||
| + | |||
| + | //First pane: simple borders | ||
| + | JPanel simpleBorders = new JPanel(); | ||
| + | simpleBorders.setBorder(paneEdge); | ||
| + | simpleBorders.setLayout(new BoxLayout(simpleBorders, | ||
| + | BoxLayout.Y_AXIS)); | ||
| + | |||
| + | addCompForBorder(blackline, | ||
| + | | ||
| + | addCompForBorder(raisedetched, | ||
| + | | ||
| + | addCompForBorder(loweredetched, | ||
| + | | ||
| + | addCompForBorder(raisedbevel, | ||
| + | | ||
| + | addCompForBorder(loweredbevel, | ||
| + | | ||
| + | addCompForBorder(empty, | ||
| + | | ||
| + | |||
| + | //Second pane: dashed borders | ||
| + | JPanel dashedBorders = new JPanel(); | ||
| + | dashedBorders.setBorder(paneEdge); | ||
| + | dashedBorders.setLayout(new BoxLayout(dashedBorders, | ||
| + | BoxLayout.Y_AXIS)); | ||
| + | |||
| + | Border border = BorderFactory.createDashedBorder(Color.CYAN, | ||
| + | | ||
| + | addCompForBorder(border, | ||
| + | " | ||
| + | | ||
| + | |||
| + | border = BorderFactory.createDashedBorder(Color.BLUE, | ||
| + | addCompForBorder(border, | ||
| + | " | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | //Third pane: titled borders | ||
| + | JPanel titledBorders = new JPanel(); | ||
| + | titledBorders.setBorder(paneEdge); | ||
| + | titledBorders.setLayout(new BoxLayout(titledBorders, | ||
| + | BoxLayout.Y_AXIS)); | ||
| + | TitledBorder titled; | ||
| + | |||
| + | titled = BorderFactory.createTitledBorder(" | ||
| + | addCompForBorder(titled, | ||
| + | " | ||
| + | + " (default just., default pos.)", | ||
| + | | ||
| + | |||
| + | titled = BorderFactory.createTitledBorder( | ||
| + | blackline, " | ||
| + | addCompForTitledBorder(titled, | ||
| + | " | ||
| + | + " (centered, default pos.)", | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | titled = BorderFactory.createTitledBorder(loweredetched, | ||
| + | addCompForTitledBorder(titled, | ||
| + | " | ||
| + | + " (right just., default pos.)", | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | titled = BorderFactory.createTitledBorder( | ||
| + | loweredbevel, | ||
| + | addCompForTitledBorder(titled, | ||
| + | " | ||
| + | + " (default just., above top)", | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | titled = BorderFactory.createTitledBorder( | ||
| + | empty, " | ||
| + | addCompForTitledBorder(titled, | ||
| + | + " (default just., bottom)", | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | //Fourth pane: compound borders | ||
| + | JPanel compoundBorders = new JPanel(); | ||
| + | compoundBorders.setBorder(paneEdge); | ||
| + | compoundBorders.setLayout(new BoxLayout(compoundBorders, | ||
| + | BoxLayout.Y_AXIS)); | ||
| + | Border redline = BorderFactory.createLineBorder(Color.red); | ||
| + | |||
| + | Border compound; | ||
| + | compound = BorderFactory.createCompoundBorder( | ||
| + | raisedbevel, | ||
| + | addCompForBorder(compound, | ||
| + | | ||
| + | |||
| + | compound = BorderFactory.createCompoundBorder( | ||
| + | redline, compound); | ||
| + | addCompForBorder(compound, | ||
| + | | ||
| + | |||
| + | titled = BorderFactory.createTitledBorder( | ||
| + | compound, " | ||
| + | TitledBorder.CENTER, | ||
| + | TitledBorder.BELOW_BOTTOM); | ||
| + | addCompForBorder(titled, | ||
| + | " | ||
| + | + " (centered, below bottom)", | ||
| + | | ||
| + | |||
| + | JTabbedPane tabbedPane = new JTabbedPane(); | ||
| + | tabbedPane.addTab(" | ||
| + | tabbedPane.addTab(" | ||
| + | tabbedPane.addTab(" | ||
| + | tabbedPane.addTab(" | ||
| + | tabbedPane.setSelectedIndex(0); | ||
| + | String toolTip = new String("< | ||
| + | tabbedPane.setToolTipTextAt(1, | ||
| + | |||
| + | add(tabbedPane); | ||
| + | } | ||
| + | |||
| + | void addCompForTitledBorder(TitledBorder border, | ||
| + | String description, | ||
| + | int justification, | ||
| + | int position, | ||
| + | Container container) { | ||
| + | border.setTitleJustification(justification); | ||
| + | border.setTitlePosition(position); | ||
| + | addCompForBorder(border, | ||
| + | | ||
| + | } | ||
| + | |||
| + | void addCompForBorder(Border border, | ||
| + | String description, | ||
| + | Container container) { | ||
| + | JPanel comp = new JPanel(new GridLayout(1, | ||
| + | JLabel label = new JLabel(description, | ||
| + | comp.add(label); | ||
| + | comp.setBorder(border); | ||
| + | |||
| + | container.add(Box.createRigidArea(new Dimension(0, | ||
| + | container.add(comp); | ||
| + | } | ||
| + | |||
| + | |||
| + | /** Returns an ImageIcon, or null if the path was invalid. */ | ||
| + | protected static ImageIcon createImageIcon(String path, | ||
| + | | ||
| + | java.net.URL imgURL = BorderDemo.class.getResource(path); | ||
| + | if (imgURL != null) { | ||
| + | return new ImageIcon(imgURL, | ||
| + | } else { | ||
| + | System.err.println(" | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Create the GUI and show it. For thread safety, | ||
| + | * this method should be invoked from the | ||
| + | * event-dispatching thread. | ||
| + | */ | ||
| + | private static void createAndShowGUI() { | ||
| + | //Create and set up the window. | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | |||
| + | //Create and set up the content pane. | ||
| + | BorderDemo newContentPane = new BorderDemo(); | ||
| + | newContentPane.setOpaque(true); | ||
| + | frame.setContentPane(newContentPane); | ||
| + | |||
| + | //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' | ||
| + | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| + | public void run() { | ||
| + | createAndShowGUI(); | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | | Προηγούμενο: | ||
| + | |||