2016-07-07 6 views

答えて

0

Javaクラスをアプレットプロジェクトの別々のファイルに作成することを意味しますか? 答えは「はい」です。

など。 (別のファイルに) 2つのクラス 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; 
    } 
} 
関連する問題