Results 1 to 9 of 9
- 07-31-2011, 04:00 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
panel is not visible on my Jframe
Java Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import javax.media.ControllerEvent; import javax.media.ControllerListener; import javax.media.Manager; import javax.media.Player; import javax.media.RealizeCompleteEvent; import javax.swing.*; import javax.swing.JOptionPane; public class demo2 extends JFrame implements Runnable,ActionListener{ private Player player; final JFrame f =new JFrame(); private File file; private JPanel panel; public demo2() { super("KABOOM Player"); setLayout(new FlowLayout()); f.setVisible(true); f.setSize(600,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); initcomponents(); } public void initcomponents(){ JMenuBar mb= new JMenuBar(); JMenu fileone=new JMenu("File"); mb.add(fileone); JMenu playback=new JMenu("Playback"); mb.add(playback); JMenuItem op=new JMenuItem("Open"); fileone.add(op); JMenuItem ex=new JMenuItem("Exit"); fileone.add(ex); /* JMenuItem prev=new JMenuItem("Previous"); playback.add(prev); JMenuItem next=new JMenuItem("Next"); playback.add(next); */ JMenuItem st=new JMenuItem("Pause"); playback.add(st); JMenuItem pl=new JMenuItem("Play"); playback.add(pl); panel = new JPanel(); panel.setPreferredSize(new Dimension(1,1)); panel.setBackground(Color.red); JButton playit = new JButton("playss"); playit.addActionListener(this); getContentPane().add(panel); panel.add(playit,FlowLayout.TRAILING); JLabel lbl=new JLabel(new ImageIcon("/e3.gif")) ; lbl.setSize(300, 100); f.add(lbl); JButton stop=new JButton("Stop"); f.setLocation(20, 0); f.setVisible(true); f.add(stop,BorderLayout.CENTER); JButton play1=new JButton("Play"); play1.size(); f.add(play1,BorderLayout.WEST); op.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); createPlayer(); } }); ex.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { f.hide(); } }); st.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { player.stop(); } }); pl.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { player.start(); } }); f.setJMenuBar(mb); f.add(mb,5000); f.setBackground(Color.red); } public static void main(String args[]) { demo2 app = new demo2(); app.setTitle("KABOOM PLAYER"); app.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fileChooser.showOpenDialog(this); // user clicked Cancel button on dialog if (result == JFileChooser.CANCEL_OPTION) file = null; else file = fileChooser.getSelectedFile(); } private void createPlayer() { if (file == null) return; removePreviousPlayer(); try { // create a new player and add listener player = Manager.createPlayer(file.toURL()); // player.addControllerListener(new EventHandler()); player.start(); // start player } catch (Exception e) { JOptionPane.showMessageDialog(this, "Invalid file or location", "Error loading file", JOptionPane.ERROR_MESSAGE); } } private void removePreviousPlayer() { if (player == null) return; player.close(); Container c = getContentPane(); } public void run() { throw new UnsupportedOperationException("Not supported yet."); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand(). equals("play")){ player.start(); } } }
here when i am adding panel then neither the play and stop button and neither the picture is appearing..blank screen comes ie. even panel is not visible.
when i remove the panel the image and the two buttons appear on frame.
but i want to add buttons on panel and then panel on frame..plz helpLast edited by Fubarable; 07-31-2011 at 04:11 PM. Reason: code tags added
-
I'm not 100% sure what is wrong, but my guess is that you're using layout managers incorrectly, and in fact your code throws exceptions for this. Have you read the tutorial on how to use layout managers?
Note: I have edited your original post and have added code tags so your code will retain its formatting and be readable. To learn how to do this yourself, please read the link in my signature below.
-
Also, you are using setSize() rather than the preferred setPreferredSize(), you are not calling pack() on the JFrame and you are adding components after setVisible(true) has been called on the JFrame. So add all components first, then call pack(), then call setVisible(true), in that order.
- 07-31-2011, 08:28 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
i edited n implemented my code in that order but its not helping..
if i remove the code for panel then the buttons are visible that are on the frame. but when the code for panel is there even those buttons are not visible
-
Can you upload a picture what your GUI looks like and another picture of what you want it to look like? Either that or describe in great detail what you're trying to achieve because your current description kind of assumes that we know in advance what you're trying to achieve -- and we don't.
- 07-31-2011, 08:39 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
i am just tryin to make a JFrame..on which i want to add a panel with few buttons on it and a picture. also i want to add a menu bar on the frame..THAT'S IT.
- 07-31-2011, 08:51 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Then post your Short, Self Contained, Correct Example that demonstrates this. Most of the code you posted is completely unrelated to that requirement. ActionListener and file chooser code for example is the next step once you solve this problem. Code your program one step at a time. That is get the components showing. Then add some ActionListeners to the buttons. Then add some menus etc. That way you debug and solve incremental problems which is easier than trying to debug an entire program.i am just tryin to make a JFrame..on which i want to add a panel with few buttons on it
- 07-31-2011, 08:58 PM #8
Your code is creating two JFrames:i am just tryin to make a JFrame
Java Code:public class demo2 extends JFrame implements Runnable,ActionListener{ // One here private Player player; final JFrame f =new JFrame(); //<<<<<<<<<AND ONE HERE
- 08-03-2011, 02:18 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Build project - paint component in Panel not visible.
By aborgeld in forum NetBeansReplies: 10Last Post: 04-11-2011, 09:34 PM -
JFrame, isn't visible sometimes..
By AndroidAppNewbie in forum New To JavaReplies: 2Last Post: 03-06-2011, 02:13 PM -
panel on a panel not visible
By warchieflll in forum Advanced JavaReplies: 2Last Post: 01-29-2011, 08:29 PM -
Label not visible on panel
By warchieflll in forum Advanced JavaReplies: 4Last Post: 01-29-2011, 01:29 PM -
How to add panel to JFrame?
By geekchick in forum New To JavaReplies: 17Last Post: 08-01-2010, 05:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks