Results 1 to 3 of 3
- 03-18-2009, 04:03 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
Next/Prev Button, Chapter Buttons
Hey guys, I'll try to be as clear as possible with what im trying to do, but please forgive me in advance if i dont make it understandable. So here goes, Im trying to make a java "book" like it has buttons that lead to different chapters and possibly a previous and next button. but my problem is that the buttons are at a different panel, called my control panel, and the display is another panel. So my question is how do i get my control panel buttons to affect what is displayed on my display panel? I hope i made that as clear as possible =/:confused:
Thanks in advance! :D
-Paul.
-
Two patterns may help you here. One is the observable pattern which Swing frequently uses in it GUI/model widgets, and the other is the MVC pattern. Wikipedia has articles on both, and both could be applied here to solve your problem.
- 03-18-2009, 07:45 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
err I've been reading the patterns on wikipedia and am ashamed to say that I don't understand it at all =/. I'm a novice java user and when I asked my teacher, all he said to me was "they're all in the same class" or something. Didnt really help as much considering he was the one who assigned it to us. Anywho, maybe if i posted my code it'd be more helpful.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Book extends JFrame
{
JFrame frame;
Toolkit toolkit;
Dimension screen;
Container contentPane;
int screenWidth, screenHeight,
headerX, headerY, headerWidth, headerHeight,
displayX, displayY, displayWidth, displayHeight,
controlX, controlY, controlWidth, controlHeight;
public static void main (String [] args)
{
Book myBook = new Book ();
}
public Book ()
{
frame = new JFrame ();
toolkit = frame.getToolkit ();
screen = toolkit.getScreenSize ();
screenWidth = screen.width;
screenHeight = screen.height;
headerX = (int) (screenWidth * 0.1);
headerY = (int) (screenHeight * 0.03);
headerWidth = (int) (screenWidth * 0.8);
headerHeight = (int) (screenHeight * 0.07);
displayX = (int) (screenWidth * 0.01);
displayY = (int) (screenHeight * 0.12);
displayWidth = (int) (screenWidth * 0.77);
displayHeight = (int) (screenHeight * 0.77);
controlX = (int) (screenWidth * 0.79);
controlY = (int) (screenHeight * 0.12);
controlWidth = (int) (screenWidth * 0.20);
controlHeight = (int) (screenHeight * 0.77);
frame.setTitle ("My Java Book");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setBounds (0, 0, screenWidth, screenHeight);
frame.setLayout (null);
contentPane = frame.getContentPane ();
HeaderPanel1 h1 = new HeaderPanel1 (headerX, headerY, headerWidth, headerHeight);
contentPane.add (h1);
DisplayPanel1 d1 = new DisplayPanel1 (displayX, displayY, displayWidth, displayHeight);
contentPane.add (d1);
ControlPanel1 c1 = new ControlPanel1 (controlX, controlY, controlWidth, controlHeight);
contentPane.add (c1);
//frame.pack ();
frame.setVisible (true);
}
}
class HeaderPanel1 extends JPanel
{
private Border border;
public HeaderPanel1 (int x, int y, int width, int height)
{
border = BorderFactory.createRaisedBevelBorder ();
setBorder (border);
setBounds (x, y, width, height);
}
public void paintComponent (Graphics g)
{
Font big = (new Font("sansserif", Font.BOLD, 20));
g.setFont(big);
g.setColor(Color.RED);
g.drawString ("History of Basketball", 350, 35);
}
}
class DisplayPanel1 extends JPanel
{
private Border border;
public DisplayPanel1 (int x, int y, int width, int height)
{
border = BorderFactory.createRaisedBevelBorder ();
setBorder (border);
setBounds (x, y, width, height);
}
public void paintComponent (Graphics g)
{
g.drawString ("PLAY BALL", 50, 50);
}
}
class ControlPanel1 extends JPanel
{
JButton prev,next,chap1,chap2,chap3;
private Border border;
public ControlPanel1 (int x, int y, int width, int height)
{
border = BorderFactory.createRaisedBevelBorder ();
setBorder (border);
setBounds (x, y, width, height);
setLayout(null);
Font label = (new Font("sansserif", Font.BOLD+Font.ITALIC, 20));
prev = new JButton ("Previous");
prev.setBounds(10,550,120,50);
prev.setFont(label);
add(prev);
next = new JButton ("Next");
next.setBounds(130,550,120,50);
next.setFont(label);
add(next);
}
private class PrevListener implements ActionListener{
public void actionPerformed (ActionEvent e){
DisplayPanel1.drawString(""); (i guess this is the part where im having difficulties.)
}
}
}
Thanks in advance for any help!
-novie java user
Similar Threads
-
Buttons and TextFields
By Xystus777 in forum New To JavaReplies: 27Last Post: 03-17-2009, 01:26 AM -
[SOLVED] Two centered buttons with right justified button
By AlwaysJava in forum AWT / SwingReplies: 2Last Post: 10-29-2008, 11:48 PM -
Applet buttons
By h3ckf1r3 in forum Java AppletsReplies: 6Last Post: 09-22-2008, 09:15 PM -
How to use SWT Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM -
Next and Previous Buttons
By JavaNewb in forum New To JavaReplies: 1Last Post: 05-09-2008, 01:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks