I want to layout the following:
An image and a button on a same 'pane that is scrollable. (i.e. the button would not be viewable until it is scrolled)
I tried the following code, but there simply is not scroll bars!!
When the frame is resized, the picture is 'croped', and everthing else is resized!
Code:
|-----------------------
| | |
| | |
| | |
| image | |
| | |
| | |
| | | <- scroll bar
| | |
| | |
| -------- | |
| |button| | |
| -------- | |
|------------------------
An image and a button on a same 'pane that is scrollable. (i.e. the button would not be viewable until it is scrolled)
I tried the following code, but there simply is not scroll bars!!
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.applet.*;
import java.net.URL;
public class TestScrollFrame
{
public static void main (String[] cool)
{
JFrame myFrame = new CoolFrame();
myFrame.show();
}
}
class CoolFrame extends JFrame
{
public CoolFrame()
{
setTitle("Frame Test");
Toolkit tk= Toolkit.getDefaultToolkit();
Dimension d= tk.getScreenSize();
setSize(d.width,d.height);
setLocation(0,0);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane = getContentPane();
Component viewedComponent = new MyBorderPanel();
JScrollPane sp = new JScrollPane(viewedComponent,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
contentPane.add(sp);
}
}
class MyFlowPanel extends JPanel
{
private Image myImage;
MediaTracker tracker;
public MyFlowPanel(Image myImage)
{
this.myImage= myImage;
tracker = new MediaTracker(this);
tracker.addImage(myImage,0);
try
{
tracker.waitForID(0);
} //try
catch (InterruptedException e)
{
e.printStackTrace();
} //catch
setLayout(new FlowLayout(FlowLayout.LEFT));
} //Constructor
public void paintComponent(Graphics g)
{
g.drawImage(myImage,5,5,null);
}
} //class MyFlowPanel
class MyBorderPanel extends JPanel
{
public MyBorderPanel()
{
Toolkit tk= Toolkit.getDefaultToolkit();
try
{
Image theImg = Toolkit.getDefaultToolkit().getImage("myImg.jpg");
MyFlowPanel imgPanel = new MyFlowPanel(theImg);
add(imgPanel,"Center");
JPanel soundPanel = new JPanel(new FlowLayout());
JButton soundButton = new JButton ("A Button");
soundButton.addActionListener(new ButtonListener());
soundPanel.add(soundButton);
add(soundPanel,"South");
} //try
catch (Exception e)
{
e.printStackTrace();
} //catch
} //Constructor
} //MyBorderPanel
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//do something
} //actionPerformed
} //ButtonListener
When the frame is resized, the picture is 'croped', and everthing else is resized!