help needed with java programming

Code:
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class TryGridLayout  {

static JFrame aWindow = new JFrame("This is a Grid Layout");

public static void main(String[] args) {

aWindow.setBounds(100,100,1024,768); 

aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridLayout grid = new GridLayout(3,4); 

Container content = aWindow.getContentPane(); 

content.setLayout(grid);

JButton button = null; 

for(int i = 1; i <= 10; i++) {

content.add(button = new JButton("Press "+ i)); 

}

aWindow.setVisible(true); 

}

}

program code extracted from ivor hortons java 5

1.can action listeners be added to the buttons in this program???

2.why is JFrame static ???

3.usually in college we write like "public classname extends JFrame", but it is not used in this program code , what diff does this make ???

i couldnt find suitable answers in complete reference java 2 ,, so askin here!!!!
 
Code:
import javax.swing.*;

import java.awt.*;

class MyClass extends JFrame

{

  public MyClass()

  {

  setVisible(true);

  setDefaultCloseOperation(EXIT_ON_CLOSE);

  }

}

public static void main(String [] args)

{

 MyClass()

}

is this program code correct?
 
iml3g3nd said:
Code:
import javax.swing.*;

import java.awt.*;

class MyClass extends JFrame

{

  public MyClass()

  {

  setVisible(true);

  setDefaultCloseOperation(EXIT_ON_CLOSE);

  }

}

public static void main(String [] args)

{

 MyClass()

}
is this program code correct?

in your main function,..you should create an object of the MyClass class. you are currently defining a constructor in the main function(as well as in your MyClass.) hence the code will be:

Code:
new MyClass();
in the main function.

(will get back you on your first post :))
 
iml3g3nd said:
Code:
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class TryGridLayout  {

static JFrame aWindow = new JFrame("This is a Grid Layout");

public static void main(String[] args) {

aWindow.setBounds(100,100,1024,768); 

aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridLayout grid = new GridLayout(3,4); 

Container content = aWindow.getContentPane(); 

content.setLayout(grid);

JButton button = null; 

for(int i = 1; i <= 10; i++) {

content.add(button = new JButton("Press "+ i)); 

}

aWindow.setVisible(true); 

}

}

program code extracted from ivor hortons java 5

1.can action listeners be added to the buttons in this program???

It can be added but be sure to implement the proper listener in the class

2.why is JFrame static ???

Search the use of static keyword and it will clear your query

3.usually in college we write like "public classname extends JFrame", but it is not used in this program code , what diff does this make ???

nothing

i couldnt find suitable answers in complete reference java 2 ,, so askin here!!!!

and in your next program your main function is outside the main class braces so correct that also.
 
1)ActionListener can be added to the button.

just implement the ActionListener interface with your class and then override the actionPerformed method in it.

Code:
public class TryGridLayout implements ActionListener

{

         //rest of the code

         

         public void actionPerformed(ActionEvent e)

         {

                  if(e.getSource == <name_of_button>)

                  {

                      //do stuff

                   }

           }

}
rest of the questions have been answered i guess....
 
Back
Top