天天看点

Java(八)GUI:1.Eclipse进行可视化插件——WindowBuilder的安装先上例子:知识点

先上例子:

例1:背景图片式(这个写法比较麻烦)

背景图片是组件显示在背景图片上,组件与图片不重合。

JFrame frame = new JFrame();
	
	JPanel panel= new JPanel();
	
	//在JLable中添加图片标签ImageIcon
	ImageIcon ii=new ImageIcon("C:/Users/sheep羊/desktop/testapp.gif");
	JLabel label = new JLabel(ii);
	
	//设置标签的大小位置   
	label.setBounds(0, 0, ii.getIconWidth(), ii.getIconHeight());  
	
	// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明  
	panel = (JPanel) frame.getContentPane();
	
	//设置背景是否可见
	panel.setOpaque(false);  
	
	//设置内容窗格的布局管理器,默认的布局管理器为BorderLayout  
	panel.setLayout(new FlowLayout());
	
	frame.getLayeredPane().setLayout(null);  
	// 把背景图片添加到分层窗格的最底层作为背景  
	frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));  
	
	frame.setSize(ii.getIconWidth(), ii.getIconHeight());
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setResizable(true);  
	frame.setVisible(true);  
	frame.setLocation(0, 0);
           

例2:​​​​​​Lable标签添加图片

import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {
	public static void main(String[] args) throws Exception{

		JFrame frame = new JFrame();

		//在JLable中添加图片标签ImageIcon		
		String gif="C:Users/sheep羊/desktop/testapp.gif";
		ImageIcon ii=new ImageIcon(gif);
		JLabel label = new JLabel(ii);
		
		//设置标签的大小位置   
		label.setBounds(0, 0, ii.getIconWidth(), ii.getIconHeight());  
		
		frame.add(label);
//注意加上不同的位置BorderLayout.NORTH,否则默认加到BorderLayout.center,就覆盖了lable
		frame.add(new JButton(), BorderLayout.NORTH);
		
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);  
		frame.setVisible(true);		
	}
}
           

例3:重写paintComponent(Graphics g)方法

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Main {
	public static void main(String[] args) throws Exception{
		JFrame frame = new JFrame();
		frame.add(new myComponent());
		frame.add(new JButton(), BorderLayout.NORTH);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);  
		frame.setVisible(true);  
		
	}
}
class myComponent extends JComponent
{
	private static final String gif="C:Users/sheep羊/desktop/testapp.gif";
	private static final Image image=new ImageIcon(gif).getImage();
private static final int width = i.getIconWidth();
	private static final int height = i.getIconHeight();
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(i.getImage(), 0, 9, this);
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(width,height);
	}	
}
           

知识点

知识点一:布局

布局详解:

知识点二:面板

JFrame 中有四层面板:根面板、层级面板、内容面板、玻璃面板。

Java(八)GUI:1.Eclipse进行可视化插件——WindowBuilder的安装先上例子:知识点

其中的根面板、层级面板和玻璃面板人们并不太关心;它们是用来组织菜单栏和内容窗格以及实现观感的。

其中,Swing 程序员最关心的是内容窗格(contentpane) 。

在设计框架的时候, 要使用下列代码将所有的组件添加到内容窗格中:

Container contentPane = frame.getContentPaneO ;

Component c = . . .;

contentPane.add (c);

Java SE1.4后,可以直接调用frame.add(c)

知识点三:图像

ImageIcon

int getIconHeight()

Returns:the height in pixels of this icon

int getIconWidth()

Returns:the width in pixels of this icon

Image getImage()

Returns the Image object for this ImageIcon。将ImageIcon转化成Image类型,不能使用强制类型转化,只能用这个方法

Image

int getWidth(ImageObserver observer)

Parameters:

observer - an object waiting for the image to be loaded.调用的时候写this

Returns:

the width of this image, or -1 if the width is not yet known.

//方法一:ImageIcon.getIconWidth()获取长宽可以声明为静态变量

class myComponent extends JComponent
{
	private static final String gif="C:Users/sheep羊/desktop/testapp.gif";
	private static final ImageIcon ii=new ImageIcon(gif);
	private static final int width = ii.getIconWidth();
	private static final int height = ii.getIconHeight();
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(ii.getImage(), 0, 9, this);
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(width,height);
	}	
}

//方法二:Image.getWidth(ImageObserver observer)不可以声明为静态变量
class myComponent extends JComponent
{
	private static final String gif="C:Users/sheep羊/desktop/testapp.gif";
	private static final Image image=new ImageIcon(gif).getImage();
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(image, 0, 9, this);
	}

	@Override
	public Dimension getPreferredSize() {
		int width=image.getWidth(this);
		int height=image.getHeight(this);
		return new Dimension(width,height);
	}	
}
           

知识点四:Graphics

常用方法

void copyArea(int x, int y, int width, int height, int dx, int dy)

按dx和dy指定的距离复制组件的一个区域。从x和y指定的点开始,此方法向下和向右复制。若要向左或向上复制组件区域,请为dx或dy指定负值。如果源矩形的一部分位于组件边界之外,或被其他窗口或组件遮挡,则CopyArea将无法复制关联的像素。可以通过调用组件的绘制方法来刷新省略的区域。

Parameters:

x - the x coordinate of the source rectangle.

y - the y coordinate of the source rectangle.

width - the width of the source rectangle.

height - the height of the source rectangle.

dx - the horizontal distance to copy the pixels.

dy - the vertical distance to copy the pixels.

