Perdorimi i JPanel
Klasat BorderLayout, FlowLayout, GridLayout dhe CardLayout ofrojne nje numer te limituar te kombinimeve te vendosjes se komponenteve ne dritare.
Qellimi kryesor i JPanel eshte te organizoje komponentet, Ne nje JPanel mund te vendosen nje variacion layout-esh duke siguruar organizim me te mire te komponenteve.
• JPanel – Nje siperfaqe plani e pakufizuar.
– Mund te mbaje komponente me peshe te lehte (lightweight UI – komponentet swing jane me peshe te lehte sepse ato implementohen plotesisht ne Java, pa thirrur sistemin e operimit per vizatimin e komponenteve)
Komponentet e krijuar duke perdorur AWT (Abstract Window Toolkit) quhen me peshe te rende (heavyweight) sepse awt thjesht therret sistemin e operimit per te prodhuar komponentet
• Double buffering – Memorje shtese do te kerkohet per te vizatuar JPanel offscreen kur update-ohet
• Konstruktoret:
– JPanel()
– JPanel(LayoutManager layout)
– JPanel(boolean isDoubleBuffered)
– JPanel(LayoutManager layout, boolean isDoubleBuffered)
• Per te shtuar nje komponent ne JPanel: – Thirret metoda add() e konteinerit duke perdorur komponentin si argument
Shembull 1:
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
public class JFrameWithPanels extends JFrame
{
private final int WIDTH = 250;
private final int HEIGHT = 120;
private JButton button1 = new JButton("One");
private JButton button2 = new JButton("Two");
private JButton button3 = new JButton("Three");
public JFrameWithPanels()
{
super("JFrame with Panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(panel1);
con.add(panel2);
panel1.add(button1);
panel1.setBackground(Color.BLUE);
panel2.add(button2);
panel2.add(button3);
panel2.setBackground(Color.BLUE);
setSize(WIDTH, HEIGHT);
}
public static void main(String[] args)
{
JFrameWithPanels frame = new JFrameWithPanels();
frame.setVisible(true);
}
}
// Shembull 2
import javax.swing.*;
import java.awt.*;
public class JDemoManyPanels extends JFrame
{
// Twelve buttons
private JButton button01 = new JButton("One");
private JButton button02 = new JButton("Two");
private JButton button03 = new JButton("Three");
private JButton button04 = new JButton("Four");
private JButton button05 = new JButton("Five");
private JButton button06 = new JButton("Six");
private JButton button07 = new JButton("Seven");
private JButton button08 = new JButton("Eight");
private JButton button09 = new JButton("Nine");
private JButton button10 = new JButton("Ten");
private JButton button11 = new JButton("Eleven");
private JButton button12 = new JButton("Twelve");
// Four panels
private JPanel panel01 = new JPanel(new GridLayout(2, 0));
private JPanel panel02 = new JPanel(new FlowLayout());
private JPanel panel03 = new JPanel(new FlowLayout());
private JPanel panel04 = new JPanel(new GridLayout(2, 0));
public JDemoManyPanels()
{
setLayout(new BorderLayout());
add(panel01, BorderLayout.WEST);
add(panel02, BorderLayout.CENTER);
add(panel03, BorderLayout.SOUTH);
add(panel04, BorderLayout.EAST);
panel01.add(button01);
panel01.add(button02);
panel01.add(button03);
panel02.add(button04);
panel02.add(button05);
panel02.add(button06);
panel03.add(button07);
panel04.add(button08);
panel04.add(button09);
panel04.add(button10);
panel04.add(button11);
panel04.add(button12);
setSize(400, 250);
}
public static void main(String[] args)
{
JDemoManyPanels frame = new JDemoManyPanels();
frame.setVisible(true);
}
}
// Shembull 3
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class Checkerboard extends JFrame
{
private final int ROWS = 8;
private final int COLS = 8;
private final int GAP = 2;
private final int NUM = ROWS * COLS;
private int x;
private JPanel pane = new JPanel
(new GridLayout(ROWS, COLS, GAP, GAP));
private JPanel[] panel = new JPanel[NUM];
private Color color1 = Color.WHITE;
private Color color2 = Color.BLUE;
private Color tempColor;
public Checkerboard()
{
super("Checkerboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(pane);
for(x = 0; x < NUM; ++x)
{
panel[x] = new JPanel();
pane.add(panel[x]);
if(x % COLS == 0)
{
tempColor = color1;
color1 = color2;
color2 = tempColor;
}
if(x % 2 == 0)
panel[x].setBackground(color1);
else
panel[x].setBackground(color2);
}
}
public static void main(String[] args)
{
Checkerboard frame = new Checkerboard();
final int SIZE = 300;
frame.setSize(SIZE, SIZE);
frame.setVisible(true);
}
}