Академический Документы
Профессиональный Документы
Культура Документы
Java Swing
Tabbed Panes, Scrolling Panes, Split Panes
I. SCOPUL LUCRRII
Lucrarea de fa are rolul de a prezenta i familiariza studentul cu modul de
construire a unei interfee grafice utilizator folosind pachetul de clase javax.swing. Se
vor prezenta cteva componente vizuale utile, mpreun cu modul de creare i utilizare a
acestora.
La sfritul acestei lucrri, studentul va avea posibilitatea s scrie programe Java n
care s utilizeze noiunile nvate.
getContentPane().add( topPanel );
JPanel yAxisPanel = createYAxisPanel();
topPanel.add( yAxisPanel, BorderLayout.CENTER );
JPanel xAxisPanel = createXAxisPanel();
topPanel.add( xAxisPanel, BorderLayout.SOUTH );
}
public JPanel createYAxisPanel()
{
JPanel panel = new JPanel();
panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) );
panel.setBackground( Color.lightGray );
panel.add( new JButton( "Button 1" ) );
panel.add( new TextArea( "This is a text area" ) );
panel.add( new JCheckBox( "Checkbox 1" ) );
return panel;
}
public JPanel createXAxisPanel()
{
JPanel panel = new JPanel();
panel.setLayout( new BoxLayout( panel, BoxLayout.X_AXIS ) );
panel.setBackground( Color.gray );
panel.add( new JButton( "Button 1" ) );
panel.add( new TextArea( "This is a text area" ) );
panel.add( new JCheckBox( "Checkbox 1" ) );
return panel;
}
public static void main( String args[] )
{
TestFrame mainFrame = new TestFrame();
mainFrame.pack();
mainFrame.setVisible( true );
}
}
2. Tabbed Panes
O component de tipul tabbed pane permite gruparea mai multor pagini de
informaie ntr-un singur punct de referin. Se comport ca orice alt component
Swing: putem s i adugm un panou (panel), s i adugm componente de obicei sub
form de pagini. Fiecrei pagini i putem asocia alte componente Java UI.
Crearea unui tabbed pane
import javax.swing.*;
{
. . .
tabbedPanel = new JTabbedPane();
topPanel.add( tabbedPanel, BorderLayout.CENTER );
. . .
}
Se va utiliza cod similar pentru crearea fiecrei pagini. Dar o astfel de secven de
cod adaug paginile secvenial. Pentru a insera pagini oriunde ntr-o ierarhie de pagini se
utilizeaz:
// Insereaza pagina in tabbed pane
tabbedPanel.insertTab( "Inserted Page",
new ImageIcon( "image.gif" ),
pagePanel,"My tooltip text",iLocation );
unde iLocation este index-ul paginii ce se va nltura. Pentru a se terge toate paginile,
trebuie s se in evidena numrului de pagini rmase, altfel Java VM va genera o
excepie.
while( tabbedPanel.getTabCount() > 0 )
tabbedPanel.removeTabAt( 0 );
O a doua metod utilizeaz instana panel-ului care a fost refereniat atunci cnd
pagina a fost adugat.
tabbedPanel.setSelectedComponent( pagePanel );
3. Scrolling panes
n urmtorul exemplu vom crea un scrolling pane, i i vom aduga o instan
JLabel care arat o imagine foarte mare. Pentru c imaginea este prea mare ca s fie
afiat ntreag, barele de navigare scroll bars vor apare automat.
import java.awt.*;
import javax.swing.*;
class TestScroll extends JFrame
{
private JScrollPane scrollPane;
public TestScroll()
{
setTitle( "Tabbed Pane Application" );
setSize( 300, 200 );
setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
Icon image = new ImageIcon( "main.gif" );
JLabel label = new JLabel( image );
// Creeaza un scroll pane
scrollPane = new JScrollPane();
scrollPane.getViewport().add( label );
topPanel.add( scrollPane, BorderLayout.CENTER );
}
public static void main( String args[] )
{
TestScroll mainFrame = new TestScroll();
mainFrame.setVisible( true );
}
}
4. Split panes
Clasa JSplitPane este utilizat pentru a divide dou componente, care prin
intervenia utilizatorului pot fi redimensionate interactiv. Divizarea se poate face n
direcia stnga-dreapta utiliznd setarea JSplitPane.HORIZONTAL_SPLIT, sau n
direcia sus-jos utiliznd JSplitPane.VERTICAL_SPLIT.
JSplitPane va divide numai dou componente. Dac este nevoie de o interfa
mai complex, se poate imbrica o instan JSplitPane ntr-o alt instan JSplitPane.
Astfel, se va putea intermixa i divizarea orizontal cu cea vertical.
Grania de diviziune poate fi ajustat de ctre utilizator cu mouse-ul, dar poate fi
setat i prin apelul metodei setDividerLocation(). Atunci cnd grania de diviziune este
mutat cu mouse-ul de ctre utilizator, se vor utiliza setrile dimensiunilor minime i
maxime ale componentelor, pentru a determina limitele deplasrii graniei. Astfel, dac
dimensiunea minim a dou componente este mai mare dect dimensiunea containerului
split pane, codul JSplitPane nu va permite redimensionarea frame-urilor separate de
grania de diviziune.
Exemplu:
import java.awt.*;
import javax.swing.*;
}
public static void main( String args[] )
{
TestSplit mainFrame = new TestSplit();
mainFrame.pack();
mainFrame.setVisible(true);
}
}
IV. TEM
1. Se vor parcurge toate exemplele prezentate n platforma de laborator testndu-se practic.
2. Scriei o aplicaie Java n care s construii o interfa utilizator folosind noile noiuni
prezentate n lucrarea de laborator.