PDA

View Full Version : چطوری پنجره جدید رو ببندم؟



ali zi zeperto
پنج شنبه 28 بهمن 1389, 10:14 صبح
سلام
من روی فرمم یه دکمه گذاشتم با زدنش یه فرم جدید باز میشه و و م یخوام بعد از اینکه دکمه ok روی فرم جدید زده شد این پنجره جدیده بسته بشه همینطور اگر دکمه cansel زده شد.
چیکار کنم؟:متفکر:
در ضمن از jframe استفاده کرده ام.

javaphantom
پنج شنبه 28 بهمن 1389, 11:11 صبح
سلام
من روی فرمم یه دکمه گذاشتم با زدنش یه فرم جدید باز میشه و و م یخوام بعد از اینکه دکمه ok روی فرم جدید زده شد این پنجره جدیده بسته بشه همینطور اگر دکمه cansel زده شد.
چیکار کنم؟:متفکر:
در ضمن از jframe استفاده کرده ام.


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

/**
* Created by IntelliJ IDEA.
* User: Babak
* Date: 2/17/11
* Time: 11:24 AM
* To change this template use File | Settings | File Templates.
*/
public class Main extends JFrame {

private JButton okButton;

public Main() {

this.setBounds(new Rectangle(300,300));
this.setDefaultCloseOperation(SubMain.EXIT_ON_CLOS E);
this.setLayout(new FlowLayout());
this.getContentPane().add(this.getOkButton());

this.setVisible(true);

}

public JButton getOkButton() {
if (this.okButton == null) {
this.okButton = new JButton("Ok");
this.okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new SubMain();
}
});
}
return okButton;
}


public static void main(String[] s) {
new Main();
}
}

class SubMain extends JFrame {

private JButton cancelButton;


public SubMain() {

this.setBounds(new Rectangle(300,300));
this.setDefaultCloseOperation(SubMain.EXIT_ON_CLOS E);
this.setLayout(new FlowLayout());
this.getContentPane().add(this.getCancelButton());
this.setVisible(true);
}

public JButton getCancelButton() {
if (this.cancelButton == null) {
this.cancelButton = new JButton("Cancel");
this.cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});

}
return cancelButton;
}
}