2016-07-07 96 views

回答

0

你的意思是你的applet項目中的java類在單獨的文件中? 答案是肯定的。

E.g. 兩類 MyApplet.javaPerson.java

enter image description here

這裏是MyApplet.java

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

public class MyApplet extends JApplet implements ActionListener { 
    public void init() { 
      JButton button = new JButton("Click Me!"); 
     button.addActionListener(this); 
     getContentPane().add(button); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Person person = new Person(); 
     person.setName("Jack"); 
     person.setAge(18); 
     String title = "Greetings"; // Shown in title bar of dialog box. 
     String message = "Hello " + person.getName() + " from the Swing User Interface Library."; 
     JOptionPane.showMessageDialog(null, message, title, 
       JOptionPane.INFORMATION_MESSAGE); 
    } 
} 

這裏是Person.java類(在一個單獨的文件)

public class Person { 
    String name; 
    int age; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 
} 
相關問題