Java Swing — различия между версиями
Материал из SEWiki
Antonk (обсуждение | вклад) |
Antonk (обсуждение | вклад) |
||
| Строка 62: | Строка 62: | ||
public static void main (String [] args) { | public static void main (String [] args) { | ||
JFrame myWindow = new SimpleWindowButton(); | JFrame myWindow = new SimpleWindowButton(); | ||
| + | myWindow.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | == 4. Layouts == | ||
| + | |||
| + | <source lang="java"> | ||
| + | import java.awt.BorderLayout; | ||
| + | import java.awt.FlowLayout; | ||
| + | import java.awt.GridLayout; | ||
| + | |||
| + | import javax.swing.Box; | ||
| + | import javax.swing.JButton; | ||
| + | import javax.swing.JComponent; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JPanel; | ||
| + | |||
| + | class SimpleWindowFlow extends JFrame { | ||
| + | SimpleWindowFlow() { | ||
| + | super("Пробное окно"); | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | setSize(400, 300); | ||
| + | JPanel panel = new JPanel(); | ||
| + | panel.setLayout(new FlowLayout()); | ||
| + | panel.add(new JButton("Кнопка")); | ||
| + | panel.add(new JButton("+")); | ||
| + | panel.add(new JButton("-")); | ||
| + | panel.add(new JButton("Кнопка с длинной надписью")); | ||
| + | setContentPane(panel); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class SimpleWindowBorder extends JFrame { | ||
| + | SimpleWindowBorder() { | ||
| + | super("Пробное окно"); | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | setSize(400, 300); | ||
| + | getContentPane().add(new JButton("NORTH"), BorderLayout.NORTH); | ||
| + | getContentPane().add(new JButton("EAST"), BorderLayout.EAST); | ||
| + | getContentPane().add(new JButton("SOUTH"), BorderLayout.SOUTH); | ||
| + | getContentPane().add(new JButton("WEST"), BorderLayout.WEST); | ||
| + | getContentPane().add(new JButton("В ЦЕНТР!")); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class SimpleWindowGrid extends JFrame { | ||
| + | SimpleWindowGrid() { | ||
| + | super("Пробное окно"); | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | setSize(400, 300); | ||
| + | JPanel panel = new JPanel(); | ||
| + | panel.setLayout(new GridLayout(2, 3, 5, 10)); | ||
| + | panel.add(new JButton("Кнопка")); | ||
| + | panel.add(new JButton("+")); | ||
| + | panel.add(new JButton("-")); | ||
| + | panel.add(new JButton("Кнопка с длинной надписью")); | ||
| + | panel.add(new JButton("еще кнопка")); | ||
| + | setContentPane(panel); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | class SimpleWindowBox extends JFrame { | ||
| + | SimpleWindowBox() { | ||
| + | super("Пробное окно"); | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | // setSize(400, 300); | ||
| + | Box box = Box.createVerticalBox(); | ||
| + | box.add(new JButton("Кнопка")); | ||
| + | box.add(Box.createVerticalStrut(10)); | ||
| + | box.add(new JButton("+")); | ||
| + | box.add(Box.createVerticalGlue()); | ||
| + | |||
| + | JButton rightButton = new JButton("-"); | ||
| + | rightButton.setAlignmentX(JComponent.RIGHT_ALIGNMENT); | ||
| + | box.add(rightButton); | ||
| + | |||
| + | box.add(Box.createVerticalStrut(10)); | ||
| + | box.add(new JButton("Кнопка с длинной надписью")); | ||
| + | setContentPane(box); | ||
| + | pack(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class SimpleWindowManual extends JFrame { | ||
| + | SimpleWindowManual() { | ||
| + | super("Пробное окно"); | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | setSize(400, 300); | ||
| + | JPanel panel = new JPanel(); | ||
| + | panel.setLayout(null); | ||
| + | JButton button = new JButton("Кнопка"); | ||
| + | button.setSize(80, 30); | ||
| + | button.setLocation(20,20); | ||
| + | panel.add(button); | ||
| + | button = new JButton("Кнопка с длинной надписью"); | ||
| + | button.setSize(120, 40); | ||
| + | button.setLocation(70,50); | ||
| + | panel.add(button); | ||
| + | setContentPane(panel); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public class testFramePanel { | ||
| + | public static void main (String [] args) { | ||
| + | JFrame myWindow = new SimpleWindowBorder(); | ||
myWindow.setVisible(true); | myWindow.setVisible(true); | ||
} | } | ||
} | } | ||
</source> | </source> | ||
Версия 10:48, 25 апреля 2012
Что почитать
1. TestFrame
import javax.swing.*;
public class testFrame {
public static void main (String [] args) {
JFrame myWindow = new JFrame("Пробное окно");
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setSize(400, 300);
myWindow.setVisible(true);
}
}
2. TestFrame2
import javax.swing.JFrame;
class SimpleWindow extends JFrame {
SimpleWindow() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
}
}
public class testFrame2 {
public static void main (String [] args) {
JFrame myWindow = new SimpleWindow();
myWindow.setVisible(true);
}
}
3. Button
import javax.swing.JButton;
import javax.swing.JFrame;
class SimpleWindowButton extends JFrame {
SimpleWindowButton() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
JButton newButton = new JButton("Жмакни меня");
getContentPane().add(newButton);
}
}
public class testFrameButton {
public static void main (String [] args) {
JFrame myWindow = new SimpleWindowButton();
myWindow.setVisible(true);
}
}
4. Layouts
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
class SimpleWindowFlow extends JFrame {
SimpleWindowFlow() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JButton("Кнопка"));
panel.add(new JButton("+"));
panel.add(new JButton("-"));
panel.add(new JButton("Кнопка с длинной надписью"));
setContentPane(panel);
}
}
class SimpleWindowBorder extends JFrame {
SimpleWindowBorder() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
getContentPane().add(new JButton("NORTH"), BorderLayout.NORTH);
getContentPane().add(new JButton("EAST"), BorderLayout.EAST);
getContentPane().add(new JButton("SOUTH"), BorderLayout.SOUTH);
getContentPane().add(new JButton("WEST"), BorderLayout.WEST);
getContentPane().add(new JButton("В ЦЕНТР!"));
}
}
class SimpleWindowGrid extends JFrame {
SimpleWindowGrid() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 3, 5, 10));
panel.add(new JButton("Кнопка"));
panel.add(new JButton("+"));
panel.add(new JButton("-"));
panel.add(new JButton("Кнопка с длинной надписью"));
panel.add(new JButton("еще кнопка"));
setContentPane(panel);
}
}
class SimpleWindowBox extends JFrame {
SimpleWindowBox() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setSize(400, 300);
Box box = Box.createVerticalBox();
box.add(new JButton("Кнопка"));
box.add(Box.createVerticalStrut(10));
box.add(new JButton("+"));
box.add(Box.createVerticalGlue());
JButton rightButton = new JButton("-");
rightButton.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
box.add(rightButton);
box.add(Box.createVerticalStrut(10));
box.add(new JButton("Кнопка с длинной надписью"));
setContentPane(box);
pack();
}
}
class SimpleWindowManual extends JFrame {
SimpleWindowManual() {
super("Пробное окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
JPanel panel = new JPanel();
panel.setLayout(null);
JButton button = new JButton("Кнопка");
button.setSize(80, 30);
button.setLocation(20,20);
panel.add(button);
button = new JButton("Кнопка с длинной надписью");
button.setSize(120, 40);
button.setLocation(70,50);
panel.add(button);
setContentPane(panel);
}
}
public class testFramePanel {
public static void main (String [] args) {
JFrame myWindow = new SimpleWindowBorder();
myWindow.setVisible(true);
}
}