just want to know how to open a second form when a button is pressed
have more then 1 form
Page 1 of 1
have more then 1 form how do i get more then 1 form up
#2
Posted 04 October 2008 - 11:50 AM
Modifying the code of an example I was just given in another question, here is how you can show multiple forms.
Read through the in code comments and you will see that main creates a form object called "pizza". During this creation it calls the constructor which sets up the form and adds a button to the form. This button is attached to an event which creates more instances of the form.
So essentially this example will create as many forms as you like depending on how many times you click the button.
Hope this is straight forward enough for you. Enjoy!
"At DIC we be pizza form creating code ninjas... this form is brought to you by pizza ninjas at DIC... want pepperoni? Want mushrooms? Call 1-800-EAT-DICC for delivery code ninjas style!"
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PizzaForm extends JFrame implements ActionListener {
JButton button;
// Create an instance of the form and its operations
public PizzaForm() {
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("New Form");
setLayout(new BorderLayout());
// Create a button, attach it to an event (actionPerformed listed below) and add it to the form
button = new JButton("New Form");
button.addActionListener(this);
add(button,BorderLayout.NORTH);
// Show the form
setVisible(true);
}
// When the button is pressed, create a new instance of the class which shows a new form.
// Now this part can be any form class you created etc. Here I just keep creating the same type of form.
public void actionPerformed(ActionEvent e) {
PizzaForm pizza = new PizzaForm();
}
// On start create the form which calls the constructor, assembles the form, then shows it.
public static void main(String args[]){
PizzaForm pizza = new PizzaForm();
}
}
Read through the in code comments and you will see that main creates a form object called "pizza". During this creation it calls the constructor which sets up the form and adds a button to the form. This button is attached to an event which creates more instances of the form.
So essentially this example will create as many forms as you like depending on how many times you click the button.
Hope this is straight forward enough for you. Enjoy!
"At DIC we be pizza form creating code ninjas... this form is brought to you by pizza ninjas at DIC... want pepperoni? Want mushrooms? Call 1-800-EAT-DICC for delivery code ninjas style!"
Page 1 of 1

Start a new topic
Add Reply




MultiQuote

| 


