applet常用控件(一)

JButton或者Button

package hb.applet.action;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestButton extends WindowAdapter{

	public static void main(String[] args) {
		//Frame类是Window类的子类
		Frame f = new Frame("test Button");
		Button b = new Button("button");
		//如果运行的this页继承了ActionListener接口就可以用this了
		b.addActionListener(new ButtonHandler());
		f.add(b,"Center");
		
		//关闭窗口添加的适配器
		f.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				System.out.println("System.exit(0)");
				System.exit(0);
			}
		});
		//调整此窗口的大小,以适合其子组件的首选大小和布局。
		f.pack();
		f.setVisible(true);
	}

}

class ButtonHandler implements ActionListener{
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("action occurred");
		System.out.println("Button's label is : " + e.getActionCommand() );
	}
	
}

 

CheckBox和JButtonRadio

package hb.applet.action;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
/*
 * CheckBox的事件触发是ItemListener
 * JRadioButton的事件触发是ActionListener
 * */
public class CheckBoxRadioTest extends JApplet implements ItemListener,ActionListener{

	int i1=0,i2=0,i3=0;
	int fonti=10;
	Font font;
	Container ctp = this.getContentPane();
	JLabel lb = new JLabel("请选择");
	JCheckBox cb1,cb2,cb3;
	JRadioButton r1,r2,r3;
	ButtonGroup bg = new ButtonGroup();
	
	
	@Override
	public void init() {
		ctp.setLayout(new FlowLayout());
		cb1 = new JCheckBox("红色",false);
		cb1.addItemListener(this);
		ctp.add(cb1);
		
		cb2 = new JCheckBox("绿色",false);
		cb2.addItemListener(this);
		ctp.add(cb2);
		
		cb3 = new JCheckBox("蓝色",false);
		cb3.addItemListener(this);
		ctp.add(cb3);
		
		r1 = new JRadioButton("10");
		r1.addActionListener(this);
		ctp.add(r1);
		
		r2 = new JRadioButton("20");
		r2.addActionListener(this);
		ctp.add(r2);
		
		r3 = new JRadioButton("30");
		r3.addActionListener(this);
		ctp.add(r3);
		
		bg.add(r1);
		bg.add(r2);
		bg.add(r3);
		
		ctp.add(lb);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		JCheckBox cbx = (JCheckBox)e.getItem();
		if(cbx.getText()=="红色"){
			if(e.getStateChange()==e.SELECTED){
				i1 = 255;
			}else{
				i1 = 0;
			}
		}
		if (cbx.getText() == "绿色") {
			if (e.getStateChange() == e.SELECTED)
				i2 = 255;
			else
				i2 = 0;
		}
		if (cbx.getText() == "蓝色") {
			if (cbx.isSelected())
				i3 = 255; // 判断组件是否被选
			else
				i3 = 0;
		}
		
		font = new Font("宋体",Font.BOLD,fonti);
		lb.setFont(font);
		lb.setForeground(new Color(i1,i2,i3));

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//下面这种方式也能够准确的找到数据源是哪个
//		if(e.getSource()==r1){
//			System.out.println(e.getSource());
//		}
		String rbt = e.getActionCommand();//返回与此动作相关的命令字符串
		if (rbt == "10")
			fonti = 10;
		else if (rbt == "20")
			fonti = 20;
		else
			fonti = 30;
		
		font = new Font("宋体",Font.BOLD,fonti);
		lb.setFont(font);
		lb.setForeground(new Color(i1,i2,i3));
	}
}

 

closeWindow

 

package hb.applet.action;
import javax.swing.JFrame;

public class CloseWindow {

	public static void main(String[] args) {
		JFrame frame = new JFrame("title");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

 

JComboBoxTest

package hb.applet.action;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JLabel;

public class JComboBoxTest extends JApplet implements ItemListener {

	Container ctp = this.getContentPane();
	JLabel lb1 = new JLabel("姓名"),lb2 = new JLabel("英语"),lb3 = new JLabel("");
	String name[]={"李林","赵欣","张扬","童梅"},score[]={"80","94","75","87"};
	
	JComboBox cbx = new JComboBox();
	
	
	@Override
	public void init() {
		ctp.setLayout(new FlowLayout());
		
		for(int j = 0; j < name.length; j++){
			cbx.addItem(name[j]);
		}
		
		ctp.add(lb1);
		ctp.add(cbx);
		
		cbx.addItemListener(this);
		
		ctp.add(lb2);
		ctp.add(lb3);
	}
	
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		
		int c = 0;
		String str = (String)e.getItem();
		for(int i=0;i<name.length;i++){
			if(str==name[i]){
				c = cbx.getSelectedIndex();
				lb3.setText(score[c]);
			}
		}
		
	}

}

 

JListTest

package hb.applet.action;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JListTest extends JApplet implements ListSelectionListener {

	JList lis = null;
	JLabel lb = null;
	String[] s = { "小学", "初中", "高中", "大学", "研究生" };

	@Override
	public void init() {

		Container container = this.getContentPane();
		container.setLayout(new FlowLayout());

		lb = new JLabel();
		// 将数组作为JList显示的内容
		lis = new JList(s);
		// 设置列表框的可见选项的行数,选项超过则出现滚动条
		lis.setVisibleRowCount(3);
		// 设置列表框的边框文本
		lis.setBorder(BorderFactory.createTitledBorder("请选择"));

		lis.addListSelectionListener(this);

		container.add(lb, BorderLayout.NORTH);
		// 将lis对象放入滚动容器,再将此内容加载到界面上
		container.add(new JScrollPane(lis), BorderLayout.CENTER);

	}

	@Override
	public void valueChanged(ListSelectionEvent listselectionevent) {
		// TODO Auto-generated method stub
		int m = 0;
		String str = "选取的是:";
		int[] index = lis.getSelectedIndices();
		for (int i = 0; i < index.length; i++) { // 根据取得的下标值,找到相应的数组元素
			m = index[i];
			str = str + s[m] + "  ";
		}
		lb.setText(str);// 输出选中项的值

	}

}

 

 

package hb.applet.action;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class JTextJTextareaTest extends JApplet implements ActionListener {

	Container ctp = this.getContentPane();
	ImageIcon icon1 = new ImageIcon("duplex.GIF");
	ImageIcon icon2 = new ImageIcon("oneside.GIF");
	JLabel lb1 = new JLabel("输入文字后按回车:",icon1,JLabel.CENTER);
	JLabel lb2 = new JLabel("输出结果:",icon2,JLabel.CENTER);
	JTextField tf1 = new JTextField(10);
	JTextArea tf2 = new JTextArea(5,10);
	
	@Override
	public void init() {
		ctp.setLayout(new FlowLayout());
		ctp.add(lb1);
		ctp.add(tf1);
		
		ctp.add(lb2);
		ctp.add(tf2);
		
		tf1.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent actionevent) {
		String str;
		str = tf1.getText();
		tf2.append(str+"\n");
	}

}

 

你可能感兴趣的:(applet)