class myComponent extends JComponent
{
	private static final String file="D:/mystring/mypicture/timg.jpg";
	private static final Image image=new ImageIcon(file).getImage();
	
	@Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		int width=image.getWidth(this);
		int height=image.getHeight(this);
//null,g.drawImage()不画,由g.copyArea()画。要是this,g.copyArea的参数x,y是0表示使用完整的image,那没事都一样的效果.但x,y不是0的话,那么左上角的图完整,其他的图是裁剪的,不是都是想选定裁剪区域的了。
		g.drawImage(image,0,0,null);
		for(int i=0;i*width<=getWidth();i++)
			for(int j=0;j*height<=getHeight();j++)	 
			{
				g.copyArea( 0, 0, width, height, i*width, j*height);
			}
	}

	@Override
	public Dimension getPreferredSize() {
		int width=8*image.getWidth(this);
		int height=6*image.getHeight(this);
		return new Dimension(width,height);
	}	
}
           

知识点五:基础

Swing库

Swing库:Java应用程序用户界面开发包

Swing是一个用于开发Java应用程序用户界面的开发工具包。它以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风格。

继承层次

Java(八)GUI:1.Eclipse进行可视化插件——WindowBuilder的安装先上例子:知识点

常用方法

自己打一遍就记住了

  • Component所有GUI的祖类

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

setLocation()的升级版,不仅设置了组件在父容器的位置,还设置了组件的大小width,height 

void setLocation(int x,int y)

设定组件左上角在父容器的坐标位置

void setSize(int width, int height)

Resizes this component so that it has width width and height height.

void setSize(Dimension d)
public void setVisible(boolean b)
  • Container

setLayout(LayoutManager mgr)

设置这个容器的布局管理LayoutManager是哪种

  • JComponent

void paintComponent(Grphics g)

覆盖这个方法来描述应该如何绘制自己的组件。

void setOpaque(boolean isOpaque)

设定是否可以看到容器的背景,true就是可以看到组件下方的背景,默认false只能看到组件。

  • ​​​​​​​Window

void pack()

根据组件自动调整窗口大小。注意,这个需要你重写getPreferredSize()方法,否则就不是合适的尺寸。

void setLocationByPlatform(boolean b)

设置 locationByPlatform 属性。这个属性在窗口显示之前被设置,由平台选择一个合适的位置.

void toBack()

将这个窗口移到桌面窗口栈的后面, 并相应地重新排列所有的可见窗

void toFront()

将这个窗口显示在其他窗口前面

  • ​​​​​​​Frame

void setExtendedState(int state)

设置窗口状态。状态是下列值之一。

Frame.NORMAL

Frame.ICONIFIED

Frame.MAXIMIZED_HORIZ

Frame.MAXIMIZED_VERT

Frame.MAXIMIZED_BOTH

其中state为”Frame.MAXIMIZED_BOTH”时,设置框架全屏

void setIconImage(Image image)

设置框架的图标。

void setResizable(boolean b)

设置框架是否可被用户拉伸

void setTitle(String s)

设置框架标题栏中的文字。

  • ​​​​​​​JFrame

getLayeredPane()

Returns the layeredPane object for this frame.

  • ​​​​​​​一对属性

组件类的很多方法是以获取 / 设置方法对形式出现的,

·get/set 约定:

public String getTitleO

public void setTitle(String title)

·is/set约定:

对于类型为 boolean 的属性, 获取方法由 is 开头。

public boolean islocationByPIatfornO

public void setLocationByPIatforra(boolean b)

JFrame

  • 基本例子​​​​​​​
JFrame frame=new JFrame();
		frame.add(view);											//添加自定义组件view,默认布局为BorderLayout.CENTER
		JButton btn=new JButton("btn");
		frame.add(btn,BorderLayout.NORTH);
		frame.pack();												//按照容器内组件的大小来自适应大小,这个要在add组件之后
        frame.setTitle("Cells");									//窗口名
		frame.setVisible(true);									    //可显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		//关闭操作
		frame.setResizable(false);									//可拉伸
        frame.setSize(1000  , 500);									//窗口大小
           

其中JFrame frame = new JFrame();frame.setTitle("Cells");等同于JFrame frame=new JFrame("Cells");

  • 继承框架​​​​​​​

继承框架看起来比上面那个都写在Main里的清晰条理,以后就用这种格式。

public class Main {
	public static void main(String args[]) {
		JFrame f= new SimpleFrame();
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class SimpleFrame extends JFrame 
{
	//把参数独立成变量,而不是setSize(100,300):这样在写的时候可以用编译器自动填充,它会给出变量名的选项,到时只需要按方向键选择就行。
	
	private static final int DEFAULT_WIDTH= 1200;
	private static final int DEFAULT_HEIGHT= 800;
	private static BufferedImage bi;
	
	public SimpleFrame()
	{
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);			//设置框架大小
		setLocation(DEFAULT_WIDTH, DEFAULT_HEIGHT);		//设置框架位置
		setIconImage(bi);								//设置图标,这里没有具体些bi
		setTitle("hello Java");							//设置标题
	}
}
           

JButton

JButton btn = new JButton();
        frame.add(btn);
        btn.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("click");
			}
		});
           

JButton提供一个接口,就是这个ActionListener(),只要符合这个接口的,当JButton被点击时,就会反过来执行你重写的actionPerformed()方法。

·由按钮公布一个守听着接口和一对注册/注销函数

·你的代码实现那个接口,将守听着对象注册在按钮上。

·一旦按钮被按下,就会反过来调用你的守听者对象的某个函数。