关闭

applet常用的布局

284人阅读 评论(0) 收藏 举报

BorderLayout

 

package hb.applet;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class BorderLayoutTest extends Applet implements ActionListener{

	Button buttonWest,buttonSouth,buttonEast,buttonNorth,buttonCenter;
	
	@Override
	public void init() {
		
		buttonNorth = new Button("North");
		buttonSouth = new Button("North");
		buttonEast = new Button("North");
		buttonWest = new Button("North");
		buttonCenter = new Button("North");
		
		//指明当前的类采用“Border”布局
		this.setLayout(new BorderLayout());
		
		this.add(buttonNorth, BorderLayout.NORTH);
		this.add(buttonSouth, BorderLayout.SOUTH);
		this.add(buttonEast, BorderLayout.EAST);
		this.add(buttonWest, BorderLayout.WEST);
		this.add(buttonCenter, BorderLayout.CENTER);
		
		//因为当前类继承了ActionListener接口,所以listener的具体实现是可以用this的
		buttonNorth.addActionListener(this);
		buttonSouth.addActionListener(this);
		buttonEast.addActionListener(this);
		buttonWest.addActionListener(this);
		buttonCenter.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj == buttonNorth){
			int result = JOptionPane.showConfirmDialog(this, 
									"I am north", 
									"title -- north", 
									JOptionPane.OK_CANCEL_OPTION);
			System.out.println(result);
		}
		
		if(obj == buttonSouth){
			int result = JOptionPane.showConfirmDialog(this, 
					"I am south", 
					"title -- south", 
					JOptionPane.OK_CANCEL_OPTION);
			System.out.println(result);
		}
		
		if(obj == buttonEast){
			int result = JOptionPane.showConfirmDialog(this, 
					"I am east", 
					"title -- east", 
					JOptionPane.OK_CANCEL_OPTION);
			System.out.println(result);
		}
		
		if(obj == buttonWest){
			int result = JOptionPane.showConfirmDialog(this, 
					"I am west", 
					"title -- west", 
					JOptionPane.OK_CANCEL_OPTION);
			System.out.println(result);
		}
		
		if(obj == buttonCenter){
			Object[] options = {"Yes, please",
            "No way!"};
			int n = JOptionPane.showOptionDialog(this,
				    "Would you like green eggs and ham?",//content
				    "A Silly Question",//title
				    JOptionPane.YES_NO_OPTION,//type
				    JOptionPane.QUESTION_MESSAGE,
				    null,     //do not use a custom Icon
				    options,  //the titles of buttons
				    options[0]); //default button title

			if(n==0){
				JOptionPane.showMessageDialog(this, n);		
			}else{
				JOptionPane.showMessageDialog(this, n);
			}
		}
		
	}
	
}

 

FlowLayout

package hb.applet;

import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;

public class FlowLayoutTest extends Applet {

	Button b1,b2,b3;
	TextField tf;
	Label lab;
	
	@Override
	public void init() {
		
		b1 = new Button("b1");
		b2 = new Button("b2");
		b3 = new Button("b3");
		
		tf = new TextField("test");
		
		lab = new Label("Test FlowLayout");
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		this.add(b1);
		this.add(b2);
		this.add(b3);
		this.add(tf);
		this.add(lab);
		
	}
}

 

GridLayout

package hb.applet;

import java.applet.Applet;
import java.awt.Button;
import java.awt.GridLayout;

public class GridLayoutTest extends Applet {

	Button[] bs;
	
	@Override
	public void init() {
		bs = new Button[7];
		//GridLayout(int rows,int cols,int hgap,int vgap)
        //指定网格的行数、列数、行间距、列间距
		this.setLayout(new GridLayout(3,3,10,10));
		
		for(int i=0;i<bs.length;i++){
			bs[i]=new Button(Integer.toString(i));
			add(bs[i]);
		}
		
	}
}

 

CardLayout

package hb.applet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutTest2 extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private Panel panel_1, panel_2, panel_3, panel_4, panel_5;
	private Button btn_n, btn_p;
	private CardLayout c = new CardLayout();

	public CardLayoutTest2() {
		super("CardLayout");
		this.setBounds(150, 150, 300, 300);
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);//点击X按钮就能够正常退出

		panel_1 = new Panel();
		panel_1.setLayout(c);
		this.add(panel_1, "Center"); // panel_1在主框架中

		panel_2 = new Panel();
		panel_2.setLayout(new FlowLayout());
		btn_p = new Button("Previous");
		btn_p.addActionListener(this);

		btn_n = new Button("Next");
		btn_n.addActionListener(this);
		panel_2.add(btn_p);
		panel_2.add(btn_n);
		this.add(panel_2, "North"); // panel_2在主框架中

		panel_3 = new Panel();
		panel_3.setBackground(new Color(255, 0, 0));
		panel_1.add("red", panel_3);

		panel_4 = new Panel();
		panel_4.setBackground(new Color(0, 255, 0));
		panel_1.add("green", panel_4);

		panel_5 = new Panel();
		panel_5.setBackground(new Color(0, 0, 255));
		panel_1.add("blue", panel_5);

		this.setVisible(true);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn_p) {
			c.previous(panel_1);//翻转到指定容器的前一张卡片。
		}
		if (e.getSource() == btn_n)
			c.next(panel_1);//翻转到指定容器的下一张卡片
	}

	public static void main(String[] args) {
		new CardLayoutTest2();
	}

}

 

package hb.applet;

import java.awt.*;
import java.applet.Applet;

class ButtonPanel extends Panel {
	ButtonPanel(String id) {
		setLayout(new BorderLayout());
		add("Center", new Button(id));
	}
}

public class CardLayoutTest extends Applet {
	
	Button first = new Button("First"), 
		   second = new Button("Second"),
		   third = new Button("Third");
	
	Panel cards = new Panel();
	
	CardLayout cardLayout = new CardLayout();
	//先采用border布局,按钮在north,Card在center
	public void init() {
		setLayout(new BorderLayout());
		
		Panel p = new Panel();
		p.setLayout(new FlowLayout());
		p.add(first);
		p.add(second);
		p.add(third);
		add("North", p);
		
		cards.setLayout(cardLayout);
		cards.add("First card", new ButtonPanel("The first one"));
		cards.add("Second card", new ButtonPanel("The second one"));
		cards.add("Third card", new ButtonPanel("The third one"));
		add("Center", cards);
	}

	@Override
	public boolean action(Event evt, Object arg) {
		if (evt.target.equals(first)) {
			cardLayout.first(cards);
		} else if (evt.target.equals(second)) {
			//先跳到第一张,然后再跳到第二章,显示的就是中间的second的card
			cardLayout.first(cards);//翻转到容器的第一张卡片
			cardLayout.next(cards);// 翻转到指定容器的下一张卡片。
		} else if (evt.target.equals(third)) {
			cardLayout.last(cards);//翻转到容器的最后一张卡片。
		} else
			return super.action(evt, arg);
		return true;
	}
} 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:313316次
    • 积分:8746
    • 等级:
    • 排名:第2257名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论