简体   繁体   中英

How to scroll up and down with java?

I have JButton as images and when you click them the title pops up. However, I do not know how to scroll with them.

Class one

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.net.MalformedURLException;
import java.io.File;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.UnsupportedLookAndFeelException;


public class QuoteGUI extends JPanel
{
   private final int WIDTH = 300, HEIGHT = 100;
   private JPanel primary;
   private JLabel quote;
   private JButton home, ut, cum, synopsis, vocab;
   private String homeQuote = "Latin Life Saver";
   private String utQuote = "Ut Clause";
   private String cumQuote = "Cum Clause";
   private String synopsisQuote = "Synopsis";
   private String vocabQuote = "Vocab";

   //-----------------------------------------------------------------
   //  Sets up a panel with a label and a set of radio buttons
   //  that control its text.
   //-----------------------------------------------------------------
   public QuoteGUI() 
   {

      quote = new JLabel (homeQuote);
      quote.setFont (new Font ("Helvetica", Font.BOLD, 24));

      home = new JButton ("Home");
      home.setBackground (Color.yellow);
              ImageIcon icon = createImageIcon("/Volumes/APCOMSCI/Screen Shot 2018-06-07 at 12.49.13 AM.png");
      ut = new JButton ("Ut Clause");
      ut.setIcon(icon);
      ut.setBackground (Color.red);
              ImageIcon don = createImageIcon("/Volumes/APCOMSCI/Cum_Clauses_Pic.jpg");
      cum = new JButton ("Cum Clause");
       cum.setIcon(don);
      cum.setBackground (Color.green);
              ImageIcon mon = createImageIcon("/Volumes/APCOMSCI/LATIN II 2010-11 subjunctive morphology paradgim SUM POSSUM.png");
      synopsis = new JButton ("Synopsis");
      synopsis.setIcon(mon);
      synopsis.setBackground (Color.blue);
              ImageIcon con = createImageIcon("");
      vocab = new JButton ("Vocab https://quizlet.com/291887149/caesar-dbg-a-call-to-conquest-book-1-chapters-1-5-flash-cards/");
      vocab.setIcon(con);
      vocab.setBackground (Color.blue);

      ButtonGroup group = new ButtonGroup();
      group.add (home);
      group.add (ut);
      group.add (cum);
      group.add (synopsis);
      group.add (vocab);

      QuoteListener listener = new QuoteListener();
      home.addActionListener (listener);
      ut.addActionListener (listener);
      cum.addActionListener (listener);
      synopsis.addActionListener (listener);
      vocab.addActionListener (listener);
      primary = new JPanel();
      primary.add (quote);
      primary.add (home);
      primary.add (ut);
      primary.add (cum);
      primary.add (synopsis);
      primary.add (vocab);
      primary.setBackground (Color.pink);
      primary.setPreferredSize (new Dimension(WIDTH, HEIGHT));

   }
   protected static ImageIcon createImageIcon(String path) {
        //      java.net.URL imgURL = NewJApplet.class.getResource(path);

        try {
            java.net.URL imgURL = (new File(path)).toURL();
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
            }
        } catch (MalformedURLException ex) {
            System.out.println(ex);
        }
        return null;
    }
   //-----------------------------------------------------------------
   //  Returns the primary panel containing the GUI.
   //-----------------------------------------------------------------
   public JPanel getPanel()
   {
      return primary;
   }

   //*****************************************************************
   //  Represents the listener for all radio buttons
   //*****************************************************************
   private class QuoteListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Sets the text of the label depending on which radio
      //  button was pressed.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
         Object source = event.getSource();

         if (source == home)
            quote.setText (homeQuote);
         else
            if (source == ut)
               quote.setText (utQuote);
            else
               if (source == cum)
                   quote.setText (cumQuote);
                   else
                   if (source == synopsis)
                   quote.setText (synopsisQuote);
                    else
                        quote.setText (vocabQuote);
       }
   }
}

Main

import javax.swing.*;

public class QuoteOptions
{
   //-----------------------------------------------------------------
   //  Creates and presents the program frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame quoteFrame = new JFrame ("Quote Options");
      quoteFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      QuoteGUI gui = new QuoteGUI();
      quoteFrame.getContentPane().add (gui.getPanel());

      quoteFrame.pack();
      quoteFrame.setVisible(true);
   }
}

How exactly would I be able to not destroy the code and have the ability to scroll without the scroll buttons there?

this is what it looks like now

You need to use JScrollPane in order to make your view scrollable.

Please check the docs from Oracle: https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

A very simple example:

  quoteFrame.getContentPane().add (new JScrollPane(gui.getPanel()));

You might want to configure the details of the JScrollPane though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM