From: Amr on 14 Feb 2010 23:38 public class Beeper extends JPanel implements ActionListener {} that is a snippet from a code from an excercise give in the book "Sams teach...." my problem is, how do i know whether to give <extends JPanel> or <extends JFrame>? both extenstions are used in the book. thank you very much for your help.
From: Peter Duniho on 15 Feb 2010 00:02 Amr wrote: > public class Beeper extends JPanel implements ActionListener {} > > that is a snippet from a code from an excercise give in the book "Sams > teach...." > > my problem is, how do i know whether to give <extends JPanel> or > <extends JFrame>? > both extenstions are used in the book. What do you want your class to do? Which of JPanel or JFrame is most like what you want your class to do? The JPanel and JFrame classes are similar, but different in notable ways, all of which are covered in the documentation. It's hard to understand why you'd be confused about which you want, if you know what you want your own component to do. And even if you're having trouble figuring it out, you haven't told us what your component should do, so we can't answer the question for you. If you don't know what you want your own component to do, well�I could see how that might be a problem. But I don't think any of us can help much with that. :) Pete
From: Amr on 15 Feb 2010 00:16 On Feb 15, 10:02 am, Peter Duniho <NpOeStPe...(a)NnOwSlPiAnMk.com> wrote: > > What do you want your class to do? Which of JPanel or JFrame is most > like what you want your class to do? > > The JPanel and JFrame classes are similar, but different in notable > ways, all of which are covered in the documentation. It's hard to > understand why you'd be confused about which you want, if you know what > you want your own component to do. And even if you're having trouble > figuring it out, you haven't told us what your component should do, so > we can't answer the question for you. > > If you don't know what you want your own component to do, well I could > see how that might be a problem. But I don't think any of us can help > much with that. :) > > Pete hi thank you for your reply. the programme would have a button, and it would beep on each click. anyway here is the full code :) import java.awt.event.ActionListener; import javax.swing.JFrame; /** * * @author arshad */ import java.awt.*; import java.awt.event.ActionEvent; import javax.swing.*; public class Beeper extends JPanel implements ActionListener { JButton button; public Beeper(){ super(new BorderLayout()); button=new JButton("Click Me"); button.setPreferredSize(new Dimension(200,80)); add(button,BorderLayout.CENTER); button.addActionListener(this); } public void actionPerformed(ActionEvent e){ Toolkit.getDefaultToolkit().beep(); } // // create the Gui and show it. for thread safety, // this method should be invoded from the evennt dispatching thread private static void createAndShowGUI(){ //create and set up the window. JFrame frame =new JFrame("Beeper"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create and set up the content pane JComponent newContentPane=new Beeper(); newContentPane.setOpaque(true); //content pane must be opaqu frame.setContentPane(newContentPane); //Display the window frame.pack(); frame.setVisible(true); } public static void main(String arg[]){ //schedule a job for the even-dispatchin thread //creating and shwoing this applications GUI javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run() { createAndShowGUI();}}); } }
From: markspace on 15 Feb 2010 02:26 Amr wrote: > public class Beeper extends JPanel implements ActionListener {} > > that is a snippet from a code from an excercise give in the book "Sams > teach...." > > my problem is, how do i know whether to give <extends JPanel> or > <extends JFrame>? > both extenstions are used in the book. > > thank you very much for your help. Knowledge and experience. Knowledge of the API and what it does helps you pick which classes are appropriate to inherit from. Experience tells you which of those classes are the best, if any, for a given circumstance. Unfortunately both knowledge and experience are hard to get quickly. You'll have to learn at the best pace you can manage. In my opinion, you usually should extend JPanel. JFrame is a top level window, and that's pretty much all it can be. JPanel can fit in other panels or in a JFrame. JPanel is more flexible, and easier for you to use in a program. Also, neither should extend ActionListener. Make that a separate class. The behavior of being a JPanel (of JFrame) and the behavior of being an ActionListener are fairly different. Don't make one object have both behaviors, separate those two concepts so they're easier to deal with. You often see simple teaching examples where one single class does everything: it extends ActionListener, extends JFrame, and assembles the entire GUI too. This isn't really appropriate for larger programs, where this "do it all" behavior would make maintenance more difficult. Separate those concerns to different classes. Those do it all classes are really only trying to save space so the demo can be as brief as possible. I think those do it all classes are heading down the road of being a God object, a known anti-pattern: <http://en.wikipedia.org/wiki/God_object>
From: RedGrittyBrick on 15 Feb 2010 04:53 On 15/02/2010 05:16, Amr wrote: > On Feb 15, 10:02 am, Peter Duniho<NpOeStPe...(a)NnOwSlPiAnMk.com> > wrote: > >> >> What do you want your class to do? Which of JPanel or JFrame is most >> like what you want your class to do? >> >> The JPanel and JFrame classes are similar, but different in notable >> ways, all of which are covered in the documentation. It's hard to >> understand why you'd be confused about which you want, if you know what >> you want your own component to do. And even if you're having trouble >> figuring it out, you haven't told us what your component should do, so >> we can't answer the question for you. >> >> If you don't know what you want your own component to do, well…I could >> see how that might be a problem. But I don't think any of us can help >> much with that. :) >> >> Pete > > > hi thank you for your reply. > the programme would have a button, and it would beep on each click. > > anyway here is the full code :) > > import java.awt.event.ActionListener; > import javax.swing.JFrame; > > /** > * > * @author arshad > */ > > import java.awt.*; > import java.awt.event.ActionEvent; > import javax.swing.*; > public class Beeper extends JPanel implements ActionListener { > > JButton button; > public Beeper(){ > super(new BorderLayout()); > button=new JButton("Click Me"); > button.setPreferredSize(new Dimension(200,80)); > add(button,BorderLayout.CENTER); > button.addActionListener(this); > > } > > public void actionPerformed(ActionEvent e){ > Toolkit.getDefaultToolkit().beep(); > } > // > // create the Gui and show it. for thread safety, > // this method should be invoded from the evennt > dispatching thread > > private static void createAndShowGUI(){ > //create and set up the window. > JFrame frame =new JFrame("Beeper"); > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > > //create and set up the content pane > > JComponent newContentPane=new Beeper(); > newContentPane.setOpaque(true); //content pane must be opaqu > frame.setContentPane(newContentPane); > > //Display the window > frame.pack(); > frame.setVisible(true); > } > > public static void main(String arg[]){ > //schedule a job for the even-dispatchin thread > //creating and shwoing this applications GUI > > javax.swing.SwingUtilities.invokeLater(new Runnable(){ public > void run() { createAndShowGUI();}}); > } > } In this example, the choice is purely arbitrary. You can write a program with identical functionality where Beeper extends JFrame, JPanel or neither. Many would argue that you should prefer composition over inheritance - this would mean that class Beeper doesn't extend any other class, it constructs a JFrame, JPanel and JButton in a method. There are other aspects to this example which I dislike. If this is from the book, I'd worry it is teaching bad habits.
|
Next
|
Last
Pages: 1 2 3 4 Prev: JFreeChart - Legend control with adding different series Next: Adding JPanel to JFrame |