need help with Frame & Scrollbar

J

Jenny

Hi,

I found an old post under this title. The code had some error so I
change it a little. But when I run it, it has an empty window and I
cannot close it. How can I display the 2 buttons with the scroll
bars?
Why is the exit control button not working?

Thanks a lot.


Here is my code:


import java.awt.*;
import javax.swing.*;
class Scroll {
public static void main(String[] args){

Frame f = new Frame ("Demo");
JScrollPane s = new JScrollPane();
Panel p = new Panel();
Button b1 = new Button("Button 1");
Button b2 = new Button("Button 2");

f.add("Center", s);
s.add(p);
p.add(b1);
p.add(b2);
f.setSize(200,200);
f.setVisible(true);
}}
 
J

Jenny

Sorry, in my code, I should only use swing objects. Here is new code.

import java.awt.*;
import javax.swing.*;
class Scroll {
public static void main(String[] args){

Frame f = new Frame ("Demo");
JScrollPane s = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel p = new JPanel();
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
p.add(b1);
p.add(b2);
s.add(p);
f.add("Center", s);
f.setSize(200,200);
f.setVisible(true);
}}
 
A

Andrew Thompson

On 12 Aug 2004 17:27:38 -0700, Jenny wrote:

Sorry, in my code, I should only use swing objects. Here is new code. ....

Frame f = new Frame ("Demo");

JFrame f = new JFrame("Demo");

You will find the behaviour of the code changes,
and you should no longer simply f.add( component )

Read up on both in the JAvaDocs.
<http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html>

....
f.add("Center", s);

f.add(s, BorderLayout.CENTER);

<http://java.sun.com/j2se/1.4.2/docs/api/java/awt/BorderLayout.html>

...where did you get that crappy code?
 
A

Andrew Thompson

f.add(s, BorderLayout.CENTER);

Or rather, since this is a JFrame and I had
specifically commented on it above..

f.getContentPane().add(...);

[ D'uhh! ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top