Java Swing Первые программы
Материал из Wiki.crossplatform.ru
ViGOur (Обсуждение | вклад) (Новая: In this chapter, we will program our first programs in Swing toolkit. The examples are going to be very simple. We will cover some basic functionality. == Our first example == In our f...) |
Evilone (Обсуждение | вклад) |
||
Строка 1: | Строка 1: | ||
- | + | В этой главе мы напишем нашу первую программу исползуя Swing. Пример будет весьма простеньким. Будет охвачена лишь некоторая часть основных функциональных возможностей. | |
Версия 11:04, 24 февраля 2009
В этой главе мы напишем нашу первую программу исползуя Swing. Пример будет весьма простеньким. Будет охвачена лишь некоторая часть основных функциональных возможностей.
Содержание |
Our first example
In our first example, we will show a basic window.
import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } }
While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer.
import javax.swing.JFrame;
Here we import the JFrame widget. It is a toplevel container, which is used for placing other widgets.
setSize(300, 200); setTitle("Simple");
This code will resize the window to be 300px wide and 200px tall. It will set the title of the window to Simple.
setDefaultCloseOperation(EXIT_ON_CLOSE);
This method will close the window, if we click on the close button. By default nothing happens.
Centering window on the screen
By default, when the window is shown on the screen, it is not centered. It will pop up most likely in the upper left corner. The following code example will show the window centered on the screen.
import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class CenterOnScreen extends JFrame { public CenterOnScreen() { setSize(300, 200); setTitle("CenterOnScreen"); setDefaultCloseOperation(EXIT_ON_CLOSE); Toolkit toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2); } public static void main(String[] args) { CenterOnScreen cos = new CenterOnScreen(); cos.setVisible(true); } }
To center the window on the screen, we must know the resolution of the monitor. For this, we use the Toolkit class.
Toolkit toolkit = getToolkit(); Dimension size = toolkit.getScreenSize();
We get the toolkit and figure out the screen size.
setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
To place the window on the screen, we call the setLocation() method.
Buttons
In our next example, we will show two buttons. The first button will beep a sound and the second button will close the window.
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Buttons extends JFrame { private Toolkit toolkit; public Buttons() { setTitle("Buttons"); setSize(300, 200); toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation((size.width - getWidth())/2, (size.height - getHeight())/2); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); JButton beep = new JButton("Beep"); beep.setBounds(150, 60, 80, 30); beep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); } }); JButton close = new JButton("Close"); close.setBounds(50, 60, 80, 30); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); panel.add(beep); panel.add(close); } public static void main(String[] args) { Buttons buttons = new Buttons(); buttons.setVisible(true); } }
In this example, we will see two new topics. Layout management and event handling. They will be touched only briefly. Both of the topics will have their own chapter.
JPanel panel = new JPanel(); getContentPane().add(panel);
We create a JPanel component. It is a generic lightweight container. We add the JPanel to the JFrame.
panel.setLayout(null);
By default, the JPanel has a FlowLayout manager. The layout manager is used to place widgets onto the containers. If we call setLayout(null) we can position our components absolutely. For this, we use the setBounds() method.
JButton beep = new JButton("Beep"); beep.setBounds(150, 60, 80, 30); beep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); } });
Here we create a button. We position it by calling the setBounds() method. Then we add an action listener. The action listener will be called, when we perform an action on the button. In our case, if we click on the button. The beep button will play a simple beep sound.
System.exit(0);
The close button will exit the application. For this, we call the System.exit() method.
panel.add(beep); panel.add(close);
In order to show the buttons, we must add them to the panel.
A tooltip
Tooltips are part of the internal application's help system. The Swing shows a small rectangular window, if we hover a mouse pointer over an object.
import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Tooltip extends JFrame { private Toolkit toolkit; public Tooltip() { setTitle("Tooltip"); setSize(300, 200); toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation((size.width - getWidth())/2, (size.height - getHeight())/2); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); panel.setToolTipText("A Panel container"); JButton button = new JButton("Button"); button.setBounds(100, 60, 80, 30); button.setToolTipText("A button component"); panel.add(button); } public static void main(String[] args) { Tooltip tooltip = new Tooltip(); tooltip.setVisible(true); } }
In the example, we set the tooltip for the frame and the button.
panel.setToolTipText("A Panel container");
To enable a tooltip, we call the setTooltipText() method.