Just nu i M3-nätverket
Jump to content

Scroll försvinner


Posidon_2

Recommended Posts

Hej!

 

Jag har ett problem med att visa hela innehållet som ligger i en panel. Trots att jag lagt till scroll så kan jag inte visa hela innehållet. Scrollen försvinner när jag förstorat fönstret lite.

Har försökt lösa problemet med hjälp av diverse förslag på nätet men inte lyckats. Kan någon snäll själ hjälpa mig med detta?

 

 

import javax.swing.*;


public class ShowQuote {
public static void main (String[] args)
{
	//Skapa en ram
	MySecondFrame frame = new MySecondFrame ("Show quote");

	//Panel
	DrawPanel panel = new DrawPanel ("Den som dömer en person låser in personen i sitt förflutna, " +
										"den som förlåter öppnar dörrarna för framtiden,",
										"Arial",
										40,
										0,
										"MAGENTA",
										"BLUE",
										3

										);




	JScrollPane 	scrollPane =	new JScrollPane(panel/*, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
													JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS*/); 

	  scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
	  scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


	frame.add(scrollPane);


	frame.setSize(180, 180);

	//Visa ramen
	frame.setVisible (true);
}




}

 

 

import    java.awt.*;         // Graphics, Graphics2D, Color, Font, BasicStroke, Shape
import    java.awt.geom.*;    // AfflineTransform
import    java.awt.font.*;    // FontRenderContext, TextLayout
import    javax.swing.*;      // JPanel, JFrame





public class DrawPanel extends JPanel {

//Fördefinierade variabler
private String 		quote 		= "Any fool can write code that a computer can understand " +
									"Good programmers write code that humans can understand.";
private String 		font 		= "Times New Roman";
private int 		fontSize 	= 60;
private int			fontStyle 	= 1;
private String		stringColor = "RED";
private String		borderColor = "BLUE";
private int			borderSize 	= 2;

public DrawPanel ()
{

}

public DrawPanel (String quote, String font, int fontSize, int fontStyle, 
					String stringColor, String borderColor, int borderSize)
{
	this.quote 			= quote;
	this.font			= font;
	this.fontSize 		= fontSize;
	this.fontStyle 		= fontStyle;
	this.stringColor 	= stringColor;
	this.borderColor	= borderColor;
	this.borderSize		= borderSize;

}

public void paintComponent (Graphics gr)
{
	//Förberedande handlingar
	super.paintComponent (gr);
	this.setBackground (Color.red);
	Graphics2D		g = (Graphics2D) gr;

	//Rita en string

	Font fontName = new Font (font, Font.PLAIN, fontSize);
	if (fontStyle == 1)
		fontName = new Font (font, Font.BOLD, fontSize);
	else if (fontStyle == 2)
		fontName = new Font (font, Font.ITALIC, fontSize);
	else if ( fontStyle == 3)
		fontName = new Font (font, Font.ITALIC + Font.BOLD, fontSize);
	g.setFont (fontName);

	//Skapa en figur utifrån en teckensträng
	FontRenderContext		context = g.getFontRenderContext();

	//Representerar teckensträngen som ett grafiskt objekt
	TextLayout		layout = new TextLayout (quote , fontName, context);

	//Detta argument preciserar den returnerade figurens position
	AffineTransform		transform = AffineTransform.getTranslateInstance (30, 250);

	//Shape representerar den givna teckensträngen och objektet placeras vid punkten (30, 250)
	Shape		figure = layout.getOutline(transform);

	this.setPreferredSize(new Dimension(320, 370));

	g.setStroke (new BasicStroke (borderSize));
	if (stringColor == "RED")
		g.setPaint (Color.RED);
	else if (stringColor == "BLUE")
		g.setPaint (Color.BLUE);
	else if (stringColor == "GREEN")
		g.setPaint (Color.GREEN);
	else if (stringColor == "WHITE")
		g.setPaint (Color.WHITE);
	else if (stringColor == "MAGENTA")
		g.setPaint (Color.MAGENTA);
	else if (stringColor == "YELLOW")
		g.setPaint (Color.YELLOW);
	g.fill (figure);

       if (borderColor == "RED")
		g.setPaint (Color.RED);
       else if (borderColor == "BLUE")
		g.setPaint (Color.BLUE);
	else if (borderColor == "GREEN")
		g.setPaint (Color.GREEN);
	else if (borderColor == "WHITE")
		g.setPaint (Color.WHITE);
	else if (borderColor == "BLACK")
		g.setPaint (Color.BLACK);
       else if (borderColor == "LIGHT_GRAY")
		g.setPaint (Color.LIGHT_GRAY);
      // this.revalidate();
     // this.repaint();
       g.draw (figure);
       }
}

 


import java.awt.*;
import javax.swing.*;

public class MySecondFrame extends JFrame
{


public MySecondFrame (String titel)
{
	//anropar konstruktorn för JFrame
			super(titel);


			Toolkit		tk = Toolkit.getDefaultToolkit ();
			Image		picture = tk.getImage ("bild.jpg");
			this.setIconImage (picture);
			this.setLocationRelativeTo(null);
			this.setDefaultCloseOperation (EXIT_ON_CLOSE);




			Dimension		d = tk.getScreenSize ();
			int		screenWidth = d.width;
			int		screenHeight = d.height;


			int		frameWidth = 5 * screenWidth / 2;
			int		frameHeight = 4 * screenHeight / 2;
			this.setSize (frameWidth, frameHeight);	

}

}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...