Java GUI-AWT
2015年12月29日
但是对于平台特有的组件无法实现Java的跨平台目标,并且Java使用JNI调用底层GUI的方式也给跨平台带来不便。于是Java在后续的GUI中使用AWT的扩展版本Swing代替AWT。
参考:《疯狂java讲义(第2版)》
注意:由于AWT已经被Swing替代,仅学习其基本功能,作为Swing的基础。
//Main.java
package lee;
import java.awt.Frame;
import java.awt.Window;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Framef=new Frame();
f.setBounds(30,30,200,200);
f.setVisible(true);
}
}
参考:http://www.cnblogs.com/growup/archive/2010/04/24/1971548.html
Object:基类
-|Component:基本组件
--|Container:容器
--|Button等具体组件
-|MenuComponent:菜单组件
--|MenuBar:菜单栏
--|MentItem:菜单项
---|Menu:菜单
根据子组件自动调整大小pack()。
大小:setSize(),
位置:setLocation()。
大小和位置:setBounds()。
可见性(默认不可见):setVisible()。
增:add()。
删:
改:
查:getComponents(),getComponentAt(),getComponentCount()。
//Main.java
package lee;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextField;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
Panelp1=new Panel();
p1.add(new Button("Hello"));
f.add(p1);
ScrollPanep2=new ScrollPane();
p2.add(new TextField(200));
f.add(p2);
}
}
可以指定对齐方式,间距(水平、垂直)。
//Main.java
/**
* Demo
*/
package lee;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODOAuto-generated method stub
Frame f=new Frame();
f.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.add(new Button("Hello"));
f.add(new TextField(20));
}
}
默认添加到Center区域中。
//Main.java
/**
* Demo
*/
package lee;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(new BorderLayout(20, 10));
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.add(new Button("NORTH"),BorderLayout.NORTH);
f.add(new Button("WEST"),BorderLayout.WEST);
f.add(new Button("CENTER"),BorderLayout.CENTER);
f.add(new Button("EAST"),BorderLayout.EAST);
f.add(new Button("SOUTH"),BorderLayout.SOUTH);
f.pack();
}
}
//Main.java
/**
* Demo
*/
package lee;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(new GridLayout(3, 2));
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.add(new Button("1"));
f.add(new Button("2"));
f.add(new Button("3"));
f.add(new Button("4"));
f.add(new Button("5"));
f.pack();
}
}
//Main.java
/**
* Demo
*/
package lee;
import java.awt.Button;
import java.awt.Frame;
importjava.awt.GridBagConstraints;
import java.awt.GridBagLayout;
/**
* @author [email protected]
*
*/
public class Main {
private GridBagLayout gb;
private Frame f;
private GridBagConstraints gbc;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().init();
}
public void init(){
f=new Frame();
gb=new GridBagLayout();
f.setLayout(gb);
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
gbc = new GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
// gbc.weightx=1;
// gbc.weighty=1;
gbc.gridwidth=2;
gbc.gridwidth=GridBagConstraints.REMAINDER;
addButton(new Button("1"));
gbc.gridwidth=1;
gbc.gridheight=3;
addButton(new Button("2"));
gbc.gridheight=1;
gbc.gridwidth=GridBagConstraints.REMAINDER;
addButton(new Button("3"));
gbc.gridwidth=GridBagConstraints.REMAINDER;
addButton(new Button("4"));
gbc.gridwidth=GridBagConstraints.REMAINDER;
addButton(new Button("5"));
f.pack();
}
public void addButton(Button btn){
gb.setConstraints(btn, gbc);
f.add(btn);
}
}
//Main.java
/**
* Demo
*/
package lee;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
CardLayout c=new CardLayout(30, 20);
f.setLayout(c);
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.add("1",new Button("1"));
f.add("2",new Button("2"));
f.add("3",new Button("3"));
f.add("4",new Button("4"));
f.add("5",new Button("5"));
f.pack();
c.show(f,"3");
}
}
//Main.java
/**
* Demo
*/
package lee;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(null);
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
Button btn1=new Button("1");
btn1.setBounds(0, 0, 100, 100);;
f.add(btn1);
Button btn2=new Button("2");
btn2.setBounds(50, 100, 100, 100);;
f.add(btn2);
}
}
//Main.java
/**
* Demo
*/
package lee;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.swing.Box;
import javax.swing.BoxLayout;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
Box hori=Box.createHorizontalBox();
hori.add(new Button("1"));
f.add(hori,BorderLayout.NORTH);
Box ver=Box.createVerticalBox();
ver.add(new Button("2"));
f.add(ver,BorderLayout.WEST);
Box ver2=Box.createVerticalBox();
ver2.add(new Button("3"));
ver2.add(new Button("4"));
ver2.add(new Button("5"));
f.add(ver2,BorderLayout.CENTER);
f.pack();
}
}
Buttonbtn1=new Button("Btn1");
btn1.addActionListener(newActionListener() {
。。。。。。
}
//Main.java
package lee;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setBounds(0, 0, 200, 200);
f.setTitle("Hello,World!");
f.setVisible(true);
f.setLayout(new FlowLayout());
Buttonbtn1=new Button("Btn1");
f.add(btn1);
btn1.addActionListener(newActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("xx");
}
});
}
}
事件分为低级事件(基于组件的基本事件)和高级事件(低级事件的抽象)。每种事件都其对应的接口(可能含有多个事件)。
为了只处理关心的事件,可以使用事件适配器(默认实现了接口所有方法),只改写关心的方法。
菜单条menubar:用于放置菜单,使用container.setMenuBar(mb)添加菜单。
-|菜单menu:用于旋转菜单项
--|菜单项menuitem:各个功能菜单按钮
---|标签String:显示的菜单文本
---|快捷键MenuShortcut:ctrl、shift等结合的虚拟键
//Main.java
/**
* Demo
*/
package lee;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
//menubar
MenuBarmb=new MenuBar();
//file menu
Menu mFile=newMenu("File");
mb.add(mFile);
MenuItem mi=newMenuItem("Open",new MenuShortcut(KeyEvent.VK_A,false));
mFile.add(mi);
mFile.addSeparator();
MenuItem miSave=new MenuItem("Save",newMenuShortcut(KeyEvent.VK_S,false));
mFile.add(miSave);
//add menubar
f.setMenuBar(mb);
//event
mi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Open");
}
});
miSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Save");
}
});
}
}
弹出菜单:用于放置菜单,使用container.add(pop)添加菜单,使用pop.show()显示。
-|菜单menu:用于旋转菜单项
--|菜单项menuitem:各个功能菜单按钮
---|标签String:显示的菜单文本
---|快捷键MenuShortcut:ctrl、shift等结合的虚拟键
//popup
finalPopupMenu pop=new PopupMenu("xx");
//file menu
Menu mFile=new Menu("File");
pop.add(mFile);
MenuItem mi=new MenuItem("Open",newMenuShortcut(KeyEvent.VK_A,false));
mFile.add(mi);
mFile.addSeparator();
MenuItem miSave=new MenuItem("Save",newMenuShortcut(KeyEvent.VK_S,false));
mFile.add(miSave);
f.add(pop);
f.addMouseListener(newMouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if(e.isPopupTrigger()){
pop.show(f,e.getX(), e.getY());
}
}
});
//Main.java
/**
* Demo
*/
package lee;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.PopupMenu;
importjava.awt.event.ActionEvent;
import java.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Frame f=new Frame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
//popup
finalPopupMenu pop=new PopupMenu("xx");
//file menu
Menu mFile=new Menu("File");
pop.add(mFile);
MenuItem mi=new MenuItem("Open",newMenuShortcut(KeyEvent.VK_A,false));
mFile.add(mi);
mFile.addSeparator();
MenuItem miSave=new MenuItem("Save",newMenuShortcut(KeyEvent.VK_S,false));
mFile.add(miSave);
f.add(pop);
//event
mi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Open");
}
});
miSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Save");
}
});
f.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if(e.isPopupTrigger()){
pop.show(f,e.getX(), e.getY());
}
}
});
}
}
repaint()调用update(),update()调用paint()。
class MyCanvas extends Canvas{
private int shape;
@Override
publicvoid paint(Graphics g) {
// TODO Auto-generated method stub
if(0==shape){
g.drawRect(0, 0, 50, 50);
}
else{
g.drawOval(0,50,50,50);
}
}
public int getShape() {
return shape;
}
public void setShape(int shape) {
this.shape = shape;
}
}
finalMyCanvas c = new MyCanvas();
c.setPreferredSize(newDimension(100,100));
//Main.java
/**
* Demo
*/
package lee;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Frame f=new Frame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
//add canvas
finalMyCanvas c = new MyCanvas();
c.setPreferredSize(newDimension(100,100));
f.add(c,BorderLayout.NORTH);
//add button
Button btn1=new Button("Rect");
Button btn2=new Button("Oval");
f.add(btn1,BorderLayout.WEST);
f.add(btn2,BorderLayout.CENTER);
//btn listener
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.setShape(0);
c.repaint();
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.setShape(1);
c.repaint();
}
});
}
}
//graphics canvas
class MyCanvas extends Canvas{
private int shape;
@Override
publicvoid paint(Graphics g) {
// TODO Auto-generated method stub
if(0==shape){
g.drawRect(0, 0, 50, 50);
}
else{
g.drawOval(0,50,50,50);
}
}
public int getShape() {
return shape;
}
public void setShape(int shape) {
this.shape = shape;
}
}
参考:http://blog.csdn.net/qgjava/article/details/5860079
目标:绘制图形。
原理:抽象画笔提供各种绘图接口,具体实现由JVM根据具体底层OS实例化后传给paint()。
方法:绘制直线,字符串,矩形等图形。
try {
Image srcImage=ImageIO.read(newFile("D:/Test/Penguins.jpg"));
g.drawImage(srcImage,0,0,100,100,null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImagebuf=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
GraphicsgBuf=buf.getGraphics();
gBuf.drawOval(0,0, 50, 50);
g.drawOval(0, 0, 50, 50);
try {
ImageIO.write(buf, "jpeg", newFile("D:/Test/My.jpg"));
} catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Main.java
/**
* Demo
*/
package lee;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* @author [email protected]
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Frame f=new Frame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
//add canvas
final MyCanvas c = new MyCanvas();
c.setPreferredSize(new Dimension(100,100));
f.add(c,BorderLayout.NORTH);
//add button
Button btn1=new Button("Read");
Button btn2=new Button("Write");
f.add(btn1,BorderLayout.WEST);
f.add(btn2,BorderLayout.CENTER);
//btn listener
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.setShape(0);
c.repaint();
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.setShape(1);
c.repaint();
}
});
}
}
//graphics canvas
class MyCanvas extendsCanvas{
private int shape;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
if(0==shape){//read image
try {
Image srcImage=ImageIO.read(new File("D:/Test/Penguins.jpg"));
g.drawImage(srcImage,0,0,100,100,null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
BufferedImagebuf=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics gBuf=buf.getGraphics();
gBuf.drawOval(0,0, 50, 50);
g.drawOval(0, 0, 50, 50);
try {
ImageIO.write(buf, "jpeg", newFile("D:/Test/My.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getShape() {
return shape;
}
public void setShape(int shape) {
this.shape = shape;
}
}