Tải bản đầy đủ (.pptx) (47 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (365.64 KB, 47 trang )

PHẦN 2 - 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


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));
Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

11/47


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

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

12/47



Size
class MyMouseListener extends MouseInputAdapter {
final int W = 100;
final int H = 100;
public void mouseReleased(MouseEvent e) {
boolean changed = false;
if (SwingUtilities.isRightMouseButton(e)) {
// This will clear the graphic objects.
objects.removeAllElements();
size.width=0; size.height=0; changed = true;
} else {
int x = e.getX() - W/2;
int y = e.getY() - H/2;
if (x < 0) x = 0;
if (y < 0) y = 0;
Rectangle rect = new Rectangle(x, y, W, H);
objects.addElement(rect);
drawingArea.scrollRectToVisible(rect);
Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

13/47


Size

}}

int this_width = (x + W + 2);

if (this_width > size.width) {
size.width = this_width; changed=true;}
int this_height = (y + H + 2);
if (this_height > size.height) {size.height = this_height;
changed=true;}
}
if (changed) {
//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();
Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

14/47


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.


Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

15/47


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:
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
void setColumnHeaderView(Component)
void setRowHeaderView(Component)
Set the column or row header for the scroll pane.
Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

16/47


API: JScrollPane
public void setViewportView(Component view)
Creates a viewport if necessary and then sets
its view.
JScrollPane scrollpane = new JScrollPane();
scrollpane.setViewportView(myBigComponentToS
croll);
 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


Khoa CNTT – ĐH Nông Lâm TP. HCM 2014


17/47


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.


Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

18/47


SplitPane Example

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014


19/47


SplitPane Example

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

20/47


How to Use Split Panes

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

21/47


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

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

22/47


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);
Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

23/47



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.


Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

24/47


Pane





A program can set a split pane's two components
dynamically with these four methods:
 setLeftComponent
 setRightComponent
 setTopComponent
 setBottomComponent
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.

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

25/47


×