Automatische anpassung im NullLayout

arab900

Mitglied
Hallo,

Ich habe mehrere Räume welche ich als JPanels angelegt habe in einem JFrame (Gridbaglayout).
In diesen Panels werden weitere (JPanels) hinzugefügt welche dann mit der Mouse bewegt werden
können. Das bewegen funktioniert nur im NullLayout deshalbs fehlt mir die automatische Anpassung von Größe und Position.

Das JFrame wird ist nur über 2 Buttons verkleiner - und vergrößerbar und das nur um einen Faktor der immer um 0,1 erhöt wird sodass die Skalierung richtig bleibt, denn die Panels sind nach den Maßen der Räume gezeichnet worden.

z.B. 1m = 10 px also nicht besonderes.

Bild vom Panel im Anhang.


Bis jetzt bin ich so weit gekommen:
Java:
// Hier speichere in die alte größe vom Panel
oldOfficeWidth = officePanel.getSize().width; 
oldOfficeHeight = officePanel.getSize().height;

this.setResizable(true);
// Hier wird der Frame um einen Faktor vergrößert hier 1.1 
setSize(new Dimension(this.getSize().width + 1.1, this.getSize().height + 1.1))
this.setResizable(false);
			
this.validate();
		
// hier berechne ich den Faktor für die breite und höhe 	
double wFaktor,hFaktor;
wFaktor= (double) officePanel.getSize().width / oldOfficeWidth; 
hFaktor= (double) officePanel.getSize().height / oldOfficeHeight;

//Device ist das Objekt, hier versuche ich die größe und position um den Faktor zu erhöhen 
device.setBounds((int)(device.getLocation().x  + wFaktor), (int)(device.getLocation().y  * hFaktor), 
(int)(device.getSize().width  * wFaktor),(int) (device.getSize().height * hFaktor));

Habe jetzt lange daran rumprobiert aber mir fällt irgendwie nichts mehr ein,
deshalb würde ich um einen denkanstoss bitten.
Vielleicht kennt ja jemand einen Layoutmanager in dem das funktionieren würde oder ähnliches.

Objekte bewegen funktioniert
Objekte automatisch bei Fenstergrößenänderung anpassen funktioniert nicht
 

Anhänge

  • BildPanel.png
    BildPanel.png
    37,3 KB · Aufrufe: 34
Zuletzt bearbeitet:

Gucky

Top Contributor
Kein Problem. Das sieht doch gut aus. Aber du könntest es nich mal mit diesem Weg versuchen:

Jede Größe des Feldes bekommt eine Zahl, den Multiplikator. Eins ist das kleinstmögliche und dann geht es in frei wählbaren Schritten weiter. Jedes Objekt bekommt eine "Grundgröße", die bei jeder Änderung mit dem Multiplikator multipliziert wird. So müsste es klappen.
 

arab900

Mitglied
Danke erstmal.

Also ich hab jetzt dem Objekt sowie den Feldern also den Panels eine Grundgröße
gegeben und einen Faktor erstellt vom Wert 1 welcher sich immer um 0.1 vergrößert.

Jede Größe also:
Width, Height, x-Pos. , y-Pos

wird mit dem Faktor multipliziert.

Bei vergrößerung wird dann der Faktor erhöht und nochmal mit der Grundgröße multipliziert.

Aber funktioniert immernoch nicht der gleiche Fehler wie davor.
 

arab900

Mitglied
wenn zum beispiel ich das objekt unter diese Öffnung (Türe) positioniere und dann vergrößere verschiebt sich das objekt vor die türe (bewegt sich nach oben) und das möchte ich naturlich verhindern aber wie weiß ich nicht ^^
 

arab900

Mitglied
Ich war gerade noch am rumprobieren bis jetzt sieht es so aus.

Java:
package qs19ESLPlanning.panel;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import javax.swing.BoxLayout;

import net.miginfocom.swing.MigLayout;

import java.awt.Insets;

import javax.swing.border.MatteBorder;

import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.SwingConstants;

import qs19ESLPlanning.QSESLDatabaseConnection;
import qs19ESLPlanning.QSESLDevice;
import qs19ESLPlanning.QSESLDevice2;
import qs19ESLPlanning.QSESLFunctions;
import uipanels.QSDesktopPanel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.SpringLayout;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;

import javax.swing.JTable;
import javax.swing.JMenuBar;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import java.awt.event.MouseAdapter;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.KeyStroke;

import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSeparator;


public class Layout2 extends JFrame implements ActionListener, MouseListener{

	/**
	 * Create the panel.
	 */
	private JPanel microscopePanel,mechanicPanel,garagePanel,officePanel,climaticPanel;
	private JFrame frame = this;
	
	private JMenuBar menuBar;
	private JButton btnSmaller;
	private JButton btnBigger;
	private double faktor = 1.1,dFaktor = 0.08;
	private static int height = 320;
	private static int width = 300;
	private int oldFrameWidth,oldFrameHeight,oldOX,oldOY;
	private Device device;
	private GroupLayout gl_officePanel;
	private int oPX,oPY,oW,oH;
	
	public Layout2() {
		setMinimumSize(new Dimension(width,height));
		this.setResizable(false);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{80, 80, 40, 80, 0};
		gridBagLayout.rowHeights = new int[]{40, 30, 90, 90, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		
		getContentPane().setLayout(gridBagLayout);
		
		microscopePanel = new JPanel();
		microscopePanel.setBorder(new MatteBorder(1, 1, 0, 1, (Color) new Color(0, 0, 0)));
		GridBagConstraints gbc_microscopePanel = new GridBagConstraints();
		gbc_microscopePanel.insets = new Insets(0, 0, 5, 5);
		gbc_microscopePanel.fill = GridBagConstraints.BOTH;
		gbc_microscopePanel.gridx = 2;
		gbc_microscopePanel.gridy = 1;
		gbc_microscopePanel.insets = new Insets(0,0,0,0);
		getContentPane().add(microscopePanel, gbc_microscopePanel);
		
		mechanicPanel = new JPanel(){
			@Override
			public void paintComponent(Graphics g){
				super.paintComponent(g);
				g.drawLine(0, 0, this.getWidth()-1, 0);
				g.drawLine(0, 0, 0, this.getHeight()-1);
				g.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1);
				g.drawLine(this.getWidth()-1, this.getHeight()-1, this.getWidth()-1, (int) ((this.getHeight()-1) * 0.6));
				g.drawLine(this.getWidth()-1, 0, this.getWidth()-1, (int) ((this.getHeight()-1) * 0.25));	
				this.validate();
			}
		};
		mechanicPanel.setBorder(null);
		GridBagConstraints gbc_mechanicPanel = new GridBagConstraints();
		gbc_mechanicPanel.insets = new Insets(0, 0, 5, 5);
		gbc_mechanicPanel.fill = GridBagConstraints.BOTH;
		gbc_mechanicPanel.gridx = 0;
		gbc_mechanicPanel.gridy = 2;
		gbc_mechanicPanel.insets = new Insets(0,0,0,0);
		getContentPane().add(mechanicPanel, gbc_mechanicPanel);
		GridBagLayout gbl_mechanicPanel = new GridBagLayout();
		gbl_mechanicPanel.columnWidths = new int[]{0};
		gbl_mechanicPanel.rowHeights = new int[]{0};
		gbl_mechanicPanel.columnWeights = new double[]{Double.MIN_VALUE};
		gbl_mechanicPanel.rowWeights = new double[]{Double.MIN_VALUE};
		mechanicPanel.setLayout(gbl_mechanicPanel);
		
		climaticPanel = new JPanel(){
			@Override
			public void paintComponent(Graphics g){
				super.paintComponent(g);
				g.drawLine(0, 0, this.getWidth()-1-microscopePanel.getWidth(), 0);
//				g.drawLine(0, 0, 0, this.getHeight()-1);
				g.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1);
				g.drawLine(this.getWidth()-1, this.getHeight()-1, this.getWidth()-1, (int) ((this.getHeight()-1) * 0.4));
				g.drawLine(this.getWidth()-1, 0, this.getWidth()-1, (int) ((this.getHeight()-1) * 0.20));	
				
				this.validate();
			}
		};
		climaticPanel.setBorder(null);
		GridBagConstraints gbc_climaticPanel = new GridBagConstraints();
		gbc_climaticPanel.gridwidth = 2;
		gbc_climaticPanel.insets = new Insets(0, 0, 5, 5);
		gbc_climaticPanel.fill = GridBagConstraints.BOTH;
		gbc_climaticPanel.gridx = 1;
		gbc_climaticPanel.gridy = 2;
		gbc_climaticPanel.insets = new Insets(0,0,0,0);
		getContentPane().add(climaticPanel, gbc_climaticPanel);
		GridBagLayout gbl_climaticPanel = new GridBagLayout();
		gbl_climaticPanel.columnWidths = new int[]{0};
		gbl_climaticPanel.rowHeights = new int[]{0};
		gbl_climaticPanel.columnWeights = new double[]{Double.MIN_VALUE};
		gbl_climaticPanel.rowWeights = new double[]{Double.MIN_VALUE};
		climaticPanel.setLayout(gbl_climaticPanel);
		
