Commit 4461a62c by Patryk Czarnik

Swing podstawy

parent 06b08a75
package swing.layouty;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Layout_Border {
private final static Font FONT = new Font("Arial", Font.BOLD, 30);
public static void main(String[] args) {
final JFrame okno = new JFrame();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
okno.setLayout(layout);
final JButton button1 = new JButton("A");
button1.setFont(FONT);
okno.add(button1, BorderLayout.CENTER);
final JButton button2 = new JButton("B");
button2.setFont(FONT);
okno.add(button2, BorderLayout.NORTH);
final JButton button3 = new JButton("C");
button3.setFont(FONT);
okno.add(button3, BorderLayout.WEST);
final JButton button4 = new JButton("D");
button4.setFont(FONT);
okno.add(button4, BorderLayout.EAST);
okno.pack();
okno.setVisible(true);
}
}
package swing.layouty;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Layout_Box {
private final static Font FONT = new Font("Arial", Font.BOLD, 30);
public static void main(String[] args) {
final JFrame okno = new JFrame();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout layout = new BoxLayout(okno.getContentPane(), BoxLayout.X_AXIS);
okno.setLayout(layout);
final JButton button1 = new JButton("A");
button1.setFont(FONT);
okno.add(button1);
final JButton button2 = new JButton("B");
button2.setFont(FONT);
okno.add(button2);
final JButton button3 = new JButton("C");
button3.setFont(FONT);
okno.add(button3);
final JButton button4 = new JButton("D");
button4.setFont(FONT);
okno.add(button4);
okno.pack();
okno.setVisible(true);
}
}
package swing.layouty;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Layout_BoxY {
private final static Font FONT = new Font("Arial", Font.BOLD, 30);
public static void main(String[] args) {
final JFrame okno = new JFrame();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout layout = new BoxLayout(okno.getContentPane(), BoxLayout.Y_AXIS);
okno.setLayout(layout);
final JButton button1 = new JButton("A");
button1.setFont(FONT);
okno.add(button1);
final JButton button2 = new JButton("B");
button2.setFont(FONT);
okno.add(button2);
final JButton button3 = new JButton("C");
button3.setFont(FONT);
okno.add(button3);
final JButton button4 = new JButton("D");
button4.setFont(FONT);
okno.add(button4);
okno.pack();
okno.setVisible(true);
}
}
package swing.layouty;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Layout_Flow {
private final static Font FONT = new Font("Arial", Font.BOLD, 30);
public static void main(String[] args) {
final JFrame okno = new JFrame();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout = new FlowLayout();
okno.setLayout(layout);
final JButton button1 = new JButton("A");
button1.setFont(FONT);
okno.add(button1);
final JButton button2 = new JButton("B");
button2.setFont(FONT);
okno.add(button2);
final JButton button3 = new JButton("C");
button3.setFont(FONT);
okno.add(button3);
final JButton button4 = new JButton("D");
button4.setFont(FONT);
okno.add(button4);
final JButton button5 = new JButton("E");
button5.setFont(FONT);
okno.add(button5);
final JButton button6 = new JButton("F");
button6.setFont(FONT);
okno.add(button6);
final JButton button7 = new JButton("G");
button7.setFont(FONT);
okno.add(button7);
final JButton button8 = new JButton("H");
button8.setFont(FONT);
okno.add(button8);
okno.pack();
okno.setVisible(true);
}
}
package swing.layouty;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Layout_Flow2 {
private final static Font FONT = new Font("Arial", Font.BOLD, 30);
public static void main(String[] args) {
final JFrame okno = new JFrame();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout = new FlowLayout(FlowLayout.RIGHT, 10, 10);
okno.setLayout(layout);
final JButton button1 = new JButton("A");
button1.setFont(FONT);
okno.add(button1);
final JButton button2 = new JButton("B");
button2.setFont(FONT);
okno.add(button2);
final JButton button3 = new JButton("C");
button3.setFont(FONT);
okno.add(button3);
final JButton button4 = new JButton("D");
button4.setFont(FONT);
okno.add(button4);
final JButton button5 = new JButton("E");
button5.setFont(FONT);
okno.add(button5);
final JButton button6 = new JButton("F");
button6.setFont(FONT);
okno.add(button6);
final JButton button7 = new JButton("G");
button7.setFont(FONT);
okno.add(button7);
final JButton button8 = new JButton("H");
button8.setFont(FONT);
okno.add(button8);
okno.pack();
okno.setVisible(true);
}
}
package swing.layouty;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Layout_Grid {
private final static Font FONT = new Font("Arial", Font.BOLD, 30);
public static void main(String[] args) {
final JFrame okno = new JFrame();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(2, 4, 10, 20);
okno.setLayout(layout);
final JButton button1 = new JButton("A");
button1.setFont(FONT);
okno.add(button1);
final JButton button2 = new JButton("B");
button2.setFont(FONT);
okno.add(button2);
final JButton button3 = new JButton("C");
button3.setFont(FONT);
okno.add(button3);
final JButton button4 = new JButton("D");
button4.setFont(FONT);
okno.add(button4);
final JButton button5 = new JButton("E");
button5.setFont(FONT);
okno.add(button5);
final JButton button6 = new JButton("F");
button6.setFont(FONT);
okno.add(button6);
final JButton button7 = new JButton("G");
button7.setFont(FONT);
okno.add(button7);
final JButton button8 = new JButton("H");
button8.setFont(FONT);
okno.add(button8);
final JButton button9 = new JButton("J");
button9.setFont(FONT);
okno.add(button9);
okno.pack();
okno.setVisible(true);
}
}
package swing.layouty;
public class WypiszDostepneStyle {
public static void main(String[] args) {
System.out.println("Oto dostępne style dla Swinga:");
for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
System.out.println(" * " + info.getName());
}
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Kolory {
// Za pomocą metod setXXX można zmieniać własności komponentów graficznych, np.
// kolor, rozmiar, itd....
public static void main(String[] args) {
JFrame frame = new JFrame("Nasza apka");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
// Gdybyśmy chcieli zmienić kolor tła całego okna, to w taki sposób:
// frame.getContentPane().setBackground(Color.PINK);
JLabel label1 = new JLabel("Ala ma kota.");
label1.setHorizontalAlignment(SwingConstants.CENTER); // domyślnie jest LEADING
label1.setVerticalAlignment(SwingConstants.CENTER); // domyślnie jest CENTER
label1.setForeground(Color.BLUE);
// label1.setForeground(new Color(0, 0, 255, 128));
label1.setFont(new Font("Arial", Font.BOLD, 40));
// Aby zmienić kolor tła nie wystarczy setBackground
// W przypadku Label itp. komponentów trzeba jeszcze wyłączyć przezroczystość.
label1.setOpaque(true);
// Jeśli chcemy zdefiniować mniej standardowy kolor, to istnieje kilka sposobów:
Color kolorTla;
// gotowa stała
// kolorTla = Color.YELLOW;
// podając wartości trzech "kanałów", czyli trzech barw podstawowych: Red Green Blue
// trzy liczby typu float z zakresu od 0.0 do 1.0
// kolorTla = new Color(0.8f, 1.0f, 0.5f);
// trzy liczby całkowite z zakresu od 0 do 255
kolorTla = new Color(255, 240, 128);
// Czwarty parametr oznacza kanał alfa - stopień przezroczystości (0-przezroczyste 255-nieprzezroczyste) - to ma sens gdy nakładamy kilka obrazów (warstw) na siebie
//kolorTla = new Color(255, 240, 128, 128);
// jedna liczba całkowita, która zapis szesnastkowy odpowiada takiemu kodowaniu:
// 0xRRGGBB gdzie RR GG BB to są wartości poszczególnych barw, każda w zakresie
// od 00 do FF
// kolorTla = new Color(0xFFF080);
// można też odkodować zapis CSS-owy
//kolorTla = Color.decode("#FFF080");
label1.setBackground(kolorTla);
frame.add(label1);
frame.setVisible(true);
}
}
package swing.podstawy;
import javax.swing.*;
public class Okno0 {
public static void main(String[] args) {
new JFrame().setVisible(true);
}
}
package swing.podstawy;
import javax.swing.*;
public class Okno1 {
public static void main(String[] args) {
JFrame jFrame = new JFrame("Nasza apka");
jFrame.setSize(600, 400);
// jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno2 {
public static void main(String[] args) {
JFrame frame = new JFrame("Nasza apka");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Układ okna i wygląd komponentów w 100% opisuje się w języku Java.
// Treść oraz ustawienia wyglądu ustawia się za pomocą setterów.
JLabel label1 = new JLabel("Hello world");
label1.setHorizontalAlignment(JLabel.CENTER);
// label1.setVerticalAlignment(JLabel.TOP);
label1.setFont(new Font("Arial", Font.BOLD, 40));
label1.setForeground(Color.BLUE);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
// przy okazji widzimy, że label zajął całe wnętrze okna
frame.add(label1);
frame.setVisible(true);
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno3a {
public static void main(String[] args) {
JFrame frame = new JFrame("Nasza apka");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("Hello world");
label1.setHorizontalAlignment(JLabel.CENTER);
// label1.setVerticalAlignment(JLabel.TOP);
label1.setFont(new Font("Arial", Font.BOLD, 40));
label1.setForeground(Color.BLUE);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
frame.add(label1);
// Guzik zajął całe okno i nie widać w ogóle labela
// Rozmieszczaniem elementów w oknie (ogólnie: komponentów w kontenerach)
// zajmują się w Swingu obiekty typu LayputManager
JButton button1 = new JButton("Guzik 1");
button1.setFont(new Font("Arial", Font.BOLD, 40));
frame.add(button1);
frame.setVisible(true);
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno3b {
public static void main(String[] args) {
JFrame frame = new JFrame("Nasza apka");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("Hello world");
label1.setHorizontalAlignment(JLabel.CENTER);
// label1.setVerticalAlignment(JLabel.TOP);
label1.setFont(new Font("Arial", Font.BOLD, 40));
label1.setForeground(Color.BLUE);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
frame.add(label1);
// Domyślnym LayoutManagerem jest BorderLayout.
// On potrafi umieścić komponent w środku lub przy jednej z krawędzi określonej słowami "North", "South"...
JButton button1 = new JButton("Guzik 1");
button1.setFont(new Font("Arial", Font.BOLD, 32));
frame.add(button1, "North");
JButton button2 = new JButton("Guzik 2");
button2.setFont(new Font("Arial", Font.BOLD, 32));
frame.add(button2, "South");
frame.setVisible(true);
}
}
package swing.podstawy;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno3c_Border {
public static void main(String[] args) {
JFrame frame = new JFrame("Nasza apka");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Tutaj BorderLayout wskazujemy jawnie. W parametrach można podać odstęp
// LayoutManager layout = new BorderLayout();
LayoutManager layout = new BorderLayout(10, 25);
frame.setLayout(layout);
JLabel label1 = new JLabel("Hello world");
label1.setHorizontalAlignment(JLabel.CENTER);
// label1.setVerticalAlignment(JLabel.TOP);
label1.setFont(new Font("Arial", Font.BOLD, 40));
label1.setForeground(Color.BLUE);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
frame.add(label1);
JButton button1 = new JButton("Guzik 1");
button1.setFont(new Font("Arial", Font.BOLD, 32));
// frame.add(button1, "North");
frame.add(button1, BorderLayout.NORTH);
JButton button2 = new JButton("Guzik 2");
button2.setFont(new Font("Arial", Font.BOLD, 32));
frame.add(button2, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno3d_Flow {
public static void main(String[] args) {
JFrame frame = new JFrame("Nasza apka");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// LayoutManager layout = new FlowLayout();
LayoutManager layout = new FlowLayout(FlowLayout.CENTER, 20, 10);
frame.setLayout(layout);
JLabel label1 = new JLabel("Hello world");
label1.setHorizontalAlignment(JLabel.CENTER);
// label1.setVerticalAlignment(JLabel.TOP);
label1.setFont(new Font("Arial", Font.BOLD, 40));
label1.setForeground(Color.BLUE);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
frame.add(label1);
JLabel label2 = new JLabel("Ala ma kota");
label2.setFont(new Font("Arial", Font.BOLD, 40));
frame.add(label2);
JButton button1 = new JButton("Guzik 1");
button1.setFont(new Font("Arial", Font.BOLD, 32));
frame.add(button1);
JButton button2 = new JButton("Guzik 2");
button2.setFont(new Font("Arial", Font.BOLD, 32));
frame.add(button2);
frame.setVisible(true);
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno3e_Box {
// Tu używam już innego layoutu - BoxLayout
public static void main(String[] args) {
System.out.println("Początek main");
JFrame okno = new JFrame("Nasza apka");
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setSize(600, 400);
LayoutManager layout = new BoxLayout(okno.getContentPane(), BoxLayout.Y_AXIS);
okno.setLayout(layout);
JLabel label1 = new JLabel("Ala ma kota");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setFont(new Font("Arial", Font.BOLD, 30));
label1.setForeground(Color.RED);
okno.add(label1);
JLabel label2 = new JLabel("Ola ma psa");
label2.setHorizontalAlignment(JLabel.RIGHT);
label2.setFont(new Font("Arial", Font.ITALIC, 30));
label2.setForeground(Color.BLUE);
okno.add(label2);
JButton button1 = new JButton("Kliknij mnie");
button1.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
button1.setForeground(Color.GREEN);
okno.add(button1);
okno.setVisible(true);
System.out.println("Koniec main");
}
}
package swing.podstawy;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Okno3f_Grid {
public static void main(String[] args) {
System.out.println("Początek main");
JFrame okno = new JFrame("Nasza apka");
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setSize(600, 400);
LayoutManager layout = new GridLayout(2, 2);
okno.setLayout(layout);
JLabel label1 = new JLabel("Ala ma kota");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setFont(new Font("Arial", Font.BOLD, 30));
label1.setForeground(Color.RED);
okno.add(label1);
JLabel label2 = new JLabel("Ola ma psa");
label2.setHorizontalAlignment(JLabel.RIGHT);
label2.setFont(new Font("Arial", Font.ITALIC, 30));
label2.setForeground(Color.BLUE);
okno.add(label2);
JButton button1 = new JButton("Kliknij mnie");
button1.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
button1.setForeground(Color.GREEN);
okno.add(button1);
JButton button2 = new JButton("Guzik 2");
button2.setFont(new Font("Arial", Font.BOLD, 30));
button2.setForeground(Color.MAGENTA);
okno.add(button2);
okno.setVisible(true);
System.out.println("Koniec main");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment