File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Building a simple interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Building a simple interface" Watch "Building a simple interface" New topic
Author

Building a simple interface

Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
What is wrong with the following program?
--------------------------------------------


import javax.swing.*;
import java.awt.*;

public class Playback extends JFrame {
public Playback() {
super("Playback");
setSize(225, 80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JButton play = new JButton("Play");
JButton stop = new JButton("Stop");
JButton pause = new JButton("Pause");
add(play);
add(stop);
add(pause);
setVisible(true);
}

public static void main(String[] args) {
Playback pb = new Playback();
}
}


---------------------------------------------


The program compiles, but when I run it, i get the following exception error:

java.lang.Error: Do not use Playback.setLayout() use Playback.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.setLayout(JFrame.java:531)
at Playback.<init>(Playback.java:11)
at Playback.main(Playback.java:22)
Exception in thread "main"


Thanks in advance.........
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I haven't tried to compile it, but it should be OK if compiled with java version 1.5+
if you have 1.4, this
setLayout(flo);

needs to be
getContentPane().setLayout(flo);
(as the message states)

similar for these
add(play);
add(stop);
add(pause);
Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
Yes, I am using ver 1.4.2...

However, I made the corrections as both the error and you suggested and it worked! Here is what the program looks like now:

import javax.swing.*;
import java.awt.*;

public class Playback extends JFrame {
public Playback( ) {
super("Playback");
setSize(225, 80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
FlowLayout flo = new FlowLayout( );
getContentPane().setLayout(flo);
JButton play = new JButton("Play");
JButton stop = new JButton("Stop");
JButton pause = new JButton("Pause");
getContentPane().add(play);
getContentPane().add(stop);
getContentPane().add(pause);
setVisible(true);
}

public static void main(String[] args) {
Playback pb = new Playback( );
}
}



Thanks for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Building a simple interface