Swing JScrollPane

Phyrra

Mitglied
Hallo,

ich habe ein JFrame, in welches ich ein JPanel (genannt mainPanel) setze, dieses mainPanel ist mit einem GridBagLayout versehen. Ich erstelle dann nach und nach neue Komponenten und füge diese mittels GridBagConstraints in das mainPanel ein.

Ich wollte einen Changelog machen, in welchen ich stets neuen Text einfüge. Ich dachte mir, ich mache das mit einem JTextPane, dessen ContentType ich auf "text/html" setze. Damit das scrollbar ist, habe ich davon ein JScrollPane erstellt. Dieses JScrollPane hänge ich dann ins mainPanel.

Das Problem dabei ist nun, dass, sobald der Text zu lange wird (also die Scrollbar tatsächlich auftritt) macht es mir das ganze Layout kaputt. Ich weiss einfach nicht woran es liegt.

Zur weiteren Erklärung hier ein Bild: (Ich bin mit der Forenregelung bzgl. Bilder nicht vertraut.. falls es zu gross ist kann ich es ändern.)
[Edit von L-ectron-X: Bild entfernt, hochgeladen und am Ende des Beitrages angehängt]

Zusätzlich noch die wichtigen Ausschnitte des Quellcodes:
[JAVA=42]private void initGUI(){
/*
* hier werden ein paar Elemente geladen
*/

chgLog=new JTextPane();
chgLog.setContentType("text/html");
chgLog.setEditable(false);
chgLogScroll=new JScrollPane(chgLog);
chgLog.addText("viele zeilenumbrüche")
//addText ist eine eigene Methode, fügt Text in die html Struktur ein
}

private void setGUI(){
GridBagConstraints c=new GridBagConstraints();

JPanel mainPanel=new JPanel(new GridBagLayout());
this.add(mainPanel);

//LINKE SEITE

c=new GridBagConstraints();
c.gridx=0; c.gridy=0; c.gridwidth=2;
mainPanel.add(mTopMenu, c);

c=new GridBagConstraints();
c.gridx=2; c.gridy=0; c.fill=GridBagConstraints.BOTH;
mainPanel.add(ctrlActionTxt,c);

c=new GridBagConstraints();
c.gridx=0; c.gridy=1; c.gridwidth=3; c.gridheight=10; c.fill=GridBagConstraints.BOTH;
mainPanel.add(drawArea,c);
drawArea.createBufferStrategy(2);
bufferStrategy=drawArea.getBufferStrategy();

c=new GridBagConstraints();
c.gridx=0; c.gridy=11;
c.insets=new Insets(10,0,0,0);
mainPanel.add(new JLabel("File Name: "),c);

c=new GridBagConstraints();
c.gridx=1; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(10,0,0,0);
mainPanel.add(fileName,c);

//RECHTE SEITE

c=new GridBagConstraints();
c.gridx=3; c.gridy=1; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
c.insets=new Insets(0,10,0,0);
mainPanel.add(bMoveable,c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=2; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
c.insets=new Insets(30,10,0,0);
mainPanel.add(new JLabel("Density:"),c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=3; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(10,10,10,10);
mainPanel.add(sDensity,c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=4; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(20,10,10,0);
mainPanel.add(new JLabel("Position:"),c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=5;
c.insets=new Insets(0,10,0,10);
mainPanel.add(new JLabel("X:"),c);

c=new GridBagConstraints();
c.gridx=4; c.gridy=5; c.fill=GridBagConstraints.HORIZONTAL;
mainPanel.add(tPosX,c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=6;
c.insets=new Insets(0,10,0,10);
mainPanel.add(new JLabel("Y:"),c);

c=new GridBagConstraints();
c.gridx=4; c.gridy=6; c.fill=GridBagConstraints.HORIZONTAL;
mainPanel.add(tPosY,c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=7; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(10,10,10,0);
mainPanel.add(new JLabel("Dimension:"),c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=8;
c.insets=new Insets(0,10,0,10);
mainPanel.add(new JLabel("X:"),c);

c=new GridBagConstraints();
c.gridx=4; c.gridy=8; c.fill=GridBagConstraints.HORIZONTAL;
mainPanel.add(tDimX,c);

c=new GridBagConstraints();
c.gridx=3; c.gridy=9;
c.insets=new Insets(0,10,0,10);
mainPanel.add(new JLabel("Y:"),c);

c=new GridBagConstraints();
c.gridx=4; c.gridy=9; c.fill=GridBagConstraints.HORIZONTAL;
mainPanel.add(tDimY,c);

/*
* HIER IST DAS SCROLLPANE
*/
c=new GridBagConstraints();
c.gridx=3; c.gridy=10; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
c.insets=new Insets(30,10,0,0);
mainPanel.add(chgLogScroll,c);
/*
*
*/

c=new GridBagConstraints();
c.gridx=3; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(8,10,0,0);
mainPanel.add(sGrid,c);
}[/code]

Das Setzen von Maximum und Minimum Size hat .. überhaupt nichts gebracht, dieser Befehl scheint mir völlig unnütz zu sein, da er ignoriert zu werden scheint.

Ein weiteres Problem ist, dass sich das Textfeld einfach in die Breite ausdehnt, statt auch dort einen Scrollbalken zu erstellen, bis es auch da das ganze Layout zerstört.

Ich hoffe jemand weiss Rat, ich hätte gerne einen vollständigen Changelog und möchte nicht, dass ich stets "oben wegschneiden" muss.
 

Anhänge

  • file.jpg
    file.jpg
    29,4 KB · Aufrufe: 27
Zuletzt bearbeitet von einem Moderator:

L-ectron-X

Gesperrter Benutzer
Code:
fill
und
Code:
anchor
werden keine Wirkung zeigen, wenn du keine Gewichtung (
Code:
weightx
und/oder
Code:
weighty
) benutzt.

Außerdem lässt sich das alles schlecht ausprobieren, da du nur einen Teil der Klasse gepostet hast.
 

Phyrra

Mitglied
Hallo,

die c.fill Teile bewirken schon etwas. Ohne diese, wäre ein JLabel nur so breit wie die Schrift darin, mit dem fill wird es auf die Breite der Zelle ausgedehnt.
Wegen dem Ausprobieren, ich bin mir nicht sicher wie gern es gesehen wird hier ganze Codes zu posten, allerdings kann ich das gerne machen. Ich habe alles rausgenommen, was an Funktionalität auf andere Klassen zugreifen würde, es bleibt die "nackte" Hauptklasse übrig.
Der Fehler lässt sich reproduzieren indem man zum Beispiel auf [New Obj] und dann [Rect] drückt, das ganze Layout wird dann verzerrt, weil das TextPanel seine Grösse ändert und das irgendwie auf alle anderen rückwirkt.

Java:
package myGui;

import java.util.Vector;

//import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

//import javax.swing.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

//import java.awt.*;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

//import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.ActionListener;

import java.awt.image.BufferStrategy;

public class CopyOfEditor extends JFrame implements ActionListener, ItemListener, ChangeListener, Runnable, MouseListener, MouseMotionListener{
	
	/*
	 * Area of GUI-Elements
	 */
	
	private static Thread fThread;
	
	private Canvas drawArea;
	private BufferStrategy bufferStrategy;
	
	private JMenuBar mTopMenu;
		private JMenu mFile;
			private JMenuItem mSave;
			private JMenuItem mOpen;
			private JMenuItem mDiscard;
		private JMenu mNew;
			private JMenuItem mRect;
			private JMenuItem mBox;
			private JMenuItem mBarrel;
			private JMenuItem mPoly;
		private JMenu mAction;
			private JMenuItem mMove;
			private JMenuItem mScale;
			private JMenuItem mTurn;
			private JMenuItem mDelete;
			
	private JTextField fileName;
	private JLabel ctrlActionTxt;
	
	private JButton bMoveable;
	private JSlider sDensity;
	private JCheckBox sGrid;
	
	private JTextField tPosX;
	private JTextField tPosY;
	private JTextField tDimX;
	private JTextField tDimY;
	
	private JTextPane chgLog;
	private JScrollPane chgLogScroll;
	
	/*
	 * 
	 */
	
	/*
	 * Area of Control Elements
	 */
	
	public boolean mouseDown;
	public int mouseX, mouseY, mouseXSpeed, mouseYSpeed;
	
	private enum CtrlAction{
		MOVE,
		SCALE,
		TURN,
		DELETE
	}
	CtrlAction ctrlAction;
    
    int scrollX, scrollY;	
	/*
	 * 
	 */
    
    /*
     * Area of Utility Elements
     */
	
    private long ftime=System.currentTimeMillis();
    private float ftimer=0;    
    /*
     * 
     */
	
	/*
	 * Area of Initialisation
	 */
	
	public static void main(String[] args){
		CopyOfEditor frame = new CopyOfEditor();
		fThread = new Thread(frame);
		fThread.start();
	}
	
	public CopyOfEditor(){
		super("Editor");
		setSize(800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setVisible(true);
		
		initGUI();
		setGUI();
	}
	
	private void initGUI(){
		drawArea=new Canvas();
		drawArea.setSize(500,500); drawArea.setBackground(new Color(255,255,255));
		drawArea.addMouseListener(this); drawArea.addMouseMotionListener(this);
		
		mTopMenu=new JMenuBar();
		mFile=new JMenu("File"); mTopMenu.add(mFile);
			mSave=new JMenuItem("Save"); mFile.add(mSave); mSave.addActionListener(this);
			mOpen=new JMenuItem("Open"); mFile.add(mOpen); mOpen.addActionListener(this);
			mDiscard=new JMenuItem("Discard"); mFile.add(mDiscard); mDiscard.addActionListener(this);
		mNew=new JMenu("New Obj"); mTopMenu.add(mNew);
			mRect=new JMenuItem("Rect"); mNew.add(mRect); mRect.addActionListener(this);
			mBox=new JMenuItem("Box"); mNew.add(mBox); mBox.addActionListener(this);
			mBarrel=new JMenuItem("Barrel"); mNew.add(mBarrel); mBarrel.addActionListener(this);
			mPoly=new JMenuItem("Polygon"); mNew.add(mPoly); mPoly.addActionListener(this);
		mAction=new JMenu("Action"); mTopMenu.add(mAction);
			mMove=new JMenuItem("Move"); mAction.add(mMove); mMove.addActionListener(this);
			mScale=new JMenuItem("Scale"); mAction.add(mScale); mScale.addActionListener(this);
			mTurn=new JMenuItem("Turn"); mAction.add(mTurn); mTurn.addActionListener(this);
			mDelete=new JMenuItem("Delete"); mAction.add(mDelete); mDelete.addActionListener(this);
			
		fileName=new JTextField();
		ctrlActionTxt=new JLabel("   [ Move ]"); ctrlAction=CtrlAction.MOVE;
		
		bMoveable=new JButton("..."); bMoveable.addActionListener(this);
		sDensity=new JSlider(0,100); sDensity.addChangeListener(this);
			sDensity.setPaintTicks(true);
			sDensity.setMajorTickSpacing(25);
			sDensity.setMinorTickSpacing(5);
		sGrid=new JCheckBox("Draw Grid"); sGrid.addActionListener(this);
		
		tPosX=new JTextField(16); tPosX.addActionListener(this);
		tPosY=new JTextField(16); tPosY.addActionListener(this);
		tDimX=new JTextField(16); tDimX.addActionListener(this);
		tDimY=new JTextField(16); tDimY.addActionListener(this);
		
		chgLog=new JTextPane();
			chgLog.setContentType("text/html");
			chgLog.setEditable(false);
		chgLogScroll=new JScrollPane(chgLog);
			chgLog.setText(chgLog.getText().substring(0,findSubstring(chgLog.getText(),"</p>"))+"New Session Started<br><br><br><br><br><br><br><br>"+chgLog.getText().substring(findSubstring(chgLog.getText(),"</p>"),chgLog.getText().length()));
	}
	
	private void setGUI(){
		GridBagConstraints c=new GridBagConstraints();
		
		JPanel mainPanel=new JPanel(new GridBagLayout());
		this.add(mainPanel);
		
		c=new GridBagConstraints();
		c.gridx=0; c.gridy=0; c.gridwidth=2;
		mainPanel.add(mTopMenu, c);
		
		c=new GridBagConstraints();
		c.gridx=2; c.gridy=0; c.fill=GridBagConstraints.BOTH;
		mainPanel.add(ctrlActionTxt,c);
		
		c=new GridBagConstraints();
		c.gridx=0; c.gridy=1; c.gridwidth=3; c.gridheight=10; c.fill=GridBagConstraints.BOTH;
		mainPanel.add(drawArea,c);
		drawArea.createBufferStrategy(2);
		bufferStrategy=drawArea.getBufferStrategy();
		
		c=new GridBagConstraints();
		c.gridx=0; c.gridy=11;
		c.insets=new Insets(10,0,0,0);
		mainPanel.add(new JLabel("File Name: "),c);
		
		c=new GridBagConstraints();
		c.gridx=1; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
		c.insets=new Insets(10,0,0,0);
		mainPanel.add(fileName,c);
		
		/*
		 * 
		 */
			
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=1; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
		c.insets=new Insets(0,10,0,0);
		mainPanel.add(bMoveable,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=2; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
		c.insets=new Insets(30,10,0,0);
		mainPanel.add(new JLabel("Density:"),c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=3; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
		c.insets=new Insets(10,10,10,10);
		mainPanel.add(sDensity,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=4; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
		c.insets=new Insets(20,10,10,0);
		mainPanel.add(new JLabel("Position:"),c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=5;
		c.insets=new Insets(0,10,0,10);
		mainPanel.add(new JLabel("X:"),c);
		
		c=new GridBagConstraints();
		c.gridx=4; c.gridy=5; c.fill=GridBagConstraints.HORIZONTAL;
		mainPanel.add(tPosX,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=6;
		c.insets=new Insets(0,10,0,10);
		mainPanel.add(new JLabel("Y:"),c);
		
		c=new GridBagConstraints();
		c.gridx=4; c.gridy=6; c.fill=GridBagConstraints.HORIZONTAL;
		mainPanel.add(tPosY,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=7; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
		c.insets=new Insets(10,10,10,0);
		mainPanel.add(new JLabel("Dimension:"),c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=8;
		c.insets=new Insets(0,10,0,10);
		mainPanel.add(new JLabel("X:"),c);
		
		c=new GridBagConstraints();
		c.gridx=4; c.gridy=8; c.fill=GridBagConstraints.HORIZONTAL;
		mainPanel.add(tDimX,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=9;
		c.insets=new Insets(0,10,0,10);
		mainPanel.add(new JLabel("Y:"),c);
		
		c=new GridBagConstraints();
		c.gridx=4; c.gridy=9; c.fill=GridBagConstraints.HORIZONTAL;
		mainPanel.add(tDimY,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=10; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
		c.insets=new Insets(30,10,0,0);
		mainPanel.add(chgLogScroll,c);
		
		c=new GridBagConstraints();
		c.gridx=3; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
		c.insets=new Insets(8,10,0,0);
		mainPanel.add(sGrid,c);
	}
	
	/*
	 * 
	 */
	
	/*
	 * Area of Control
	 */
	
	public void mousePressed(MouseEvent e){
		mouseDown=true;
		
		setVisible(true);
	}
	public void mouseReleased(MouseEvent e){
		mouseDown=false;
		
		setVisible(true);
	}

	public void mouseMoved(MouseEvent e){
		mouseXSpeed=e.getX()-mouseX; mouseYSpeed=e.getY()-mouseY;
		mouseYSpeed=0;
		mouseX=e.getX(); mouseY=e.getY();
		
		setVisible(true);
	}
	public void mouseDragged(MouseEvent e){
		mouseXSpeed=e.getX()-mouseX; mouseYSpeed=e.getY()-mouseY;
		mouseX=e.getX(); mouseY=e.getY();
		
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e){		
		Object src=e.getSource();
		
		if(src==mRect){
			addToChgLog("Rect added");
		} else if(src==mPoly){
			addToChgLog("Polygon added");
		} else if(src==mBox){
			addToChgLog("Box added");
		} else if(src==mBarrel){
			addToChgLog("Barrel added");
			
		} else if(src==mMove){
			ctrlAction=CtrlAction.MOVE;
			ctrlActionTxt.setText("   [ Move ]");
			addToChgLog("Mode changed to [MOVE]");
		} else if(src==mScale){
			ctrlAction=CtrlAction.SCALE;
			ctrlActionTxt.setText("   [ Scale ]");
			addToChgLog("Mode changed to [SCALE]");
		} else if(src==mDelete){
			ctrlAction=CtrlAction.DELETE;
			ctrlActionTxt.setText("   [ Delete ]");
			addToChgLog("Mode changed to [DELETE]");
		} else if(src==mTurn){
			ctrlAction=CtrlAction.TURN;
			ctrlActionTxt.setText("   [ Turn ]");
			addToChgLog("Mode changed to [TURN]");
			
		} else if(src==bMoveable){
			addToChgLog("Moveability changed to [Unmoveable]");

		} else if(src==mSave){
			String path=fileName.getText();
			if(path.equals("")){
				path="Test.txt";
				fileName.setText(path);
			}

			addToChgLog("Saved to "+path);
		} else if(src==mOpen){
			setInactive();
			
			String path=fileName.getText();
			if(path.equals("")){
				path="Test.txt";
				fileName.setText(path);
			}

			addToChgLog("Loaded from "+path);
		} else if(src==mDiscard){
			setInactive();
			addToChgLog("Discarded file");
			
		} else if(src==tPosX){
			try{
				float oldX=0;
				float newX=Float.parseFloat(tPosX.getText());

				addToChgLog("Moved X from "+oldX+" to "+newX);
			}catch(NumberFormatException pxe){
				//ignore
			}
		} else if(src==tPosY){
			try{
				float oldY=0;
				float newY=Float.parseFloat(tPosY.getText());
			
				addToChgLog("Moved Y from "+oldY+" to "+newY);
			}catch(NumberFormatException pye){
				//ignore
			}
		} else if(src==tDimX){
			try{
				float oldX=0;
				float newX=Float.parseFloat(tDimX.getText());
				
				addToChgLog("Scaled X from "+oldX+" to "+newX);
			}catch(NumberFormatException dxe){
				//ignore
			}
		} else if(src==tDimY){
			try{
				float oldY=0;
				float newY=Float.parseFloat(tDimY.getText());
				
				addToChgLog("Scaled Y from "+oldY+" to "+newY);
			}catch(NumberFormatException dye){
				//ignore
			}
		}
	}
	
	public void itemStateChanged(ItemEvent e){
		
	}
	
	public void stateChanged(ChangeEvent e){
		Object src=e.getSource();
	}
	
	/*
	 * 
	 */
	
	/*
	 * Area of Work Stuff
	 */
    
	public void run(){
		Graphics g;
		ftimer=1000f/60f;
		
		int dragX=0, dragY=0;
		
		scrollX=250;
		scrollY=250;
		
		setVisible(true);
		
		while(true){
      	  	/*if(this.ftimer>0){
      	  		while(System.currentTimeMillis()-ftime<ftimer){ }
      	  		ftime=System.currentTimeMillis();
      	  	}*/			
      	  	try{
      	  		int suspTime=(int)(ftimer-(System.currentTimeMillis()-ftime));
      	  		if (suspTime>0)
      	  			fThread.sleep(suspTime);
      	  		ftime=System.currentTimeMillis();
      	  	}catch(InterruptedException e){
      	  		//ignore
      	  	}
      	  	
			try{
				if(!bufferStrategy.contentsLost())
					bufferStrategy.show();
				
				g=bufferStrategy.getDrawGraphics();
				draw((Graphics2D) g);
				g.dispose();
			}catch(NullPointerException e){
				//ignore
			}
			
			if(inRect(mouseX,mouseY,475,210,25,80))
				scrollX--;
			if(inRect(mouseX,mouseY,0,210,25,80))
				scrollX++;
			if(inRect(mouseX,mouseY,210,0,80,25))
				scrollY++;
			if(inRect(mouseX,mouseY,210,475,80,25))
				scrollY--;
			
			mouseXSpeed=0;
			mouseYSpeed=0;
		}
	}
	
	private void setActive(){		
		tPosX.setText("0");
		tPosY.setText("0");
		
		tDimX.setText("0");
		tDimY.setText("0");
		
		sDensity.setValue(50);
	}
	
	public void setInactive(){
		bMoveable.setText("...");
		
		sDensity.setValue(50);
		
		tPosX.setText("");
		tPosY.setText("");
		tDimX.setText("");
		tDimY.setText("");
	}
	
	public void draw(Graphics2D g){
		g.setColor(g.getBackground());
		g.fillRect(0,0,500,500);
		
		g.setColor(new Color(175,175,175));
		g.drawLine(scrollX-5,scrollY-5,scrollX+5,scrollY+5);
		g.drawLine(scrollX+5,scrollY-5,scrollX-5,scrollY+5);
		
		int gridSize=25;
		if(sGrid.isSelected())
			for(int i=0; i<500+gridSize; i+=gridSize){
				g.drawLine(i+(scrollX % gridSize), 0, i+(scrollX % gridSize), 500);
				g.drawLine(0, i+(scrollY % gridSize), 500, i+(scrollY % gridSize));
			}
	}
	
	private void addToChgLog(String newText){
		String oldText=chgLog.getText();
		
		String part1, part2;
		int pos;
		
		pos=findSubstring(oldText,"</p>");
		
		part1=oldText.substring(0,pos);
		part2=oldText.substring(pos,oldText.length());
		
		chgLog.setText(part1+"<br>"+newText+part2);
	}
	
	public int findSubstring(String src, String part){
		boolean found;
		
		for(int i=0; i<src.length()-part.length(); i++){
			found=true;
			for(int j=0; j<part.length(); j++)
				if(src.charAt(i+j)!=part.charAt(j))
					found=false;
			
			if(found)
				return i;
		}
		
		return -1;
	}
	
	/*
	 * 
	 */
	
	/*
	 * Area of Creation Stuff
	 */
	
	public boolean inRect(int x, int y, int x1, int y1, int b, int h){
		if(x>x1 && x<x1+b && y>y1 && y<y1+h)
			return true;
		return false;
	}
	
	/*
	 * 
	 */
	
	/*
	 * Area of Useless Stuff
	 */
	
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	
	/*
	 * 
	 */
}

Ich habe vorhin ausprobiert ob sich mit dem Setzen der Gewichte (c.weightx, c.weighty) etwas ändert. Das Fenster wird dann zur vollen Breite ausgenutzt, aber es verzieht sich immernoch alles, wenn ich dann versuche neuen Text in das TextPanel einzufügen.

Ich hoffe mit dem obigen Code ist das Problem klar und eine Lösung in Sicht.

MfG,
Phyrra

EDIT:

Zur besseren Übersicht, und als Versuch wegen der weightx und weighty Werte hier nochmal mit einer Unterteilung des mainPanel in "unter Panel", die dann mit Komponenten gefüllt werden.

Java:
	private void setGUI(){
		GridBagConstraints c=new GridBagConstraints();
		
		JPanel mainPanel=new JPanel(new GridBagLayout());
		this.add(mainPanel);
		
		JPanel topLeftPan=new JPanel(new GridBagLayout());
		c=new GridBagConstraints();
		c.gridx=0; c.gridy=0;
		c.weightx=0.625; c.weighty=0.9;
		c.gridheight=2;
		c.fill=GridBagConstraints.BOTH;
		mainPanel.add(topLeftPan, c);
		
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=0;
			c.weightx=0.20; c.weighty=0.1;
			c.insets=new Insets(10,10,0,0);
			c.fill=GridBagConstraints.HORIZONTAL;
			topLeftPan.add(mTopMenu, c);
		
			c=new GridBagConstraints();
			c.gridx=1; c.gridy=0;
			c.weightx=0.425; c.weighty=0.1;
			c.insets=new Insets(10,0,0,0);
			c.fill=GridBagConstraints.HORIZONTAL;
			topLeftPan.add(ctrlActionTxt, c);
		
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=1;
			c.gridwidth=2; c.gridheight=1;
			c.weightx=0.625; c.weighty=0.8;
			c.insets=new Insets(0,10,0,0);
			topLeftPan.add(drawArea, c);
			drawArea.createBufferStrategy(2);
			bufferStrategy=drawArea.getBufferStrategy();
		
		JPanel bottomLeftPan=new JPanel(new GridBagLayout());
		c=new GridBagConstraints();
		c.gridx=0; c.gridy=2;
		c.weightx=0.625; c.weighty=0.1;
		c.fill=GridBagConstraints.BOTH;
		mainPanel.add(bottomLeftPan, c);
		
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=0;
			c.weightx=0; c.weighty=0.1;
			c.insets=new Insets(5,10,5,0);
			c.fill=GridBagConstraints.HORIZONTAL;
			bottomLeftPan.add(new JLabel("Filename:"), c);
		
			c=new GridBagConstraints();
			c.gridx=1; c.gridy=0;
			c.weightx=1; c.weighty=0.1;
			c.insets=new Insets(5,10,5,0);
			c.fill=GridBagConstraints.HORIZONTAL;
			bottomLeftPan.add(fileName, c);
			
		JPanel topRightPan=new JPanel(new GridBagLayout());
		c=new GridBagConstraints();
		c.gridx=1; c.gridy=0;
		c.weightx=0.375; c.weighty=0.6;
		c.fill=GridBagConstraints.BOTH;
		mainPanel.add(topRightPan, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=0;
			c.weightx=1; c.weighty=0.1;
			c.gridwidth=2;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(bMoveable, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=1;
			c.weightx=1; c.weighty=0.1;
			c.gridwidth=2;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(new JLabel("Density:"), c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=2;
			c.weightx=1; c.weighty=0.1;
			c.gridwidth=2;
			c.insets=new Insets(5,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(sDensity, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=3;
			c.weightx=1; c.weighty=0.1;
			c.gridwidth=2;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(new JLabel("Position:"), c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=4;
			c.weightx=0; c.weighty=0.1;
			c.insets=new Insets(10,10,0,0);
			topRightPan.add(new JLabel("X:"), c);
			
			c=new GridBagConstraints();
			c.gridx=1; c.gridy=4;
			c.weightx=1; c.weighty=0.1;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(tPosX, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=5;
			c.weightx=0; c.weighty=0.1;
			c.insets=new Insets(10,10,0,0);
			topRightPan.add(new JLabel("Y:"), c);
			
			c=new GridBagConstraints();
			c.gridx=1; c.gridy=5;
			c.weightx=1; c.weighty=0.1;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(tPosY, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=6;
			c.weightx=1; c.weighty=0.1;
			c.gridwidth=2;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(new JLabel("Dimension:"), c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=7;
			c.weightx=0; c.weighty=0.1;
			c.insets=new Insets(10,10,0,0);
			topRightPan.add(new JLabel("X:"), c);
			
			c=new GridBagConstraints();
			c.gridx=1; c.gridy=7;
			c.weightx=1; c.weighty=0.1;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(tDimX, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=8;
			c.weightx=0; c.weighty=0.1;
			c.insets=new Insets(10,10,0,0);
			topRightPan.add(new JLabel("Y:"), c);
			
			c=new GridBagConstraints();
			c.gridx=1; c.gridy=8;
			c.weightx=1; c.weighty=0.1;
			c.insets=new Insets(10,10,0,10);
			c.fill=GridBagConstraints.BOTH;
			topRightPan.add(tDimY, c);
			
		JPanel middleRightPan=new JPanel(new GridBagLayout());
		c=new GridBagConstraints();
		c.gridx=1; c.gridy=1;
		c.weightx=0.375; c.weighty=0.4;
		c.fill=GridBagConstraints.BOTH;
		mainPanel.add(middleRightPan, c);
			
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=0;
			c.weightx=1; c.weighty=1;
			c.insets=new Insets(20,10,10,10);
			c.fill=GridBagConstraints.BOTH;
			middleRightPan.add(chgLogScroll, c);
			
		JPanel bottomRightPan=new JPanel(new GridBagLayout());
		c=new GridBagConstraints();
		c.gridx=1; c.gridy=2;
		c.weightx=0.375; c.weighty=0.1;
		c.fill=GridBagConstraints.BOTH;
		mainPanel.add(bottomRightPan, c);
		
			c=new GridBagConstraints();
			c.gridx=0; c.gridy=0;
			c.weightx=1; c.weighty=1;
			c.insets=new Insets(10,10,10,10);
			c.fill=GridBagConstraints.BOTH;
			bottomRightPan.add(sGrid, c);
	}

Das Problem besteht immernoch. Gibt es bei der erstellung des Scrollbalkens irgend einen Resize-Befehl, der mir das ganze Layout zerwürfelt? Es ist relativ frustrierend, dass das dauernd geschieht.
Kann ich nicht irgend eine feste Grösse für das JTextPane/JScrollPane festsetzen? mit .setMaximumSize, .setMinimumSize, .setPreferredSize ist nix anzufangen, die Befehle werden scheinbar ignoriert.

MfG,
Phyrra
 
Zuletzt bearbeitet:

L-ectron-X

Gesperrter Benutzer
Ich habe den Code überflogen und den des Layouts mal so angepasst, wie du ihn wahrscheinlich haben wolltest. Die Änderungen habe ich kommentiert, so dass du erkennst, was ich gemacht habe.
Es kann durchaus sein, dass du einige Zeilen des GridBag-Gitters anpassen musst.
Und es sind noch einige unschöne Dinge in deinem Code, die du irgendwann mal verbessern solltest. Zum Beispiel solltest du nun statt im Canvas im von mir angelegten JPanel zeichnen. Dazu ist die paintComponent()-Methode zu überschreiben.
Java:
package myGui;

import java.util.Vector;
 
//import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.BorderFactory;
 
//import javax.swing.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
 
//import java.awt.*;
//import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Dimension;
 
//import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.ActionListener;
 
import java.awt.image.BufferStrategy;
 
public class CopyOfEditor extends JFrame implements ActionListener, ItemListener, ChangeListener, Runnable, MouseListener, MouseMotionListener{
    
    /*
     * Area of GUI-Elements
     */
    
    private static Thread fThread;
    
    //private Canvas drawArea;
    private JPanel drawArea;
    //private BufferStrategy bufferStrategy;
    
    private JMenuBar mTopMenu;
        private JMenu mFile;
            private JMenuItem mSave;
            private JMenuItem mOpen;
            private JMenuItem mDiscard;
        private JMenu mNew;
            private JMenuItem mRect;
            private JMenuItem mBox;
            private JMenuItem mBarrel;
            private JMenuItem mPoly;
        private JMenu mAction;
            private JMenuItem mMove;
            private JMenuItem mScale;
            private JMenuItem mTurn;
            private JMenuItem mDelete;
            
    private JTextField fileName;
    private JLabel ctrlActionTxt;
    
    private JButton bMoveable;
    private JSlider sDensity;
    private JCheckBox sGrid;
    
    private JTextField tPosX;
    private JTextField tPosY;
    private JTextField tDimX;
    private JTextField tDimY;
    
    private JTextPane chgLog;
    private JScrollPane chgLogScroll;
    
    /*
     * 
     */
    
    /*
     * Area of Control Elements
     */
    
    public boolean mouseDown;
    public int mouseX, mouseY, mouseXSpeed, mouseYSpeed;
    
    private enum CtrlAction{
        MOVE,
        SCALE,
        TURN,
        DELETE
    }
    CtrlAction ctrlAction;
    
    int scrollX, scrollY;   
    /*
     * 
     */
    
    /*
     * Area of Utility Elements
     */
    
    private long ftime=System.currentTimeMillis();
    private float ftimer=0;    
    /*
     * 
     */
    
    /*
     * Area of Initialisation
     */
    
    public static void main(String[] args){
        CopyOfEditor frame = new CopyOfEditor();
        fThread = new Thread(frame);
        fThread.start();
    }
    
    public CopyOfEditor(){
        super("Editor");
        //setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //setVisible(true);
        
        initGUI();
        setGUI();
        pack(); //statt setSize()
        setVisible(true);
    }
    
    private void initGUI(){
        //drawArea=new Canvas(); Swing und AWT nicht mischen!
	drawArea = new JPanel();
	drawArea.setPreferredSize(new Dimension(500, 500)); //gewünschte Größe festlegen
        /*drawArea.setSize(500,500);*/ drawArea.setBackground(new Color(255,255,255));
        drawArea.addMouseListener(this); drawArea.addMouseMotionListener(this);
        
        mTopMenu=new JMenuBar();
        mFile=new JMenu("File"); mTopMenu.add(mFile);
            mSave=new JMenuItem("Save"); mFile.add(mSave); mSave.addActionListener(this);
            mOpen=new JMenuItem("Open"); mFile.add(mOpen); mOpen.addActionListener(this);
            mDiscard=new JMenuItem("Discard"); mFile.add(mDiscard); mDiscard.addActionListener(this);
        mNew=new JMenu("New Obj"); mTopMenu.add(mNew);
            mRect=new JMenuItem("Rect"); mNew.add(mRect); mRect.addActionListener(this);
            mBox=new JMenuItem("Box"); mNew.add(mBox); mBox.addActionListener(this);
            mBarrel=new JMenuItem("Barrel"); mNew.add(mBarrel); mBarrel.addActionListener(this);
            mPoly=new JMenuItem("Polygon"); mNew.add(mPoly); mPoly.addActionListener(this);
        mAction=new JMenu("Action"); mTopMenu.add(mAction);
            mMove=new JMenuItem("Move"); mAction.add(mMove); mMove.addActionListener(this);
            mScale=new JMenuItem("Scale"); mAction.add(mScale); mScale.addActionListener(this);
            mTurn=new JMenuItem("Turn"); mAction.add(mTurn); mTurn.addActionListener(this);
            mDelete=new JMenuItem("Delete"); mAction.add(mDelete); mDelete.addActionListener(this);

        setJMenuBar(mTopMenu); //Zeile 200-202 auskommentiert!
            
        fileName=new JTextField();
        ctrlActionTxt=new JLabel("   [ Move ]"); ctrlAction=CtrlAction.MOVE;
        
        bMoveable=new JButton("..."); bMoveable.addActionListener(this);
        sDensity=new JSlider(0,100); sDensity.addChangeListener(this);
            sDensity.setPaintTicks(true);
            sDensity.setMajorTickSpacing(25);
            sDensity.setMinorTickSpacing(5);
        sGrid=new JCheckBox("Draw Grid"); sGrid.addActionListener(this);
        
        tPosX=new JTextField(16); tPosX.addActionListener(this);
        tPosY=new JTextField(16); tPosY.addActionListener(this);
        tDimX=new JTextField(16); tDimX.addActionListener(this);
        tDimY=new JTextField(16); tDimY.addActionListener(this);
        
        chgLog=new JTextPane();
        chgLog.setContentType("text/html");
        chgLog.setEditable(false);
        chgLogScroll=new JScrollPane(chgLog);
        chgLogScroll.setPreferredSize(new Dimension(200, 200)); //gewünschte Größe des Log
        chgLogScroll.setMinimumSize(new Dimension(200, 200)); //Mindestgröße des Log
        chgLog.setText(chgLog.getText().substring(0,findSubstring(chgLog.getText(),"</p>"))+"New Session Started<br><br><br><br><br><br><br><br>"+chgLog.getText().substring(findSubstring(chgLog.getText(),"</p>"),chgLog.getText().length()));
    }
    
    private void setGUI(){
        GridBagConstraints c;
        
        JPanel mainPanel=new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); //einen leeren 10-Pixel-Rahmen
        this.add(mainPanel);
        
        /*c=new GridBagConstraints();
        c.gridx=0; c.gridy=0; c.gridwidth=2;
        mainPanel.add(mTopMenu, c);*/ //dafür wird setJMenuBar() benutzt!
        
        c=new GridBagConstraints();
        c.gridx=2; c.gridy=0; c.fill=GridBagConstraints.BOTH;
        mainPanel.add(ctrlActionTxt,c);
        
        c=new GridBagConstraints();
        c.gridx=0; c.gridy=1; c.gridwidth=3; c.gridheight=10; c.fill=GridBagConstraints.BOTH;
	c.weightx = 1.0; c.weighty = 1.0; //Gewichtung
        mainPanel.add(drawArea,c);
        //drawArea.createBufferStrategy(2); //ein JPanel ist bereits gepuffert, Canvas ersetzt
        //bufferStrategy=drawArea.getBufferStrategy();
        
        c=new GridBagConstraints();
        c.gridx=0; c.gridy=11;
        c.insets=new Insets(10,0,0,0);
        mainPanel.add(new JLabel("File Name: "),c);
        
        c=new GridBagConstraints();
        c.gridx=1; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
        c.insets=new Insets(10,0,0,0);
        mainPanel.add(fileName,c);
        
        /*
         * 
         */
            
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=1; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
        c.insets=new Insets(0,10,0,0);
        mainPanel.add(bMoveable,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=2; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
        c.insets=new Insets(30,10,0,0);
        mainPanel.add(new JLabel("Density:"),c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=3; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
        c.insets=new Insets(10,10,10,10);
        mainPanel.add(sDensity,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=4; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
        c.insets=new Insets(20,10,10,0);
        mainPanel.add(new JLabel("Position:"),c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=5;
        c.insets=new Insets(0,10,0,10);
        mainPanel.add(new JLabel("X:"),c);
        
        c=new GridBagConstraints();
        c.gridx=4; c.gridy=5; c.fill=GridBagConstraints.HORIZONTAL;
        mainPanel.add(tPosX,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=6;
        c.insets=new Insets(0,10,0,10);
        mainPanel.add(new JLabel("Y:"),c);
        
        c=new GridBagConstraints();
        c.gridx=4; c.gridy=6; c.fill=GridBagConstraints.HORIZONTAL;
        mainPanel.add(tPosY,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=7; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
        c.insets=new Insets(10,10,10,0);
        mainPanel.add(new JLabel("Dimension:"),c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=8;
        c.insets=new Insets(0,10,0,10);
        mainPanel.add(new JLabel("X:"),c);
        
        c=new GridBagConstraints();
        c.gridx=4; c.gridy=8; c.fill=GridBagConstraints.HORIZONTAL;
        mainPanel.add(tDimX,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=9;
        c.insets=new Insets(0,10,0,10);
        mainPanel.add(new JLabel("Y:"),c);
        
        c=new GridBagConstraints();
        c.gridx=4; c.gridy=9; c.fill=GridBagConstraints.HORIZONTAL;
        mainPanel.add(tDimY,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=10; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;
        c.insets=new Insets(30,10,0,0);
        mainPanel.add(chgLogScroll,c);
        
        c=new GridBagConstraints();
        c.gridx=3; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;
        c.insets=new Insets(8,10,0,0);
        mainPanel.add(sGrid,c);
    }
    
    /*
     * 
     */
    
    /*
     * Area of Control
     */
    
    public void mousePressed(MouseEvent e){
        mouseDown=true;
        
        setVisible(true);
    }
    public void mouseReleased(MouseEvent e){
        mouseDown=false;
        
        setVisible(true);
    }
 
    public void mouseMoved(MouseEvent e){
        mouseXSpeed=e.getX()-mouseX; mouseYSpeed=e.getY()-mouseY;
        mouseYSpeed=0;
        mouseX=e.getX(); mouseY=e.getY();
        
        setVisible(true);
    }
    public void mouseDragged(MouseEvent e){
        mouseXSpeed=e.getX()-mouseX; mouseYSpeed=e.getY()-mouseY;
        mouseX=e.getX(); mouseY=e.getY();
        
        setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e){     
        Object src=e.getSource();
        
        if(src==mRect){
            addToChgLog("Rect added");
        } else if(src==mPoly){
            addToChgLog("Polygon added");
        } else if(src==mBox){
            addToChgLog("Box added");
        } else if(src==mBarrel){
            addToChgLog("Barrel added");
            
        } else if(src==mMove){
            ctrlAction=CtrlAction.MOVE;
            ctrlActionTxt.setText("   [ Move ]");
            addToChgLog("Mode changed to [MOVE]");
        } else if(src==mScale){
            ctrlAction=CtrlAction.SCALE;
            ctrlActionTxt.setText("   [ Scale ]");
            addToChgLog("Mode changed to [SCALE]");
        } else if(src==mDelete){
            ctrlAction=CtrlAction.DELETE;
            ctrlActionTxt.setText("   [ Delete ]");
            addToChgLog("Mode changed to [DELETE]");
        } else if(src==mTurn){
            ctrlAction=CtrlAction.TURN;
            ctrlActionTxt.setText("   [ Turn ]");
            addToChgLog("Mode changed to [TURN]");
            
        } else if(src==bMoveable){
            addToChgLog("Moveability changed to [Unmoveable]");
 
        } else if(src==mSave){
            String path=fileName.getText();
            if(path.equals("")){
                path="Test.txt";
                fileName.setText(path);
            }
 
            addToChgLog("Saved to "+path);
        } else if(src==mOpen){
            setInactive();
            
            String path=fileName.getText();
            if(path.equals("")){
                path="Test.txt";
                fileName.setText(path);
            }
 
            addToChgLog("Loaded from "+path);
        } else if(src==mDiscard){
            setInactive();
            addToChgLog("Discarded file");
            
        } else if(src==tPosX){
            try{
                float oldX=0;
                float newX=Float.parseFloat(tPosX.getText());
 
                addToChgLog("Moved X from "+oldX+" to "+newX);
            }catch(NumberFormatException pxe){
                pxe.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!
            }
        } else if(src==tPosY){
            try{
                float oldY=0;
                float newY=Float.parseFloat(tPosY.getText());
            
                addToChgLog("Moved Y from "+oldY+" to "+newY);
            }catch(NumberFormatException pye){
                pye.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!
            }
        } else if(src==tDimX){
            try{
                float oldX=0;
                float newX=Float.parseFloat(tDimX.getText());
                
                addToChgLog("Scaled X from "+oldX+" to "+newX);
            }catch(NumberFormatException dxe){
                dxe.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!
            }
        } else if(src==tDimY){
            try{
                float oldY=0;
                float newY=Float.parseFloat(tDimY.getText());
                
                addToChgLog("Scaled Y from "+oldY+" to "+newY);
            }catch(NumberFormatException dye){
                dye.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!
            }
        }
    }
    
    public void itemStateChanged(ItemEvent e){
        
    }
    
    public void stateChanged(ChangeEvent e){
        Object src=e.getSource();
    }
    
    /*
     * 
     */
    
    /*
     * Area of Work Stuff
     */
    
    public void run(){
        //Graphics g; //wird nicht benutzt und auch nicht benötigt
        ftimer=1000f/60f;
        
        int dragX=0, dragY=0;
        
        scrollX=250;
        scrollY=250;
        
        setVisible(true);
        
        while(true){
            /*if(this.ftimer>0){
                while(System.currentTimeMillis()-ftime<ftimer){ }
                ftime=System.currentTimeMillis();
            }*/         
            try{
                int suspTime=(int)(ftimer-(System.currentTimeMillis()-ftime));
                if (suspTime>0)
                    fThread.sleep(suspTime);
                    ftime=System.currentTimeMillis();
            }catch(InterruptedException e){
                e.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!
            }
            
            /*try{
                if(!bufferStrategy.contentsLost())
                    bufferStrategy.show();
                
                g=bufferStrategy.getDrawGraphics();
                draw((Graphics2D) g);
                g.dispose();
            }catch(NullPointerException e){
	        e.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!
            }*/
            
            if(inRect(mouseX,mouseY,475,210,25,80))
                scrollX--;
            if(inRect(mouseX,mouseY,0,210,25,80))
                scrollX++;
            if(inRect(mouseX,mouseY,210,0,80,25))
                scrollY++;
            if(inRect(mouseX,mouseY,210,475,80,25))
                scrollY--;
            
            mouseXSpeed=0;
            mouseYSpeed=0;
        }
    }
    
    private void setActive(){       
        tPosX.setText("0");
        tPosY.setText("0");
        
        tDimX.setText("0");
        tDimY.setText("0");
        
        sDensity.setValue(50);
    }
    
    public void setInactive(){
        bMoveable.setText("...");
        
        sDensity.setValue(50);
        
        tPosX.setText("");
        tPosY.setText("");
        tDimX.setText("");
        tDimY.setText("");
    }
    
    public void draw(Graphics2D g){
        g.setColor(g.getBackground());
        g.fillRect(0,0,500,500);
        
        g.setColor(new Color(175,175,175));
        g.drawLine(scrollX-5,scrollY-5,scrollX+5,scrollY+5);
        g.drawLine(scrollX+5,scrollY-5,scrollX-5,scrollY+5);
        
        int gridSize=25;
        if(sGrid.isSelected())
            for(int i=0; i<500+gridSize; i+=gridSize){
                g.drawLine(i+(scrollX % gridSize), 0, i+(scrollX % gridSize), 500);
                g.drawLine(0, i+(scrollY % gridSize), 500, i+(scrollY % gridSize));
            }
    }
    
    private void addToChgLog(String newText){
        String oldText=chgLog.getText();
        
        String part1, part2;
        int pos;
        
        pos=findSubstring(oldText,"</p>");
        
        part1=oldText.substring(0,pos);
        part2=oldText.substring(pos,oldText.length());
        
        chgLog.setText(part1+"<br>"+newText+part2);
    }
    
    public int findSubstring(String src, String part){
        boolean found;
        
        for(int i=0; i<src.length()-part.length(); i++){
            found=true;
            for(int j=0; j<part.length(); j++)
                if(src.charAt(i+j)!=part.charAt(j))
                    found=false;
            
            if(found)
                return i;
        }
        
        return -1;
    }
    
    /*
     * 
     */
    
    /*
     * Area of Creation Stuff
     */
    
    public boolean inRect(int x, int y, int x1, int y1, int b, int h){
        /*if(x>x1 && x<x1+b && y>y1 && y<y1+h)
             true;
        return false;*/
	
        return x>x1 && x<x1+b && y>y1 && y<y1+h; //so gehts einfacher
    }
    
    /*
     * 
     */
    
    /*
     * Area of Useless Stuff
     */
    
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    
    /*
     * 
     */
}
 


Schreibe deine Antwort... und nutze den </> Button, wenn du Code posten möchtest...
Ähnliche Java Themen
  Titel Forum Antworten Datum
volcanos Scrollen: JScrollPane mit Graphics g und Java-Fonts extends Frame ? AWT, Swing, JavaFX & SWT 5
E JScrollPane mit JPanel verbinden AWT, Swing, JavaFX & SWT 1
B Swing JScrollPane scrollt nicht AWT, Swing, JavaFX & SWT 10
P JTextarea (in JScrollPane) zeigt nur die beiden letzten Einträge an? AWT, Swing, JavaFX & SWT 0
O Swing "Eigenes" JPanel wird dem JScrollPane nicht hinzugefügt AWT, Swing, JavaFX & SWT 5
E Komponenten von JScrollPane werden nicht richtig ermittelt AWT, Swing, JavaFX & SWT 2
E wie in JScrollPane auf JTextArea zugreifen AWT, Swing, JavaFX & SWT 12
N Swing JScrollPane mit Tastatur bedienen? AWT, Swing, JavaFX & SWT 0
F JScrollPane anzeige AWT, Swing, JavaFX & SWT 7
K Swing JScrollPane wird nicht angezeigt wenn man Components darstellt. AWT, Swing, JavaFX & SWT 2
M Swing JPanel in JScrollPane AWT, Swing, JavaFX & SWT 3
M Problem mit Add JScrollPane AWT, Swing, JavaFX & SWT 25
P Swing JEditorPane in Kombination mit JScrollPane AWT, Swing, JavaFX & SWT 29
S Swing jScrollPane - pro Eintrag der Liste, die jCheckBoxes Speichern die in dem Eintrag aktiviert wurden AWT, Swing, JavaFX & SWT 2
T Swing Probleme mit repaint() bzw. JScrollPane AWT, Swing, JavaFX & SWT 7
D JScrollPane in JTabbedPane AWT, Swing, JavaFX & SWT 3
MaxG. Swing JScrollPane AWT, Swing, JavaFX & SWT 10
Soloeco Swing JScrollPane AWT, Swing, JavaFX & SWT 6
A Swing Probleme mit dem adden von JButtons zur JScrollPane AWT, Swing, JavaFX & SWT 2
A JScrollPane soll JPanel mit JButtons enthalten und eine Scollbar anzeigen AWT, Swing, JavaFX & SWT 1
J JScrollPane funktioniert nicht AWT, Swing, JavaFX & SWT 5
B LayoutManager GridBagLayout und JScrollPane AWT, Swing, JavaFX & SWT 5
Thallius JScrollPane Scrollpos setzen nach Neuzeichnen AWT, Swing, JavaFX & SWT 3
P MalProgramm mit JScrollPane und Canvas AWT, Swing, JavaFX & SWT 2
D Swing JScrollPane Tabelle ändern (2 Tabellen) AWT, Swing, JavaFX & SWT 7
U JScrollPane -> JTabbedPane u.A. AWT, Swing, JavaFX & SWT 3
A Layout-Manager, JScrollPane, ... Chaos AWT, Swing, JavaFX & SWT 5
V JScrollPane im zur Laufzeit verändernden JFrame AWT, Swing, JavaFX & SWT 0
W Swing Größenänderung vom JPanel im JScrollPane und anschließendes positionieren AWT, Swing, JavaFX & SWT 2
X Swing JButton's zum JScrollPane hinzufügen geht nicht. Bitte um Hilfe. AWT, Swing, JavaFX & SWT 9
G JTextArea mit JScrollPane wird nicht angezeigt AWT, Swing, JavaFX & SWT 2
K Swing JScrollPane - JScrollBar anzeigen, wenn Frame verkleinert wird AWT, Swing, JavaFX & SWT 3
N Swing Problem beim Scrollen mit JScrollPane AWT, Swing, JavaFX & SWT 6
Bluedaishi JScrollpane AWT, Swing, JavaFX & SWT 0
L JScrollPane scrollt nicht !! AWT, Swing, JavaFX & SWT 3
F JScrollPane Position setzen (x=0, y=0) AWT, Swing, JavaFX & SWT 2
R JScrollPane überdeckt JPanel? AWT, Swing, JavaFX & SWT 7
H JTree in JScrollPane passt sich nicht an Größe von JPanel an AWT, Swing, JavaFX & SWT 2
S JScrollPane und BorderLayout AWT, Swing, JavaFX & SWT 1
M Mehrere Jpanel in einem JScrollPane (Layout) AWT, Swing, JavaFX & SWT 2
W Swing JScrollPane für mein Fenster AWT, Swing, JavaFX & SWT 4
A Swing Probleme mit JScrollPane AWT, Swing, JavaFX & SWT 6
F JTable mit JScrollPane AWT, Swing, JavaFX & SWT 4
I JPanel in JScrollPane verkleinert sich leider nicht .... AWT, Swing, JavaFX & SWT 2
G JScrollPane bricht am Ende der Anzeige nicht um AWT, Swing, JavaFX & SWT 2
J Swing JTextPane mit JScrollPane auf JPanel scrollen nicht AWT, Swing, JavaFX & SWT 6
W Sichtbarer Bereich JScrollPane vs. JList AWT, Swing, JavaFX & SWT 3
B Swing JPanel in JScrollPane einfügen AWT, Swing, JavaFX & SWT 2
S 2D-Grafik Transparentes JScrollPane AWT, Swing, JavaFX & SWT 0
S 3D-Grafik 3d Grafik auf einem JPanel oder im JScrollPane AWT, Swing, JavaFX & SWT 1
R Ändern Default-Verhalten von JScrollpane in einer JScrollpane AWT, Swing, JavaFX & SWT 1
S JScrollPane --> Programm hängt sich beim scrollen auf AWT, Swing, JavaFX & SWT 2
A JScrollPane - ScrollBar Buttongröße verändern AWT, Swing, JavaFX & SWT 1
J Bilder auf JPanel (im JScrollPane) nacheinander laden AWT, Swing, JavaFX & SWT 0
A Swing JScrollPane Problem AWT, Swing, JavaFX & SWT 6
H Swing JScrollPane mit "viel Inhalt" scrollt zu langsam (inkl. See-For-Yourself.jar :D) AWT, Swing, JavaFX & SWT 2
T Swing JScrollPane in JPanel - Breite dynamisch, Höhe fix - wie? AWT, Swing, JavaFX & SWT 2
D Swing JScrollPane in JScrollPane - Problem AWT, Swing, JavaFX & SWT 3
K Bug in JScrollPane? AWT, Swing, JavaFX & SWT 4
T Swing JScrollPane Farbe der Scrollbar AWT, Swing, JavaFX & SWT 3
M JScrollPane Header AWT, Swing, JavaFX & SWT 3
G JScrollPane über JScrollPane AWT, Swing, JavaFX & SWT 2
V LayoutManager JScrollPane verwirft Elementanordnung AWT, Swing, JavaFX & SWT 3
B JScrollPane Problem AWT, Swing, JavaFX & SWT 2
D Swing JScrollPane AWT, Swing, JavaFX & SWT 7
H cardlayout und jscrollpane AWT, Swing, JavaFX & SWT 14
F 2D-Grafik Skalieren der Scrollbars beim Zoomen einer JScrollPane AWT, Swing, JavaFX & SWT 4
B JScrollPane Horizontal klappt nicht. AWT, Swing, JavaFX & SWT 4
G JScrollPane reagiert nicht auf Änderung der JPanel-Größe AWT, Swing, JavaFX & SWT 4
C jPanel auf jScrollPane zentrieren AWT, Swing, JavaFX & SWT 3
M Swing JScrollPane (Parent JTextArea) aktualisieren + neue Zeile AWT, Swing, JavaFX & SWT 5
U Swing JPane und JScrollpane in JSplitpane-Seite: Scrollen funktioniert nicht AWT, Swing, JavaFX & SWT 3
A Swing Korrekte JTable.rowAt(point) für JTable in JScrollPane AWT, Swing, JavaFX & SWT 5
D Label über JScrollPane AWT, Swing, JavaFX & SWT 3
S JScrollPane reagiert nicht AWT, Swing, JavaFX & SWT 3
X JScrollPane zu JTable hinzufügen AWT, Swing, JavaFX & SWT 16
H JScrollPane - JEditorPane und HTML-Tabelle AWT, Swing, JavaFX & SWT 2
D LayoutManager JScrollPane mit dynamischem Inhalt AWT, Swing, JavaFX & SWT 8
P Swing JScrollpane AWT, Swing, JavaFX & SWT 2
B JScrollPane scrollt nicht immer automatisch AWT, Swing, JavaFX & SWT 2
V Swing JScrollPane Viewport verschieben AWT, Swing, JavaFX & SWT 2
J JScrollPane Probleme AWT, Swing, JavaFX & SWT 3
S Swing JScrollPane verliert Scrollbars AWT, Swing, JavaFX & SWT 5
D Swing JTable in JScrollpane beliebig verbreitern AWT, Swing, JavaFX & SWT 5
A Swing Anzeige Text in JScrollpane AWT, Swing, JavaFX & SWT 8
J Swing Unteren Abstand für JTable in JScrollPane festlegen AWT, Swing, JavaFX & SWT 10
xehpuk Swing Line-wrapping von JTextPane (in JScrollPane) AWT, Swing, JavaFX & SWT 3
M JScrollPane Problem AWT, Swing, JavaFX & SWT 6
S Swing JScrollpane und ViewPort AWT, Swing, JavaFX & SWT 4
D JScrollPane funktioniert nicht AWT, Swing, JavaFX & SWT 12
X JScrollPane funzt net... AWT, Swing, JavaFX & SWT 5
H jscrollpane AWT, Swing, JavaFX & SWT 12
RELAXccc Swing JTree + JScrollPane, refresh Problem AWT, Swing, JavaFX & SWT 17
A Problem mit JScrollPane in einer JScrollpane AWT, Swing, JavaFX & SWT 4
B JScrollPane Problem AWT, Swing, JavaFX & SWT 25
T Swing JScrollPane- und Tabelle-Header Problem AWT, Swing, JavaFX & SWT 25
P Swing JScrollPane-Inhalt: Grösse anpassen AWT, Swing, JavaFX & SWT 6
G Swing Höhe des View eines JScrollPane fest auf Höhe des JScrollPane setzen! AWT, Swing, JavaFX & SWT 4
A JScrollPane AWT, Swing, JavaFX & SWT 16
C Swing JScrollPane und Scroll-Geschwindigkeit AWT, Swing, JavaFX & SWT 8

Ähnliche Java Themen

Neue Themen


Oben