Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Building a simple interface

 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic