.


:




:

































 

 

 

 


awt Swing




Java 2 , , (GUI graphics user interface):

- AWT (Abstract Window Toolkit ) JDK java.awt

- JFC (Java Foundation Classes) Swing JDK javax.swing.

(Java 1.0, 1.1) AWT, JFC Java 2. (, , , ..) : , , .

, AWT- Swing- . Java, - . (lightweight).

Java 2 AWT, Sun Swing. Swing J AWT (, Button AWT JButton Swing).

AWT Swing .

 

 

 

AWT Swing

, . , , GUI . , , , , , .. .

Java Component , Component. Component GUI. , .

Component

Component , . , , , ..

, . . (0,0) . , .

 

Ox

 

Oy

 

 

Dimension getSize()

Dimension, width height, .

int getWidth()

int getHeight()

Component. , Dimension width height double, Component int.

setBounds(int x, int y, int width, int height)

isEnabled()

isVisible()

setEnabled(boolean b)

setVisible(boolean b)

Container

, . ,

Component add(Component comp)

. Java Container . Container , Component. Container.

- JFrame

JFrame - , , / , . Frame , .

JFrame:

JFrame()

JFrame(String title)

. , title.

setTitle(String title)

Frame . ,

setVisible(Boolean state)

Component, .

JFrame

(graphics context). , , . Graphics. .. , .

Graphics getGraphics()

, Graphics . Graphics

drawLine(int x1,int y1, int x2, int y2) (x1,y1) (x2,y2)
drawRect(int x, int y, int width, int height) , , (x,y), width height
drawOval(int x, int y, int width, int height) , , ( width = height )
drawArc(int x, int y, int width, int height, int startAngle, int arc) , , . arc startAngle. Ox. , .
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) . , arcWidth arcHeight, .
drawPolygon(int xPoints[], int yPoints[], int nPoints) (xPoints[i], yPoints[i]) nPoints
drawString(String s, int x, int y) s. (x,y).

Color

Java Color. Color :

Color(int r, int g, int b)

, (r), (g) (b) ( RGB). 0 255.

, Color, Color. 13: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow.

Polygon

. :

Polygon()

.

Polygon(int xPoints[], int yPoints[], int nPoints)

(xPoints[i], yPoints[i]). nPoints.

, , , . Polygon .

boolean contains(double x, double y) , (x,y)
boolean contains(double x, double y, double w, double h) , ,
boolean contains(int x, int y) , (x,y)
boolean contains(Point p) , (x,y)
boolean intersects(double x, double y, double w, double h) , ,

. , .

:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.Random;

class Rect

{public static void main(String args[])

{int xPoints[] = new int[4];

int yPoints[] = new int[4];

int x,y;

JFrame f = new JFrame();

f.setBounds(10,10,500,200);

f.setVisible(true);

Graphics g = f.getGraphics();

g.setColor(new Color (0,0,255));

Random r = new Random();

xPoints[0]=20;yPoints[0]=70;

xPoints[1]=280;yPoints[1]=70;

xPoints[2]=280;yPoints[2]=150;

xPoints[3]=20;yPoints[3]=150;

x = r.nextInt (500);

y = r.nextInt (200);

Polygon p = new Polygon(xPoints,yPoints,4);

g.drawPolygon (p);

g.setColor(new Color (255,0,0));

if (p.contains (x,y)==true)

g.drawString (" " + x + " " + y +

" ", 20,50);

else

g.drawString (" " + x + " " + y +

" ", 20,50);

f.addWindowListener (new WindowAdapter()

{public void windowClosing(WindowEvent ev)

{System.exit (0); } }); }}

:

AWT Swing :

, Component. , ( ) ( Container). AWT . add(), Container. :

Component add(Component obj)

obj , . , obj. , .

void remove (Component obj).

removeAll() .

, , setBounds() getBounds(). :

void setBounds(int x, int y, int width, int height)

void setBounds(Rectangle r)

Rectangle getBounds()

. :

Point getLocation()

:

void setLocation(int x, int y)

void setLocation(Point p)

, , , , . , Java, .

Swing , , (content pane). , :

JFrame f = new JFrame("Test");

Container cp = f.getContentPane();

/ , .

Swing ImageIcon, Icon.

ImageIcon:

ImageIcon(String filename)

ImageIcon:

int getIconHeight()

int getIconWidth()

void paintIcon(Component comp, Graphics g, int x, int y)

(label). JLabel, / . , .. .

JLabel:

JLabel (String str)

JLabel (Icon id_icon)

JLabel (String str, Icon id_icon, int align)

, str / id_icon. align. align , JLabel:

JLabel.LEFT -

JLabel.RIGHT -

JLabel.CENTER -

void setText(String str)

,

String getText()

void setAlignment(int align)

int getAlignment()

:

void setIcon(Icon id_icon)

,

Icon getIcon()

JButton. ( ) / . .

JButton:

JButton(String str)

JButton(Icon id_icon)

, :

String getLabel()

void setLabel(String str)

Icon getLabel()

void setIcon(Icon id_icon)

- JCheckBox. :

JCheckBox (String str, Icon id_icon, boolean state)

:

boolean getSelected()

void setSelected(boolean state)

()

Swing JRadioButton. :

JRadioButton (String str, Icon id_icon, boolean state)

, . ButtonGroup. ( ).

:

JFrame f = new JFrame();

Container cp = f.getContentPane();

JRadioButton jrb1 = new JRadioButton("a",false);

JRadioButton jrb2 = new JRadioButton("b",false);

JRadioButton jrb3 = new JRadioButton("c",true);

