Nnnnn Sssss

Greenhorn
+ Follow
since Oct 06, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nnnnn Sssss

My question deals with proper placement of code (if there is such a thing as proper placement).

In the code sample below, the About dialog is shown when a button is clicked, however the code from which the dialog is created is contained within the event listener attahced to the button! The code sample is from a skeleton file for a class project -- rules are that all code must be contained within one file.

My question is... ideally (ignoring the class project's rules)... where should the code to create the About dialog box be? Should I have created a class called AboutDialog that extends JFrame?

And what if I wanted to write code such that it wouldn't spawn additional About dialogs if one were already visible? I think I could just have a member variable of type AboutDialog and check to see if it were null every time the button to spawn an About dialog is clicked. Then, if it were null, initialize the dialog, if not, do nothing.

Whew, if you've made it this far, congrats!

Thanks
-N SCJP

-------- CODE SAMPLE -------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class App {
JFrame frame;
JButton about;
public static void main(String[] args) {
App me = new App();
me.go();
}
public void go() {
frame = new JFrame("Launch another frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
about = new JButton("About");
about.addActionListener(new AboutListener());
frame.getContentPane().add(about);
frame.setVisible(true);
}
public class AboutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame popUp = new JFrame("About frame");
popUp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popUp.setSize(250,200);
MyPanel panel = new MyPanel();
popUp.getContentPane().add(panel);
popUp.setVisible(true);
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
g.drawString("Your stuff goes here...", width/4, height/2);
}
}
}
}

[ October 06, 2004: Message edited by: Nihal Sinha ]
[ October 06, 2004: Message edited by: Nihal Sinha ]
19 years ago