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

Thông tin cơ bản

Định dạng
Số trang 47
Dung lượng 391,61 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

You can divide screen space among three or more components by putting split panes inside of split panes Instead of adding the components of interest directly to a split pane, you often

Trang 1

PHẦN 2 - SWING

SPECIAL PANE

Trang 2

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);

Trang 3

How to Use Scroll Panes

Trang 4

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 );

Trang 5

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( );

Trang 6

Providing Custom Decorations

Column Header

row Header

Horizotal Scrollbar

Vertical Scrollbar

Trang 7

Providing Custom Decorations

Trang 8

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" )));

// And throw in an information button

JButton jb1 = new JButton( new ImageIcon( "images/open.gif" ));

jb1.addActionListener( new ActionListener( ) {

public void actionPerformed(ActionEvent ae) {

Trang 9

Dynamically Changing the Client's Size

Changing the size of a scroll pane's client is a two-step

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

Trang 10

Dynamically Changing the Client's Size

Trang 11

Dynamically Changing the Client's Size

drawingArea = new JPanel() {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Rectangle rect;

for (int i = 0; i < objects.size(); i++) {

rect = (Rectangle)objects.elementAt(i);

g.setColor(colors[(i % color_n)]);

g.fillOval(rect.x, rect.y, rect.width, rect.height);

}

}

};

drawingArea.setBackground(Color.white);

drawingArea.addMouseListener(new MyMouseListener());

//Put the drawing area in a scroll pane

JScrollPane scroller = new JScrollPane(drawingArea);

scroller.setPreferredSize(new Dimension(200,200));

Trang 12

Dynamically Changing the Client's Size

//Set up the instructions

JLabel instructionsLeft = new JLabel(

"Click left mouse button to place a circle." );

JLabel instructionsRight = new JLabel(

"Click right mouse button to clear drawing area." ); JPanel instructionPanel = new JPanel( new GridLayout(0,1)); instructionPanel.add(instructionsLeft);

instructionPanel.add(instructionsRight);

//Layout this demo.

setLayout( new BorderLayout());

add(instructionPanel, BorderLayout NORTH );

add(scroller, BorderLayout CENTER );

Trang 13

Dynamically Changing the Client's Size

class MyMouseListener extends MouseInputAdapter {

final int W = 100;

final int H = 100;

public void mouseReleased(MouseEvent e) {

boolean changed = false;

Trang 14

Dynamically Changing the Client's Size

int this_width = (x + W + 2);

if (this_width > size.width) {

size.width = this_width; changed=true;}

//Update client's preferred size because

//the area taken up by the graphics has

//gotten larger or smaller (if cleared)

drawingArea.setPreferredSize(size);

//Let the scroll pane know to update itself

//and its scrollbars

drawingArea.revalidate();

}

drawingArea.repaint();

}}

Trang 15

API: JScrollPane

public JScrollPane( )

public JScrollPane(Component view)

public JScrollPane(Component view, int

verticalScrollBarPolicy, int horizontalScrollBarPolicy)

public JScrollPane(int verticalScrollBarPolicy, int

horizontalScrollBarPolicy)

 Create new scrollpanes You can start off by

specifying the view (i.e., the component to scroll), the scrollbar policies, or both Just make sure you get the scrollbar policies in the right order! Of

course, any of these pieces can be specified or

changed after the scrollpane has been created See the setViewportView( ) method later in this

chapter.

Trang 16

API: JScrollPane

public void setVerticalScrollBarPolicy(int policy)

Determines when the vertical scrollbar appears in the

scrollpane Legal values are:

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED

JScrollPane.VERTICAL_SCROLLBAR_NEVER

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS

public void setHorizontalScrollBarPolicy(int policy)

Determines when the horizontal scrollbar appears in the scrollpane The options are:

Trang 17

API: JScrollPane

 public void setViewportView(Component view)

Creates a viewport if necessary and then sets its view JScrollPane scrollpane = new JScrollPane();

scrollpane.setViewportView(myBigComponentToScroll);

 public void setCorner(String key,Component corner)

Set the corner specified Legal values for the key are: JScrollPane.LOWER_LEFT_CORNER

JScrollPane.LOWER_RIGHT_CORNER

JScrollPane.UPPER_LEFT_CORNER

JScrollPane.UPPER_RIGHT_CORNER

Trang 18

How to Use Split Panes

The JSplitPane component allows you to place two (and only two) components side by side or one on top of the other in a

single pane You can separate the pane horizontally or vertically, and the user can adjust this separator graphically at runtime

You can divide screen space among three or more components

by putting split panes inside of split panes

Instead of adding the components of interest directly to a split pane, you often put each component into a scroll pane You

then put the scroll panes into the split pane This allows the user

to view any part of a component of interest, without requiring the component to take up a lot of screen space or adapt to

displaying itself in varying amounts of screen space

Trang 19

SplitPane Example

Trang 20

SplitPane Example

Trang 21

How to Use Split Panes

Trang 22

How to Use Split Panes

 //Create a split pane with the two scroll panes in it

splitPane = new

JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane);

