gpt4 book ai didi

java - Swing JFrame 水平滚动条

转载 作者:行者123 更新时间:2023-12-01 18:04:24 26 4
gpt4 key购买 nike

我正在尝试使用我刚接触的 java swing 制作一个图表。在我的程序中,我从文本文件中获取一些随机数,根据这些随机数创建图表。我已经能够制作图表,但我需要添加一个滚动条才能查看完整的图表,该图表的长度足以适合生成的窗口。任何人都可以修改或建议我一个非常简单的所以

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

文本文件的值如下所示,

360第334章第327章313第312章第302章301294第292章第289章第283章第287章278第282章275第277章274273273268273266273266270第267章第267章....

提前致谢。阿努帕姆。

最佳答案

替换

f.add(new MyPanel());

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

并替换这个

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

按此

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

哒哒哒! enter image description here

关于java - Swing JFrame 水平滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37857167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com