简体   繁体   中英

Swing JFrame horizontal scrollbar

I am trying make a graph using java swing which I'm new at. In my program I am getting some random numbers from text file based on which the graph will be created. I have been able to make the graph but I need to add a scroll bar to see the full graph, which is long enough to fit in the generated window. Could anyone modify or suggest me a very simple so

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import javax.swing.*; // used to access the JFrame, JPanel, JLabel, and JScrollPane class
import java.awt.*; // used to access the Font class

public class NewLine {
   MyPanel panel;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });
    }

    private static void createAndShowGUI() {
        System.out.println("Created GUI on EDT? "+
        SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing Paint Demo");




 f.setResizable(true);
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 //panel=new MyPanel();  

        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }
}

class MyPanel extends JPanel {
    private static final Color GRAPH_COLOR = Color.red;
    private static final Color GRID_COLOR = Color.gray;

    public MyPanel() {
        JScrollPane scroll = new JScrollPane(this, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        //JLabel label = new JLabel("This is a JScrollPane Component");

    }

    public Dimension getPreferredSize() {
        return new Dimension(250,200);
    }


    void drawLines(Graphics g) {


        Graphics2D g2d = (Graphics2D) g;

         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setColor(GRID_COLOR);


         for(int x =20 ; x <=1020; x +=25 ){
         g2d.drawLine(x, 70, x, 500);  // Y AXIS
         }
         for(int y =70 ; y <=500; y +=25 ){
         g2d.drawLine(20, y, 1020, y); // X axis
         }
         g2d.setColor(GRAPH_COLOR);

         int x = 20;

         int diff = 5;
                    try{    
                        FileInputStream fstream = new FileInputStream("data.txt");
                        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

                        String strLine;

                //Read File Line By Line
                int counter = 0;
                int prevPoint = 0;
                        while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                        //x +=5

                        if( counter == 0){
                        System.out.println (strLine);
                        //g2d.drawLine(x, 500-312, x +=15, Integer.parseInt(strLine));
                        }
                        counter ++;
                        g2d.drawLine(x , 500-prevPoint, x +=5, 500-Integer.parseInt(strLine));

                        prevPoint =  Integer.parseInt(strLine);

                }
                //Close the input stream
                        br.close();
                        }
                        catch(Exception ec){}




    }



    public void paint(Graphics g) {
        super.paint(g);
        drawLines(g);
    }
}

The value of the text file is something like below,

360 334 327 313 312 302 301 294 292 289 283 287 278 282 275 277 274 273 273 268 273 266 273 266 270 267 267 ....

Thanks in Advance. Anupam.

Replace

f.add(new MyPanel());

By

JScrollPane scroll = new JScrollPane(
        new MyPanel(), 
        ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
);
f.add(scroll);

And replace this

public Dimension getPreferredSize() {
    return new Dimension(250,200);
}

By this

public Dimension getPreferredSize() {
    return new Dimension(1020,500);
}

Tadaaa! 在此处输入图片说明

  1. This stuff, parsing the data, should be in the constructor:

     try{ FileInputStream fstream = new FileInputStream("data.txt"); // ... etc. 
  2. The values for the graph should be stored in an array or collection that is declared as an attribute of the class.
  3. Stored as a class attribute, the data can be used when calculating the preferred size, or painting.

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