ButtonGroup bg = new ButtonGroup();

bg.add(jrb1);

bg.add(jrb2);

bg.add(jrb3);

cp.add(jrb1);

cp.add(jrb2);

cp.add(jrb3);

JComboBox. :

JComboBox()

JComboBox(Vector v[])

JComboBox(Object obj[])

:

addItem(Object item)

addItem().

(index) :

insertItemAt(Object anObject, int index)

:

void removeItem(String text)

void removeItemAt(int index)

void RemoveAllItems()

String getSelectedItem()

int getSelectedIndex()

, . 0. .

:

int getItemCount()

:

String getItemAt(int index)

:

void setSelectedIndex (int index)

void setSelectedItem (Object obj)

. , :

void setEditable (boolean state)

JList. .

JList , .

:

JList(Vector v[])

JList(Object obj[])

JList . , , JScrollPane, .

:

JFrame f = new JFrame();

Container cp = f.getContentPane();

f.setBounds(100,100,300,300);

String s[] = {"1","2","3","4","5","6","7","8","9","10"};

JList jl = new JList(s);

jl.setBounds(10,10,30,30);

JScrollPane p = new JScrollPane(jl);

cp.add(p);

f.setVisible(true);

() :

Object getSelectedValue()

Object[] getSelectedValues()

int getSelectedIndex()

int[] getSelectedIndices()

( ) JTextField. , -, Backspace, Delete, .

:

JTextFiled()

JTextFiled(int numChars)

JTextFiled(String str)

JTextFiled(String str, int numChars)

. numChars . str. .

JTextField:

String getText()

, .

void setText(String str)

str.

String getSelectedText()

. getSelectedText() .

void select(int start, int end)

start end.

.

boolean isEditable()

void setEditable(boolean canEdit)

true .

, (, ),

void setEchoChar(char ch)

ch , .

( ) JTextArea. Enter . .

:

JTextArea()

JTextArea(int numLines, int numChars)

JTextArea(String str)

JTextArea(String str, int numLines, int numChars)

numLines , numChars , str .

JTextArea , JTextField. :

void append (String str)

str

void insert (String str, int index)

str , index.

void replaceRange(String str, int start, int end)

start end-1 str.

JTextField JTextArea JScrollPane.

JTable

. Java Swing JTable. JTable , .

. , . , .

, JTable, . . , . Windows Name ( ), Last modified ( ), Type () Size (). :

  String[] columnNames = {"Name", "Last modified", "Type", "Size"};

, . :

  String[][] data = { {"addins", "02.11.2006 19:15", "Folder", ""}, {"AppPatch", "03.10.2006 14:10", "Folder", ""}, {"assembly", "02.11.2006 14:20", "Folder", ""}, {"Boot", "13.10.2007 10:46", "Folder", ""}, {"Branding", "13.10.2007 12:10", "Folder", ""}, {"Cursors", "23.09.2006 16:34", "Folder", ""}, {"Debug", "07.12.2006 17:45", "Folder", ""}, {"Fonts", "03.10.2006 14:08", "Folder", ""}, {"Help", "08.11.2006 18:23", "Folder", ""}, {"explorer.exe", "18.10.2006 14:13", "File", "2,93MB"}, {"helppane.exe", "22.08.2006 11:39", "File", "4,58MB"}, {"twunk.exe", "19.08.2007 10:37", "File", "1,08MB"}, {"nsreg.exe", "07.08.2007 11:14", "File", "2,10MB"}, {"avisp.exe", "17.12.2007 16:58", "File", "12,67MB"}};

JTable .

  JTable table = new JTable(data, columnNames);

JScrollPane, JFrame. .

.

import java.awt.Dimension;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

public class TestFrame extends JFrame {

public static void createGUI() {

JFrame frame = new JFrame("Test frame");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] columnNames = {"Name","Last modified","Type", "Size" };

String[][] data = {

{"addins", "02.11.2006 19:15", "Folder", ""},

{"AppPatch", "03.10.2006 14:10", "Folder", ""},

{"assembly", "02.11.2006 14:20", "Folder", ""},

{"Boot", "13.10.2007 10:46", "Folder", ""},

{"Branding", "13.10.2007 12:10", "Folder", ""},

{"Cursors", "23.09.2006 16:34", "Folder", ""},

{"Debug", "07.12.2006 17:45", "Folder", ""},

{"Fonts", "03.10.2006 14:08", "Folder", ""},

{"Help", "08.11.2006 18:23", "Folder", ""},

{"explorer.exe", "18.10.2006 14:13", "File", "2,93MB"},

{"helppane.exe", "22.08.2006 11:39", "File", "4,58MB"},

{"twunk.exe", "19.08.2007 10:37", "File", "1,08MB"},

{"nsreg.exe", "07.08.2007 11:14", "File", "2,10MB"},

{"avisp.exe", "17.12.2007 16:58", "File", "12,67MB"},

};

JTable table = new JTable(data, columnNames);

JScrollPane scrollPane = new JScrollPane(table);

frame.getContentPane().add(scrollPane);

frame.setPreferredSize(new Dimension(450, 200));

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true); }

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

JFrame.setDefaultLookAndFeelDecorated(true);

createGUI(); } }); }}

Swing

, . , , .

, :

, .

, ( ).

.

 

. , , .

(listener)

Swing .

- , , . , .

, . , , .

, , - ( ) . , . .

, , , . , .

, (.. ), (.. -). .





:


: 2016-11-02; !; : 1771 |


:

:

, .
==> ...

1580 - | 1354 -


© 2015-2024 lektsii.org - -

: 0.184 .