我的JPanel应该包含300行文本,但无论我做什么来扩大它的大小,它都不会包含所有300行文本,为什么以及如何修复?这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class My_Chart_Panel extends JPanel
{
String Label_Text=" -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10";
Vector<String> String_Vector=new Vector();
Font Times_New_Roman_15_Font=new Font("Times New Roman",0,15);
My_Chart_Panel()
{
for (int i=0;i<300;i++) String_Vector.add("Line : "+(i+1));
JScrollPane Chart_ScrollPane=new JScrollPane(new A_Chart_Panel(String_Vector));
Chart_ScrollPane.setPreferredSize(new Dimension(536,770));
Chart_ScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Chart_ScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel Top_Panel=new JPanel();
JLabel Top_Label=new JLabel(Label_Text);
Top_Label.setForeground(new Color(0,50,160));
Top_Label.setFont(Times_New_Roman_15_Font);
Top_Panel.add(Top_Label);
add("North",Top_Panel);
add("Center",Chart_ScrollPane);
JPanel Bottom_Panel=new JPanel();
JLabel Bottom_Label=new JLabel(Label_Text);
Bottom_Label.setForeground(new Color(0,50,160));
Bottom_Label.setFont(Times_New_Roman_15_Font);
Bottom_Panel.add(Bottom_Label);
add("South",Bottom_Panel);
setPreferredSize(new Dimension(536,900));
}
public static void main(String[] args)
{
JFrame f=new JFrame("My_Chart_Panel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new My_Chart_Panel());
f.setSize(560,910);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class A_Chart_Panel extends JPanel
{
Vector<String> String_Vector;
final int PAD=2;
A_Chart_Panel(Vector<String> String_Vector)
{
this.String_Vector=String_Vector;
setPreferredSize(new Dimension(530,String_Vector.size()*8));
Draw_Chart();
}
void Draw_Chart()
{
Out("String_Vector.size() = "+String_Vector.size());
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w=getWidth();
int h=getHeight();
g2.drawLine(PAD+w*9/15,PAD,PAD+w*9/15,h-PAD);
g2.drawLine(PAD+w*9/15,PAD,PAD+w*9/15-5,PAD+26);
g2.drawLine(PAD+w*9/15,PAD,PAD+w*9/15+5,PAD+26);
double xScale=(w-2*PAD)/(20);
double maxValue=100.0;
double yScale =(h-2*PAD)/maxValue;
// The origin location.
int x0=PAD;
int y0=h-PAD;
g2.setPaint(Color.BLUE);
for (int j=0;j<String_Vector.size();j++)
{
int x=x0+(int)(xScale*(j+1));
int y=20+(int)(yScale*j);
g2.drawString(String_Vector.elementAt(j),5,y);
}
}
private static void out(String message) { System.out.print(message); }
private static void Out(String message) { System.out.println(message); }
}
答案 0 :(得分:0)
我明白了,这是答案:
class A_Chart_Panel extends JPanel
{
Vector<String> String_Vector;
final int PAD=2;
A_Chart_Panel(Vector<String> String_Vector)
{
this.String_Vector=String_Vector;
setPreferredSize(new Dimension(530,String_Vector.size()*12));
Draw_Chart();
}
void Draw_Chart()
{
Out("String_Vector.size() = "+String_Vector.size());
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w=getWidth();
int h=getHeight();
g2.drawLine(PAD+w*9/15,PAD,PAD+w*9/15,h-PAD);
g2.drawLine(PAD+w*9/15,PAD,PAD+w*9/15-5,PAD+26);
g2.drawLine(PAD+w*9/15,PAD,PAD+w*9/15+5,PAD+26);
double xScale=(w-2*PAD)/(20);
double maxValue=String_Vector.size();
double yScale =(h-2*PAD)/maxValue;
// The origin location.
int x0=PAD;
int y0=h-PAD;
g2.setPaint(Color.BLUE);
for (int j=0;j<String_Vector.size();j++)
{
int x=x0+(int)(xScale*(j+1));
int y=10+(int)(yScale*j);
g2.drawString(String_Vector.elementAt(j),5,y);
}
}