		officePanel = new JPanel(){
			@Override
			public void paintComponent(Graphics g){
				super.paintComponent(g);
				g.drawLine(0, 0, 0, this.getHeight()-1-(climaticPanel.getHeight()+microscopePanel.getHeight()));
				this.validate();
			}
		};
		officePanel.addMouseListener(this);
		officePanel.setBorder(new MatteBorder(1, 0, 1, 1, (Color) new Color(0, 0, 0)));
		officePanel.setLayout(null);
		GridBagConstraints gbc_officePanel = new GridBagConstraints();
		gbc_officePanel.insets = new Insets(0, 0, 5, 0);
		gbc_officePanel.gridheight = 3;
		gbc_officePanel.fill = GridBagConstraints.BOTH;
		gbc_officePanel.gridx = 3;
		gbc_officePanel.gridy = 0;
		gbc_officePanel.insets = new Insets(0,0,0,0);
		getContentPane().add(officePanel, gbc_officePanel);

		
		garagePanel = new JPanel();
		garagePanel.setBorder(new MatteBorder(0, 1, 1, 1, (Color) new Color(0, 0, 0)));
		GridBagConstraints gbc_garagePanel = new GridBagConstraints();
		gbc_garagePanel.gridwidth = 3;
		gbc_garagePanel.insets = new Insets(0, 0, 0, 5);
		gbc_garagePanel.fill = GridBagConstraints.BOTH;
		gbc_garagePanel.gridx = 0;
		gbc_garagePanel.gridy = 3;
		gbc_garagePanel.insets = new Insets(0,0,0,0);
		getContentPane().add(garagePanel, gbc_garagePanel);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		btnSmaller = new JButton("-");
		btnSmaller.addActionListener(this);
		btnSmaller.setActionCommand("-");
		menuBar.add(btnSmaller);
		
		btnBigger = new JButton("+");
		btnBigger.addActionListener(this);
		btnBigger.setActionCommand("+");
		menuBar.add(btnBigger);
//		officePanel.setLayout(new BorderLayout());
		load();

	}
	
	public void load() {
		
		
		device = new Device();
		device.setBounds(0, 111, 24, 25);
		oPX = 0;
		oPY = 111;
		oW = 24;
		oH = 25;
		officePanel.add(device);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getActionCommand().equals("+")){
			faktor += 0.1;
			oldOX = device.getLocation().x; 
			oldOY = device.getLocation().y;
			oldFrameWidth = getSize().width;
			oldFrameHeight = getSize().height;
			this.setResizable(true);
			this.setSize(MyFunctions.getDimension(faktor,height,width));
			this.setResizable(false);
			
			this.validate();
			
			device.setBounds((int)( oPX * faktor ), (int)(oPY * faktor), 
					(int)(oW * faktor),(int) (oH * faktor));
		
		}
		if(ae.getActionCommand().equals("-")){
			faktor -= 0.1;
////			oldOfficeWidth = officePanel.getSize().width; 
////			oldOfficeHeight = officePanel.getSize().height;
////			oldFrameWidth = getSize().width;
//			oldFrameHeight = getSize().height;
//			this.setResizable(true);
//			frame.setSize(MyFunctions.getDimension(faktor,height,width));
//			this.setResizable(false);
//			
//			this.validate();
//			
//			double aRechts,aUnten;
//			aRechts = (double) officePanel.getSize().width / oldOfficeWidth; 
//			aUnten = (double) officePanel.getSize().height / oldOfficeHeight;
//			
//			int w =  officePanel.getSize().width - oldOfficeWidth ; 
//			int h =  officePanel.getSize().height - oldOfficeHeight; 
//			device.setBounds((int)(device.getLocation().x), (int)(device.getLocation().y), 
//					(int)(device.getSize().width  * aRechts),(int) (device.getSize().height * aUnten));
//			System.out.println(oldOfficeHeight + "-" + officePanel.getSize().height + "=" + (oldOfficeHeight - officePanel.getSize().height));			
		}
		
		// TODO Auto-generated method stub
    }
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	protected void layoutMyPanel(int newWidth, int newHeight){
		//myPanel.removeAll(); ...ggfs nötig? (s. (*))    
//		myTable.setBounds( /*neue bounds*/ );
		//myPanel.add(myTable);(*)     
//		myTextPane.setBounds( /* neue bounds*/ );
		//myPanel.add(myTextPane);(*)     ...usw.        
//		myPanel.revalidate();}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getLocationOnScreen());
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
 

Gucky

Top Contributor
Ich sehe nirgends, dass du auch die Größe der Objekte anpasst. Korrigier mich, wenn ich falsch liege aber kann es sein, dass die Objekte ihre alte Größe behalten, wenn du vergrößerst oder verkleinerst?
 

arab900

Mitglied
[Java] device.setBounds((int)( oPX * faktor ), (int)(oPY * faktor),
(int)(oW * faktor),(int) (oH * faktor)); [/code]

Hier wird die Position und die Größe gesetzt alles mit Faktoren :)

