Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/artifacts/SwingApp_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: lesson8.examples.Example2

3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: lesson8.examples.Example2

42 changes: 42 additions & 0 deletions src/lesson8/borderlayout/BorderLayout1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lesson8.borderlayout;

import javax.swing.*;
import java.awt.*;

public class BorderLayout1 {

static class MyWindow extends JFrame {
public MyWindow() {
setTitle("Test Window");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800, 800);
setLocationRelativeTo(null);

JButton button = new JButton("Button 1 (PAGE_START)");
add(button, BorderLayout.NORTH);

button = new JButton("Button 2 (CENTER)");
add(button, BorderLayout.CENTER);

button = new JButton("Button 3 (LINE_START)");
// button.setSize(new Dimension(200, 200));
button.setSize(200, 200);
// add(button, BorderLayout.LINE_START);
add(button, BorderLayout.WEST);

button = new JButton("Long-Named Button 4 (PAGE_END)");
add(button, BorderLayout.EAST);

button = new JButton("5 (LINE_END)");
// add(button, BorderLayout.LINE_END);
add(button, BorderLayout.SOUTH);

setVisible(true);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
29 changes: 29 additions & 0 deletions src/lesson8/borderlayout/BorderLayout2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lesson8.borderlayout;

import javax.swing.*;
import java.awt.*;

public class BorderLayout2 {

static public class MyWindow extends JFrame {
public MyWindow() {
setBounds(500,500,500,300);
setTitle("BoxLayoutDemo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton[] jbs = new JButton[10];
// setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // одну из строк надо закомментировать
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); // одну из строк надо закомментировать
for (int i = 0; i < jbs.length; i++) {
jbs[i] = new JButton("#" + i);
jbs[i].setAlignmentY(Component.TOP_ALIGNMENT);
add(jbs[i]);
}
setVisible(true);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
27 changes: 27 additions & 0 deletions src/lesson8/borderlayout/BorderLayout3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lesson8.borderlayout;

import javax.swing.*;
import java.awt.*;

public class BorderLayout3 {

static public class MyWindow extends JFrame {
public MyWindow() {
setBounds(500, 500, 400, 300);
setTitle("FlowLayoutDemo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton[] jbs = new JButton[10];
setLayout(new FlowLayout());
for (int i = 0; i < jbs.length; i++) {
jbs[i] = new JButton("#" + i);
add(jbs[i]);
}
setVisible(true);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
27 changes: 27 additions & 0 deletions src/lesson8/borderlayout/BorderLayout4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lesson8.borderlayout;

import javax.swing.*;
import java.awt.*;

public class BorderLayout4 {

static public class MyWindow extends JFrame {
public MyWindow() {
setBounds(500,500,400,300);
setTitle("GridLayoutDemo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton[] jbs = new JButton[10];
setLayout(new GridLayout(4, 3));
for (int i = 0; i < jbs.length; i++) {
jbs[i] = new JButton("#" + i);
add(jbs[i]);
}
setVisible(true);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
57 changes: 57 additions & 0 deletions src/lesson8/events/KeyboardExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lesson8.events;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class KeyboardExample {

static public class MyWindow extends JFrame {
public MyWindow() {
setBounds(500, 500, 400, 300);
setTitle("Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JLabel text = new JLabel("<blank>");
add(text, BorderLayout.NORTH);

JTextField field = new JTextField();
add(field, BorderLayout.CENTER);
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Your message: " + field.getText());
text.setText(((JTextField)e.getSource()).getText());
field.setText(null);
}
});

field.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (e.isShiftDown()) {
System.out.println("Shift down");
}
System.out.println(e.getKeyChar() + " down");

}
});

JButton button = new JButton("Execute");
add(button, BorderLayout.SOUTH);
button.addActionListener(e -> {
text.setText(field.getText());
field.setText(null);
});

setVisible(true);
}

}


public static void main(String[] args) {
new MyWindow();
}

}
39 changes: 39 additions & 0 deletions src/lesson8/events/MouseEventExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lesson8.events;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseEventExample {

static public class MyWindow extends JFrame {
public MyWindow() {
setBounds(500, 500, 400, 300);
setTitle("Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel pan = new JPanel();
add(pan);
pan.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("MousePos: " + e.getX() + " " + e.getY());
}

@Override
public void mouseExited(MouseEvent e) {
System.out.println("Exited!");
}
});



setVisible(true);
}
}


public static void main(String[] args) {
new MyWindow();
}

}
36 changes: 36 additions & 0 deletions src/lesson8/examples/Example1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lesson8.examples;

import javax.swing.*;
import java.awt.*;

public class Example1 {

static class MyWindow extends JFrame {
public MyWindow() {
setTitle("Test Window");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// setLocationRelativeTo(null);
setSize(400, 400);
setLocation(300, 300);
// setBounds(2400, 300, 400, 400);
setVisible(true);
}
}

private static class WindowRunnable implements /* extends */ Runnable {

@Override
public void run() {
new MyWindow();
}
}

public static void main(String[] args) {
new MyWindow();
// EventQueue.invokeLater(new WindowRunnable());
// EventQueue.invokeLater(MyWindow::new);

}


}
40 changes: 40 additions & 0 deletions src/lesson8/examples/Example2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lesson8.examples;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Example2 {

static class MyWindow extends JFrame {
public MyWindow() {
setTitle("Test Window");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(300, 300, 400, 400);

JButton[] jbs = new JButton[5];
for (int i = 0; i < 5; i++) {
jbs[i] = new JButton("#" + i);
jbs[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton)e.getSource()).getText() + " was pressed");
}
});
}
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);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
69 changes: 69 additions & 0 deletions src/lesson8/graphics/ButtonDrawExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package lesson8.graphics;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonDrawExample {
static class MyWindow extends JFrame {

private static final String DRAW_X = "DRAW_X";
private static final String DRAW_O = "DRAW_O";

public MyWindow() {
setSize(800,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

setLayout(new GridLayout(1, 3));
add(createButton());
add(createButton());
add(createButton());

setVisible(true);
}

private JButton createButton() {
return new JButton() {

{
setActionCommand(DRAW_O);
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String action = getActionCommand().equals(DRAW_O) ? DRAW_X : DRAW_O;
setActionCommand(action);
}
});
}

@Override
public void paint(Graphics graphics) {
super.paint(graphics);

if (getActionCommand().equals(DRAW_O)) {
graphics.drawOval(0, this.getHeight() / 4, getWidth(), getWidth());
graphics.setColor(Color.RED);
graphics.fillOval(0, this.getHeight() / 4, getWidth(), getWidth());
}
else {
Graphics2D g2d = (Graphics2D) graphics;
g2d.setStroke(new BasicStroke(10));
g2d.setColor(Color.BLUE);
g2d.drawLine(0, 0, this.getWidth(), this.getHeight());
g2d.drawLine(this.getWidth(), 0, 0, this.getHeight());
}
}
};

}

}


public static void main(String[] args){
new MyWindow();
}

}
Loading