기록: 자바 프로그램은main 함수가 없고 init 함수만 어떻게 실행하거나 applet 애플릿이 어떻게 실행하는지

1706 단어 Android편.
【예1-1】
Java 프로그램 J507.java:
4
import java.applet.*;import java.awt.*;
import java.awt.event.*;import javax.swing.*;
class MyPanel extends JPanel{
	int x;JLabel label1;
	MyPanel(int a){
		x=a;getSize();
		label1=new JLabel("   "+x+"   ");add(label1);
	}
	public Dimension getPreferredSize(){
		return new Dimension(200,50);
	}
}
public class J507 extends Applet implements ActionListener{
	CardLayout mycard;MyPanel myPanel[];JPanel p;
	private void addButton(JPanel pan,String butName,ActionListener listener){
		JButton aButton=new JButton(butName);
		aButton.addActionListener(listener);
		pan.add(aButton);
	}
	public void init(){
		setLayout(new BorderLayout());//           
		mycard=new CardLayout();
		this.setSize(400,150);
		p=new JPanel();p.setLayout(mycard);//p           
		myPanel=new MyPanel[10];
		for(int i=0;i<10;i++){
			myPanel[i]=new MyPanel(i+1);
			p.add("A"+i,myPanel[i]);
		}
		JPanel p2=new JPanel();
		this.addButton(p2,"   ",this);
		this.addButton(p2,"    ",this);
		addButton(p2,"   ",this);
		addButton(p2,"   ",this);
		add(p,"Center"); add(p2,"South");
	}
	public void actionPerformed(ActionEvent e){
		if (e.getActionCommand().equals("   "))mycard.first(p);
		else if(e.getActionCommand().equals("    "))mycard.last(p);
		else if(e.getActionCommand().equals("   "))mycard.previous(p);
		else if(e.getActionCommand().equals("   "))mycard.next(p);
	}
}
test를 작성하고 있습니다.html 파일:
 
  
$ appletviewer test.html

좋은 웹페이지 즐겨찾기