1. Trang chủ
  2. » Giáo Dục - Đào Tạo

SWING 6 special pane (lập TRÌNH NÂNG CAO SLIDE)

47 20 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Slide 1

  • How to Use Scroll Panes

  • How to Use Scroll Panes

  • How to Use Scroll Panes

  • How to Use Scroll Panes

  • Providing Custom Decorations

  • Providing Custom Decorations

  • Providing Custom Decorations

  • Dynamically Changing the Client's Size

  • Dynamically Changing the Client's Size

  • Dynamically Changing the Client's Size

  • Dynamically Changing the Client's Size

  • Dynamically Changing the Client's Size

  • Dynamically Changing the Client's Size

  • API: JScrollPane

  • API: JScrollPane

  • API: JScrollPane

  • How to Use Split Panes

  • SplitPane Example

  • SplitPane Example

  • How to Use Split Panes

  • How to Use Split Panes

  • How to Use Split Panes

  • How to Use Split Panes

  • Setting the Components in a Split Pane

  • Nesting Split Panes

  • Nesting Split Panes

  • Nesting Split Panes

  • Nesting Split Panes

  • Nesting Split Panes

  • API: JSplitPane

  • API: JSplitPane

  • API: JSplitPane

  • API: JSplitPane

  • How to Use Tabbed Panes

  • How to Use Tabbed Panes

  • API: JTabbed

  • API: JTabbed

  • API: JTabbed

  • API: JTabbed

  • API: JTabbed

  • Border

  • Border

  • Border

  • API: javax.swing.BorderFactory

  • API: javax.swing.BorderFactory

  • API: javax.swing.BorderFactory

Nội dung

PHẦN - SWING SPECIAL PANE How to Use Scroll Panes  A JScrollPane provides a scrollable view of a component When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically textArea = new JTextArea(5, 30); JScrollPane scrollPane = new JScrollPane(textArea); contentPane.setPreferredSize(new Dimension(400, 100)); contentPane.add(scrollPane, BorderLayout.CENTER); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 2/47 How to Use Scroll Panes Khoa CNTT – ĐH Nông Lâm TP HCM 2014 3/47 How to Use Scroll Panes JRadioButton form[][] = new JRadioButton[12][5]; String counts[] = { "", "0-1", "2-5", "6-10", "11-100", "101+" }; String categories[] = { "Household", "Office", "Extended Family", "Company (US)", "Company (World)", "Team", "Will", "Birthday Card List", "High School", "Country", "Continent", "Planet" }; JPanel p = new JPanel( ); p.setSize(600, 400); p.setLayout(new GridLayout(13, 6, 10, 0)); scrollpane = new JScrollPane(p); getContentPane( ).add(scrollpane, BorderLayout.CENTER); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 4/47 How to Use Scroll Panes for (int row = 0; row < 13; row++) { ButtonGroup bg = new ButtonGroup( ); for (int col = 0; col < 6; col++) { if (row == 0) p.add(new JLabel(counts[col])); else if (col == 0) p.add(new JLabel(categories[row - 1])); else { form[row - 1][col - 1] = new JRadioButton( ); bg.add(form[row -1][col - 1]); p.add(form[row -1][col - 1]); } } } Khoa CNTT – ĐH Nông Lâm TP HCM 2014 5/47 Providing Custom Decorations Column Header row Header Vertical Scrollbar Horizotal Scrollbar Khoa CNTT – ĐH Nông Lâm TP HCM 2014 6/47 Providing Custom Decorations Khoa CNTT – ĐH Nông Lâm TP HCM 2014 7/47 Providing Custom Decorations // Add in some JViewports for the column and row headers JViewport jv1 = new JViewport( ); jv1.setView(new JLabel(new ImageIcon("images/blue.gif"))); scrollpane.setColumnHeader(jv1); scrollpane.setColumnHeaderView(new JLabel(new ImageIcon("images/blue.gif"))); JViewport jv2 = new JViewport( ); jv2.setView(new JLabel(new ImageIcon("images/red.gif"))); scrollpane.setRowHeader(jv2); // And throw in an information button JButton jb1 = new JButton(new ImageIcon("images/open.gif")); jb1.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, "This is an Active Corner!", "Information", JOptionPane.INFORMATION_MESSAGE); } } ); scrollpane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, jb1); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 8/47 Size Changing the size of a scroll pane's client is a twostep process First, set the client's preferred size Then, call revalidate on the client to let the scroll pane know that it should update itself and its scroll bars if (changed) { //Update client's preferred size because the area taken up //by the graphics has gotten larger or smaller (if cleared) drawingArea.setPreferredSize(/* the new size */); //This lets the scroll pane know to update itself //and its scroll bars drawingArea.revalidate(); }  Note that when the client changes size, the scroll bars adjust The scroll pane doesn't resize, nor does the viewport Khoa CNTT – ĐH Nông Lâm TP HCM 2014 9/47  Size Khoa CNTT – ĐH Nông Lâm TP HCM 2014 10/47 API: JSplitPane void setOneTouchExpandable(boolean)  boolean getOneTouchExpandable() Set or get whether the split pane displays a control on the divider to expand/collapse the divider The default depends on the look and feel In the Java Look & Feel, it's off by default  void setTopComponent(Component) void setBottomComponent(Component) void setLeftComponent(Component) void setRightComponent(Component) Component getTopComponent() Component getBottomComponent() Component getLeftComponent() Component getRightComponent() Set or get the indicated component Each method works regardless of the split pane's orientation Top and left are equivalent, and bottom and right are equivalent  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 33/47 API: JSplitPane void add(Component) Add the component to the split pane You can add only two components to a split pane The first component added is the top/left component The second component added is the bottom/right component Attempt to add more components result in an exception  void setDividerLocation(double) void setDividerLocation(int) int getDividerLocation() Set or get the current divider location When setting the divider location, you can specify the new location as a percentage (double) or a pixel location (int)  void resetToPreferredSizes() Move the divider such that both components are at their preferred sizes This is how a split pane divides itself at startup, unless specified otherwise  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 34/47 How to Use Tabbed Panes With the JTabbedPane class, you can have several components (usually panels) share the same space The user chooses which component to view by selecting the tab corresponding to the desired component  To create a tabbed pane, you simply instantiate JTabbedPane, create the components you wish it to display, and then add the components to the tabbed pane using the addTab method  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 35/47 How to Use Tabbed Panes Khoa CNTT – ĐH Nông Lâm TP HCM 2014 36/47 API: JTabbed public JTabbedPane() Creates an empty TabbedPane with a default tab placement of JTabbedPane.TOP  public JTabbedPane(int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either: JTabbedPane.TOP, JTabbedPane.BOTTOM, JTabbedPane.LEFT, or JTabbedPane.RIGHT Parameters:tabPlacement - the placement for the tabs relative to the content  public int getSelectedIndex() Returns the currently selected index for this tabbedpane Returns -1 if there is no currently selected tab  public void setSelectedIndex(int index) Sets the selected index for this tabbedpane The index must be a valid tab index or -1, which indicates that no tab should be selected (can also be used when there are no tabs in the tabbedpane) If a -1 value is specified when the tabbedpane contains one or more tabs, then 37/47 Khoa CNTT – ĐH Nông Lâm TP HCM 2014 the results will be implementation defined  API: JTabbed public Component getSelectedComponent() Returns the currently selected component for this tabbedpane Returns null if there is no currently selected tab  public void insertTab(String title, Icon icon, Component component, String tip, int index) Inserts a component, at index, represented by a title and/or icon, either of which may be null If icon is nonnull and it implements ImageIcon a corresponding disabled icon will automatically be created and set on the tabbedpane  public void addTab(String title, Icon icon, Component component, String tip) public void addTab(String title, Icon icon, Component component) public void addTab(String title, Component component) Adds a component and tip represented by a title and/or icon, either of which can be null If icon is non-null and it implements ImageIcon 38/47 will Khoa CNTT –a ĐHcorresponding Nông Lâm TP HCM 2014 disabled icon  API: JTabbed public void removeTabAt(int index) Removes the tab at index After the component associated with index is removed, its visibility is reset to true to ensure it will be visible if added to other containers  public int getTabCount() Returns the number of tabs in this tabbedpane  public void setTitleAt(int index, String title) Sets the title at index to title which can be null An internal exception is raised if there is no tab at that index  public void setIconAt(int index, Icon icon) Sets the icon at index to icon which can be null Does not set disabled icon at icon To set disabled icon, use setDisableIconAt() An internal exception is raised if there is no tab at that index  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 39/47 API: JTabbed public void setToolTipTextAt(int index, String toolTipText) Sets the tooltip text at index to toolTipText which can be null An internal exception is raised if there is no tab at that index  setBackgroundAt public void setBackgroundAt(int index, Color background)  Sets the background color at index to background which can be null, in which case the tab's background color will default to the background color of the tabbedpane  public void setForegroundAt(int index,Color foreground) Sets the foreground color at index to foreground which can be null, in which case the tab's Khoa CNTT – ĐH Nông Lâm TP HCM 2014 40/47 API: JTabbed public void setEnabledAt(int index, boolean enabled) Sets whether or not the tab at index is enabled  public void setTabLayoutPolicy(int tabLayoutPolicy) Sets the policy which the tabbedpane will use in laying out the tabs when all the tabs will not fit within a single run Possible values are: JTabbedPane.WRAP_TAB_LAYOUT JTabbedPane.SCROLL_TAB_LAYOUT The default value, if not set by the UI, is JTabbedPane.WRAP_TAB_LAYOUT Some look and feels might only support a subset of the possible layout policies, in which case the value of this property may be ignored  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 41/47 Border Khoa CNTT – ĐH Nông Lâm TP HCM 2014 42/47 Border If you have multiple groups of radio buttons in a window, you will want to visually indicate which buttons are grouped together The Swing set provides a set of useful borders for this purpose  There are quite a few borders to choose from, but you follow the same steps for all of them Call a static method of the BorderFactory to create a border You can choose among the following styles:  Lowered bevel  Raised bevel  Etched  Line  Matte  Empty (just to create some blank space around the component)  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 43/47 Border 2.If you like, add a title to your border by passing your border to BorderFactory.createTitledBorder 3.If you really want to go all out, combine several borders with a call to BorderFactory.createCompoundBorder 4.Add the resulting border to your component by calling the setBorder method of the JComponent class  For example, here is how you add an etched border with a title to a panel:  Border etched = BorderFactory.createEtchedBorder() Border titled = BorderFactory.createTitledBorder( etched, "A Title"); panel.setBorder(titled); Khoa CNTT – ĐH Nông Lâm TP HCM 2014 44/47 API: javax.swing.BorderFactory static Border createLineBorder(Color color) static Border createLineBorder(Color color, int thickness) create a simple line border  static MatteBorder createMatteBorder(int top, int left, int bottom, int right, Color color) static MatteBorder createMatteBorder(int top, int left, int bottom, int right, Icon tileIcon) create a thick border that is filled with a color or a repeating icon  static Border createEmptyBorder() static Border createEmptyBorder(int top, int left, int bottom, int right) create an empty border  static Border createEtchedBorder() static Border createEtchedBorder(Color highlight, Color shadow) static Border createEtchedBorder(int type)  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 45/47 API: javax.swing.BorderFactory static Border createEtchedBorder(int type, Color highlight, Color shadow) create a line border with a 3D effect Parameters:  highlight, shadowColors for 3D effect   Type One of EtchedBorder.RAISED, EtchedBorder.LOWERED  static Border createBevelBorder(int type) static Border createBevelBorder(int type, Color highlight, Color shadow) static Border createLoweredBevelBorder() static Border createRaisedBevelBorder() create a border that gives the effect of a lowered or raised surface  Parameters:  Type One of BevelBorder.LOWERED, BevelBorder.RAISED   highlight, shadowColors for 3D effect  Khoa CNTT – ĐH Nông Lâm TP HCM 2014 46/47 API: javax.swing.BorderFactory  static TitledBorder createTitledBorder(String title) static TitledBorder createTitledBorder(Border border) static TitledBorder createTitledBorder(Border border, String static TitledBorder createTitledBorder(Border border, String justification, int position) static TitledBorder createTitledBorder(Border border, String justification, int position, Font font) static TitledBorder createTitledBorder(Border border, String justification, int position, Font font, Color color) Parameters:        title) title, int title, int title, int titleThe title string  borderThe border to decorate with the title  Justification One of TitledBorder.LEFT, TitledBorder.CENTER, TitledBorder.RIGHT  Position One of the TitledBorder constants ABOVE_TOP, TOP, BELOW_TOP, ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM  Font The font for the title  Color The color of the title static CompoundBorder createCompoundBorder(Border outsideBorder, Border insideBorder) combines two borders to a new border Khoa CNTT – ĐH Nông Lâm TP HCM 2014 47/47 ... pictureScrollPane = new JScrollPane(picture); //Create a split pane with the two scroll panes in it topSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane); topSplitPane.setOneTouchExpandable(true);... How to Use Split Panes   //Create a split pane with the two scroll panes in it splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane); splitPane.setOneTouchExpandable(true);... "Continent", "Planet" }; JPanel p = new JPanel( ); p.setSize (60 0, 400); p.setLayout(new GridLayout(13, 6, 10, 0)); scrollpane = new JScrollPane(p); getContentPane( ).add(scrollpane, BorderLayout.CENTER);

Ngày đăng: 29/03/2021, 10:53

TỪ KHÓA LIÊN QUAN