Sorry habe etwas wenig Zeit um schnell zurück zu schreiben, bin nicht oft online.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
Neumi5694 Swing Gridbaglayout - automatische Anpassung verhindern AWT, Swing, JavaFX & SWT 1
S automatische Zeilenhöhen Anpassung bei JTable AWT, Swing, JavaFX & SWT 2
L Layout automatische Anpassung umgehen? AWT, Swing, JavaFX & SWT 5
M Automatische Anpassung eines JPanels in einem JFrame AWT, Swing, JavaFX & SWT 6
H automatische Anzahl der Spalten ermitteln -> geht nicht AWT, Swing, JavaFX & SWT 6
J Swing Pane im SplitPane automatische Größe aktivieren AWT, Swing, JavaFX & SWT 0
S java.fxml.load.exception und keine automatische Aktualliseriung der Mainausgabe AWT, Swing, JavaFX & SWT 5
I JAVAFX - CSS - automatische Property- und Methoden-Vorlagen in Eclipse AWT, Swing, JavaFX & SWT 17
F Textfeld Währungszahlen und automatische Aktualisierung AWT, Swing, JavaFX & SWT 14
S Swing Warum funktioniert der automatische Zeilenumbruch mit arabischen Zeichen beim JTextPane nicht AWT, Swing, JavaFX & SWT 3
J ungewollt-automatische Größenänderung von JLabel AWT, Swing, JavaFX & SWT 5
N JEditorPane und automatische Scrollposition AWT, Swing, JavaFX & SWT 2
B Automatische Größenanpassung AWT, Swing, JavaFX & SWT 7
K Automatische Skalierung von GUI Elementen (Java Swing) AWT, Swing, JavaFX & SWT 2
N automatische Auswahl einer JComboBox AWT, Swing, JavaFX & SWT 6
C Automatische Screenshots + Analyse des Bildes AWT, Swing, JavaFX & SWT 5
aze GridLayout: Keine automatische Ausdehnung AWT, Swing, JavaFX & SWT 2
A AWT Automatische Weiterleitung? AWT, Swing, JavaFX & SWT 13
L JTable automatische Spaltenbreite AWT, Swing, JavaFX & SWT 2
G JPanel automatische Größ AWT, Swing, JavaFX & SWT 4
C JSplitPane automatische Veränderung verbieten. AWT, Swing, JavaFX & SWT 3
T Automatische Grössenanpassung JPanel / JScrollpane AWT, Swing, JavaFX & SWT 3
G GridBagLayout - automatische Größenanpassung AWT, Swing, JavaFX & SWT 3
L jTextField mit automatische Suchfunktion? AWT, Swing, JavaFX & SWT 2
A Automatische Scrollbalken bei einem JFrame AWT, Swing, JavaFX & SWT 8
ExceptionOfExpectation Anpassung von JKomponentengrößen (Swing) AWT, Swing, JavaFX & SWT 3
S 2D-Grafik User-BufferedImage rotieren im Zentrum und ohne "anpassung" AWT, Swing, JavaFX & SWT 2
J Java - dynamische Anpassung an Fenstergröße? AWT, Swing, JavaFX & SWT 4
K JTable +TableModel --> Anpassung der Tabellengröße (Zeilenanzahl) AWT, Swing, JavaFX & SWT 3
C Anpassung von JLabel Größe AWT, Swing, JavaFX & SWT 4
T Dynamische Anpassung einer jCheckBox AWT, Swing, JavaFX & SWT 11
G Größe JLabel anpassung an Textlänge AWT, Swing, JavaFX & SWT 3
Z Swing Nulllayout AWT, Swing, JavaFX & SWT 9
GUI-Programmer LayoutManager NullLayout - Ist es auch in diesem Fall schlecht? AWT, Swing, JavaFX & SWT 3
T NullLayout verwenden, oder nicht? AWT, Swing, JavaFX & SWT 9
alex_fairytail LayoutManager NullLayout überlappende Labels werden nicht angezeigt AWT, Swing, JavaFX & SWT 4
H Nulllayout in ein anderes Layout AWT, Swing, JavaFX & SWT 5
V Nulllayout und wie auf Resizen reagieren? AWT, Swing, JavaFX & SWT 4
L Größe von Komponenten im Nulllayout mit der Maus verändern. AWT, Swing, JavaFX & SWT 2
G NullLayout: "Überlappung von Komponenten" AWT, Swing, JavaFX & SWT 15

Ähnliche Java Themen

Neue Themen


Oben