Trang 23

How to Use Split Panes

 //Create a split pane with the two scroll panes

splitPane = new

JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane);

splitPane.setOneTouchExpandable(true);

splitPane.setDividerLocation(150);

//Provide minimum sizes for the two components

//in the split pane

Dimension minimumSize = new Dimension(100, 50);

listScrollPane.setMinimumSize(minimumSize);

pictureScrollPane.setMinimumSize(minimumSize);

Trang 24

How to Use Split Panes

The split pane in this example is split horizontally the two

components appear side by side as specified by the

JSplitPane.HORIZONTAL_SPLIT argument to the constructor

Split pane provides one other option, specified with

JSplitPane.VERTICAL_SPLIT, that places one component above the other You can change the split direction after the split pane has been created with the setOrientation method

Two small arrows appear at the top of the divider in the

example's split pane These arrows let the user collapse (and then expand) either of the components with a single click The current look and feel determines whether these controls appear

by default In the Java Look & Feel, they are turned off by

default The example turned them on with a call to the

setOneTouchExpandable

Trang 25

Setting the Components in a Split

 You can use any of these methods at any time regardless of

the split pane's current split direction Calls to

setLeftComponent and setTopComponent are equivalent and set the specified component in the top or left position,

depending on the split pane's current split orientation

Similarly, calls to setRightComponent and setBottomComponent are equivalent

Trang 26

Nesting Split Panes

Trang 27

Nesting Split Panes

public class NestingSplitPaneDemo extends JFrame

implements ListSelectionListener {

private Vector imageNames;

private JLabel picture;

private JList list;

private JSplitPane topSplitPane,splitPane;

private JScrollPane listScrollPane, pictureScrollPane;

private JLabel label;

public NestingSplitPaneDemo(String title) {

super(title);

//Create the list of images and put it in a scroll pane

imageNames = new Vector();

Trang 28

Nesting Split Panes

//Set up the picture label and put it in a scroll pane

ImageIcon firstImage = new ImageIcon("images/" +

(String)imageNames.firstElement());

picture = new JLabel(firstImage);

picture.setPreferredSize(new Dimension(firstImage.getIconWidth(), firstImage.getIconHeight()));

pictureScrollPane = new JScrollPane(picture);

//Create a split pane with the two scroll panes in it

topSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,

Trang 29

Nesting Split Panes

//Provide a preferred size for the split pane

topSplitPane.setPreferredSize(new Dimension(400, 200));

label = new JLabel("Click on an image name in the list.",

JLabel.CENTER);

// Create a split pane and put "top" (topSplitPane) and

// JLabel instance in it.

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topSplitPane, label);

Trang 30

Nesting Split Panes

public void valueChanged(ListSelectionEvent e) {

int index = theList.getSelectedIndex();

label.setText("Selected image number " + index);

ImageIcon newImage = new ImageIcon("images/" +

} }

Trang 31

API: JSplitPane

public JSplitPane()

public JSplitPane(int newOrientation)

public JSplitPane(int newOrientation, boolean

newContinuousLayout)

public JSplitPane(int newOrientation, Component

newLeftComponent,Component newRightComponent)

public JSplitPane(int newOrientation, boolean

newContinuousLayout, Component newLeftComponent,

Component newRightComponent)

Parameters:

newOrientation - HORIZONTAL_SPLIT or VERTICAL_SPLIT

newContinuousLayout - true for the components to redraw

continuously as the divider changes position, false to wait until the divider position stops changing to redraw

newLeftComponent - the Component that will appear on the left

of a horizontally-split pane, or at the top of a vertically-split panenewRightComponent - the Component that will appear on the right of a horizontally-split pane, or at the bottom of a vertically-

Trang 32

API: JSplitPane

Set or get the split pane's orientation Use either

HORIZONTAL_SPLIT or VERTICAL_SPLIT defined in JSplitPane If left unspecified, the split pane will be

Trang 33

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

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

Trang 34

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

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

Trang 35

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

Trang 36

How to Use Tabbed Panes

Trang 37

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 the results will be

Trang 38

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 non-null 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 a corresponding disabled icon will automatically be created and set on the tabbedpane Cover method for insertTab

Trang 39

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

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

TỪ KHÓA LIÊN QUAN

w