Results 1 to 1 of 1
- 12-06-2010, 02:46 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Using Jscrollpane to move images on jpanel
basicially
I have code that is quite messy,
its a graphical programme that reviews movies.
I have jscrollpane on the frame, and I want it to scroll down the images on the j panel .
I want it to be able to atlest scroll 1 jpanels= war movies,
, and maybe jtabbed panes.
thanks,
CH
ps the programme is personal,for myslef getting used to java graphics.:)......
code * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//Code to scroll the page. currently not working.
JScrollBar scrollBar1 = new JScrollBar (
JScrollBar.VERTICAL, 0, 5, 0, 100);
JScrollBar scrollBar2 = new JScrollBar (
JScrollBar.HORIZONTAL, 0, 5, 0, 100);
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
full programme below
Java Code:import java.awt.event.*; import java.awt.*; import javax.swing.*; class Main extends JFrame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Books & Movies Review Programme"); Container cp = frame.getContentPane(); cp.setLayout(new BorderLayout()); // Panels for adventure tab JPanel A0 = new JPanel(); JPanel A1 = new JPanel(); JPanel A2 = new JPanel(); JPanel A3 = new JPanel(); JPanel A4 = new JPanel(); // Panels for Books tab JPanel B0 = new JPanel(); JPanel B1 = new JPanel(); JPanel B2 = new JPanel(); JPanel B3 = new JPanel(); JPanel B4 = new JPanel(); //code for J tool bar // not currently displaying, also icons to stuck together. JButton j1 = getButton("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\open16.gif"); JButton j2 = getButton("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\save16.gif"); j1.setToolTipText("open file"); j1.setText("open"); j2.setToolTipText("save file"); j2.setText("save"); JToolBar jtb = new JToolBar(); jtb.add(j1); jtb.add(j2); // Code for J Menu // working but no Functionality JMenu menu1 = new JMenu("File"); menu1.add(new JMenuItem("New")); menu1.add(new JMenuItem("open")); menu1.add(new JSeparator()); menu1.add(new JMenuItem("save")); menu1.add(new JMenuItem("save as...")); menu1.add(new JSeparator()); menu1.add(new JMenuItem("Exit")); JMenu menu2 = new JMenu("Help"); menu2.add(new JMenuItem("Help contents")); menu2.add(new JSeparator()); menu2.add(new JMenuItem("About...")); JMenuBar bar = new JMenuBar(); bar.add(menu1); bar.add(menu2); //Code to scroll the page. currently not working. JScrollBar scrollBar1 = new JScrollBar ( JScrollBar.VERTICAL, 0, 5, 0, 100); JScrollBar scrollBar2 = new JScrollBar ( JScrollBar.HORIZONTAL, 0, 5, 0, 100); // tabbed pane is for adventure panel final JTabbedPane jtp = new JTabbedPane(); JLabel alabel = new JLabel(); ImageIcon aicon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\tibet.png"); alabel.setIcon(aicon); A0.add(alabel); JLabel blabel = new JLabel(); ImageIcon bicon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\face.png"); blabel.setIcon(bicon); A1.add(blabel); JLabel clabel = new JLabel(); ImageIcon cicon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\touch.png"); clabel.setIcon(cicon); A2.add(clabel); JLabel dlabel = new JLabel(); ImageIcon dicon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\feet.png"); dlabel.setIcon(dicon); A3.add(dlabel); JLabel elabel = new JLabel(); ImageIcon eicon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\Wild.png"); elabel.setIcon(eicon); A4.add(elabel); // name of tabs jtp.setBackground(Color.black); jtp.setForeground(Color.GRAY); jtp.addTab("Seven Years in tibet",A0); jtp.addTab("North Face",A1); jtp.addTab("Touching the Void",A2); jtp.addTab("As Far as my Feet can carry me",A3); jtp.addTab("Into the Wild",A4); // tabs for the book panel * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * final JTabbedPane jtbooks = new JTabbedPane(); JLabel b0label = new JLabel(); ImageIcon b0icon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\escape.png"); b0label.setIcon(b0icon); B0.add(b0label); JLabel b1label = new JLabel(); ImageIcon b1icon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\shake.png"); b1label.setIcon(b1icon); B1.add(b1label); JLabel b2label = new JLabel(); ImageIcon b2icon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\alone.png"); b2label.setIcon(b2icon); B2.add(b2label); JLabel b3label = new JLabel(); ImageIcon b3icon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\knee.png"); b3label.setIcon(b3icon); B3.add(b3label); JLabel b4label = new JLabel(); ImageIcon b4icon = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\worst.png"); b4label.setIcon(b4icon); B4.add(b4label); // name of tabs jtbooks.setBackground(Color.GREEN); jtbooks.setForeground(Color.GRAY); jtbooks.addTab("Escape or DIE",B0); jtbooks.addTab("Shake Hands With ",B1); jtbooks.addTab("WE Die Alone",B2); jtbooks.addTab("Bury My Heart",B3); jtbooks.addTab("Worst Journey",B4); //Home Panel final JPanel homePanel = new JPanel(); homePanel.setBackground(Color.black); ImageIcon icon1 = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\p1image.png"); JLabel homelabel = new JLabel("You have reached the home panel"); JLabel label1 = new JLabel(); label1.setIcon(icon1); homePanel.add(homelabel); homePanel.add(label1); //War movies Panel final JPanel warPanel = new JPanel(); ImageIcon icon2 = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\Warmovie.png"); ImageIcon notice = new ImageIcon("C:\\Users\\matt\\Desktop\\mojavemp\\Images\\notice.png"); warPanel.setBackground(Color.black); JLabel label2 = new JLabel(); JLabel Nlabel = new JLabel(); label2.setIcon(icon2); Nlabel.setIcon(notice); JLabel warlabel = new JLabel("War Movies PANEL"); warPanel.add(warlabel); warPanel.add(label2); //warPanel.add(Nlabel); //Adventure Movies Panel final JPanel adventurePanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); adventurePanel.setBackground(Color.darkGray); JLabel adventureLabel = new JLabel(" ."); adventurePanel.add(adventureLabel); adventurePanel.add(jtp); // Top 10 books Panel final JPanel booksPanel = new JPanel(); booksPanel.setBackground(Color.darkGray); JLabel booksLabel = new JLabel("top 10 Books Panel "); booksPanel.add(jtbooks); //Recommend you movies panel * * * * * * * * * * * * * * * * * * * * * * * * final JPanel topMoviePanel = new JPanel(); final JPanel comments = new JPanel(); final JPanel Choices = new JPanel(); comments.setBackground(Color.GRAY); // header for the comments page. comments.add(new JLabel(" ")); JLabel LFont = new JLabel(" Contact us with your Movie Choices "); Font font = new Font("Verdana", Font.BOLD, 22); // Setting the text colour LFont.setFont(font); LFont.setForeground(Color.YELLOW); comments.add(LFont); // spacing labels. comments.add(new JLabel(" ")); comments.add(new JLabel(" ")); comments.add(new JLabel(" ")); comments.add(new JLabel("Enter your Email Address ")); comments.add(new JTextField(" @.com")); comments.add(new JLabel(" ")); comments.add(new JLabel("Enter your Your Name ")); comments.add(new JTextField("enter your text here")); comments.add(new JLabel(" ")); comments.add(new JLabel("Please Enter your Address ")); comments.add(new JTextArea("enter your text here",6,2)); comments.add(new JLabel(" ")); comments.add(new JLabel("Please Enter your favourate Movies ")); // code to add/ remove items form jcombobox final JTextField tfText = new JTextField(15); JButton button1 = new JButton("Remove"); JButton button2 = new JButton("Add"); String items[] = {"..", " ..", "..", ".."}; final JComboBox combo = new JComboBox(items); ActionListener removeAction = new ActionListener() { public void actionPerformed(ActionEvent e) { combo.removeItemAt(0); } }; button1.addActionListener(removeAction); ActionListener addAction = new ActionListener() { public void actionPerformed(ActionEvent e) { combo.addItem(tfText.getText()); } }; button2.addActionListener(addAction); Choices.add(tfText); Choices.add(button1); Choices.add(button2); Choices.add(combo); Choices.setBackground(Color.darkGray); Choices.setLayout(new FlowLayout(FlowLayout.CENTER)); comments.add(Choices); //end of code * * * * * * * * * ** * ** * * * * * * * * * * * ** * * comments.add(new JLabel(" ")); comments.add(new JLabel("Select your Favourate Genre(s)")); comments.add(new JLabel(" ")); JRadioButton Action = new JRadioButton("Action Movies "); Action.setSelected(true); JRadioButton Adventure = new JRadioButton("Adventure Movies "); Adventure.setSelected(false); JRadioButton Comedy = new JRadioButton("Comedy Movies "); Comedy.setSelected(false); JRadioButton Crime = new JRadioButton("Crime & Gangstger Movies"); Crime.setSelected(false); JRadioButton Historical = new JRadioButton("Historical Movies"); Historical.setSelected(false); JRadioButton Drama = new JRadioButton("Drama Movies"); Drama.setSelected(false); comments.add(Action); comments.add(Adventure); comments.add(Comedy); comments.add(Crime); comments.add(Historical); comments.add(Drama); comments.add(new JLabel(" ")); comments.add(new JLabel(" Do you wish to save your choices ")); comments.add(new JCheckBox("Yes")); comments.add(new JCheckBox("No")); comments.add(new JButton("Submit")); comments.add(new JButton("Reset")); comments.setLayout(new BoxLayout(comments, BoxLayout.Y_AXIS)); comments.setBorder(BorderFactory.createEmptyBorder(10, 10, 15, 15)); topMoviePanel.setBackground(Color.darkGray); JLabel movielabel = new JLabel("Your choice of Movies"); topMoviePanel.add(comments, BorderLayout.EAST); //Button Panel JPanel buttonPanel = new JPanel(); //Home button JButton homeButton = new JButton("Mojave Home"); ActionListener toggleHome = new ActionListener() { public void actionPerformed(ActionEvent e) { topMoviePanel.setVisible(false); booksPanel.setVisible(false); adventurePanel.setVisible(false); warPanel.setVisible(false); homePanel.setVisible(true); } }; homeButton.addActionListener(toggleHome); //War movies Button JButton warButton = new JButton("War Movies"); ActionListener toggleWar = new ActionListener() { public void actionPerformed(ActionEvent e) { homePanel.setVisible(false); topMoviePanel.setVisible(false); booksPanel.setVisible(false); adventurePanel.setVisible(false); warPanel.setVisible(true); } }; warButton.addActionListener(toggleWar); //Adventure Button JButton adventureButton = new JButton("Adventure Movies"); ActionListener toggleAdventure = new ActionListener() { public void actionPerformed(ActionEvent e) { warPanel.setVisible(false); homePanel.setVisible(false); topMoviePanel.setVisible(false); booksPanel.setVisible(false); adventurePanel.setVisible(true); } }; adventureButton.addActionListener(toggleAdventure); //Books Button JButton BooksButton = new JButton("Books Reviews"); ActionListener toggleBooks = new ActionListener() { public void actionPerformed(ActionEvent e) { adventurePanel.setVisible(false); warPanel.setVisible(false); homePanel.setVisible(false); topMoviePanel.setVisible(false); booksPanel.setVisible(true); } }; BooksButton.addActionListener(toggleBooks); //contact with your own movie Choices button JButton topMovieButton = new JButton("Movie Choice"); ActionListener togglemovie = new ActionListener() { public void actionPerformed(ActionEvent e) { homePanel.setVisible(false); booksPanel.setVisible(false); adventurePanel.setVisible(false); warPanel.setVisible(false); topMoviePanel.setVisible(true); } }; topMovieButton.addActionListener(togglemovie); buttonPanel.add(homeButton); buttonPanel.add(warButton); buttonPanel.add(adventureButton); buttonPanel.add(BooksButton); buttonPanel.add(topMovieButton); //Create the a panel like a deck of "cards". JPanel cards = new JPanel(new CardLayout()); cards.add(homePanel, "1"); cards.add(warPanel, "2"); cards.add(adventurePanel, "3"); cards.add(booksPanel, "4"); cards.add(topMoviePanel, "5"); cp.add(jtb, BorderLayout.NORTH); cp.add(buttonPanel, BorderLayout.NORTH); cp.add(cards, BorderLayout.CENTER); cp.add(scrollBar2, BorderLayout.SOUTH); cp.add(scrollBar1, BorderLayout.EAST); frame.setJMenuBar(bar); frame.pack(); frame.setSize(950,750); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static JButton getButton(String fn) { ImageIcon ic = null; try { ic = new ImageIcon("Images/" + fn); } catch(Exception e) {ic=null;} return new JButton(ic); } }
Similar Threads
-
JPanel/JScrollPane issue
By Moncleared in forum New To JavaReplies: 1Last Post: 09-23-2010, 04:02 AM -
problem positioning a JPanel in a JScrollPane
By gib65 in forum AWT / SwingReplies: 4Last Post: 08-02-2010, 10:34 AM -
Mouselistener for a Jscrollpane which contains Jpanel within it.
By bikashg in forum New To JavaReplies: 1Last Post: 05-09-2010, 06:34 PM -
Move Jpanel to center
By tpyq in forum NetBeansReplies: 1Last Post: 12-15-2008, 10:11 AM -
Need help making ball move and bounce off of sides of JPanel
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-01-2007, 07:48 AM


LinkBack URL
About LinkBacks

Bookmarks