Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Building a simple interface

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.........
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic