نوشته شده توسط
abdollah110110
سلام
دوستان لطفا
چگونگی ایجاد فرم و کلید(دکمه) و رویداد را با کد و مثال توضیح بدین.
خیلی متشکرم
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyForm extends JFrame {
private JPanel myPanel;
private JButton myButton;
public MyForm() {
this.initialComponents();
}
private void initialComponents() {
this.setBounds(new Rectangle(200,200));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.getContentPane().add(this.getMyPanel());
this.setVisible(true);
}
public JPanel getMyPanel() {
if (this.myPanel == null) {
this.myPanel = new JPanel();
this.myPanel.setLayout(new FlowLayout());
this.myPanel.add(this.getMyButton());
}
return myPanel;
}
public JButton getMyButton() {
if (this.myButton == null) {
this.myButton = new JButton("Click On Me");
this.myButton.setBackground(Color.GREEN);
// create an event
this.myButton.addActionListener(new ActionListener() {
private boolean flag = true;
public void actionPerformed(ActionEvent e) {
if (flag) {
myButton.setBackground(Color.BLUE);
} else {
myButton.setBackground(Color.GREEN);
}
flag = !flag;
}
});
}
return myButton;
}
public static void main(String[] s) {
new MyForm();
}
}