如何用java向上和向下滚动?

时间:2018-06-07 14:05:19

标签: java swing awt jbutton

我有JButton作为图片,当您点击它们时,标题会弹出。但是,我不知道如何滚动它们。

第一类

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);
       }
   }
}

主要

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);
   }
}

我怎么能够不破坏代码并且能够在没有滚动按钮​​的情况下滚动?

this is what it looks like now

1 个答案:

答案 0 :(得分:0)

您需要使用JScrollPane才能使视图滚动。

请查看Oracle的文档: https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

一个非常简单的例子:

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

您可能希望配置JScrollPane的详细信息。