Вы находитесь на странице: 1из 10

Swing.

, JFrame, : ,
, , , . .





Enter







geekbrains.ru


JFrame. :

public class Form1 extends JFrame {


public Form1() {
setTitle("Test Window");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400, 400);
setLocation(300, 300);
setVisible(true);
}
}

:
setTitle() ;
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
,
;
setSize(400, 400) ;
setLocation(300, 300) ;
setVisible(true) .
.

1 Swing
, .
, 5 (JButton. , Swing ,
, J).
(
BorderLayout). / ,
geekbrains.ru

add(_, ). BorderLayout
, , , , , ,
. , CENTER, , ,
, ,
, .
.

public class Form2 extends JFrame {


public Form2() {
setTitle("Test Window");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400, 400);
setLocation(300, 300);
JButton[] jbs = new JButton[5];
for (int i = 0; i < 5; i++) {
jbs[i] = new JButton("#" + i);
}
setLayout(new BorderLayout());
add(jbs[0], BorderLayout.EAST);
add(jbs[1], BorderLayout.WEST);
add(jbs[2], BorderLayout.SOUTH);
add(jbs[3], BorderLayout.NORTH);
add(jbs[4], BorderLayout.CENTER);
setVisible(true);
}
}

2 5
.
BorderLayout .

geekbrains.ru

3 BorderLayout

JButton button = new JButton("Button 1 (PAGE_START)");


add(button, BorderLayout.PAGE_START);
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
add(button, BorderLayout.LINE_END);

BoxLayout .

FlowLayout , ,
.

GridBagLayout ,
, .
, .

geekbrains.ru

GridLayout .

:
JFrame
JButton
JLabel
JTextField
JTextArea
JScrollPane
JMenuBar
JTabel
JRadioButton RadioButton
JCheckBox CheckBox



().
,
. , , ,
.
,
. :
.
, .
, .
, .
: ,
.
, .
.
, : , ,
.
, .

geekbrains.ru


. , ,
,
.


JButton button = new JButton("Btn");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button pressed...");
}
});

ActionListener, .
, actionPerformed.

Enter
JTextField field = new JTextField();
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Your message: " + field.getText());
}
});

geekbrains.ru


JPanel pan = new JPanel();
pan.addMouseListener(new MouseAdapter() {
@Override
public void MousePressed(MouseEvent e) {
System.out.println("Mouse Position: " + e.getX() + " " + e.getY());
}
});


.
, Graphics.
:

, , paint() update();
getGraphics() Cornponent .
, Graphics
, , .
, .
, (
setColor()). ,
.
0 , 0 , .


drawLine(), .

void drawLine ( int , int Y, int , int Y);

drawLine() X, Y
, Y.


drawRect() fillRect() .
.
.
drawRoundRect() fillRoundRect().

geekbrains.ru


drawOval(), fillOvl().
,
( ). , , ..
.


Color. Color (
Color.blk) .
, .
Color.

Color (int R, int G, int B);


Color (int RGB);
Color (float R, float G, float B);

,
, .
0 255, .

new Color(255, 100, 100); // -

Color
, (RGB).
, 16- 23- ,
- 8- 15- , - 0- 7- .
Color
0.0f 1.0f, ,
.


1.
2.
3.
4.

, ( );
, ( 3 );
;
( repaint());.


1. . , Java. . 1. // . . - .:
, 2014. - 864 .
geekbrains.ru

2.
3.
4.
5.
6.

// . . .: , 2007. 896 .
Java // 4- .: . . .: , 2016. 1168 .
. Java 8. // 9- .: . . - .: , 2015. - 1376 .
. Java 8: . // 6- .: . . - .: , 2015. - 720 .
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

geekbrains.ru