Java イベントリスナー メッセージボックスJavaのボタンのイベントで、「こんにちは」のメッセージボックスを表示
前回は、ソースは全てCUIで行い、Hello Worldのフォームを表示してみたのだが、
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class World {
private JFrame frmHelloWorld;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
World window = new World();
window.frmHelloWorld.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public World() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmHelloWorld = new JFrame();
frmHelloWorld.setTitle("Hello World");
frmHelloWorld.setBounds(100, 100, 450, 300);
frmHelloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmHelloWorld.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("ボタン");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"Hello World","こんにちは",JOptionPane.YES_OPTION | JOptionPane.INFORMATION_MESSAGE);
}
});
btnNewButton.setBounds(0, 0, 79, 37);
frmHelloWorld.getContentPane().add(btnNewButton);
}
}
|