M
Maus
Gast
Hallo Java Community
Meine JComponete will nicht wie ich will!
Ich folgendes programmiert:
Wenn ich nun an der einen Seite ziehe (NORTH) vergrößere ich die Fläche. Die JLabels werden aber beim loslassen nicht neugezeichnet. Sie füllen nicht die komplette Fläche der Komponente aus.
Was muss ich verändern damit es geht? Im Konstruktor habe ich die Labesl erzeugt und auf der Komponente verteilet.
Code:
Meine JComponete will nicht wie ich will!
Ich folgendes programmiert:
Wenn ich nun an der einen Seite ziehe (NORTH) vergrößere ich die Fläche. Die JLabels werden aber beim loslassen nicht neugezeichnet. Sie füllen nicht die komplette Fläche der Komponente aus.
Was muss ich verändern damit es geht? Im Konstruktor habe ich die Labesl erzeugt und auf der Komponente verteilet.
Code:
Code:
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Klasse extends JComponent implements MouseListener, MouseMotionListener{
Container cp;
private String text;
private Color color;
private Point location;
private Point size;
private boolean selectiert;
JLabel jl_north, jl_n, jl_east, jl_south, jl_s, jl_west, jl_center;
JLabel jl_nw, jl_ne, jl_sw, jl_se;
JPanel jl_alles;
public Klasse(String text, Color color, Point location, Point size) {
this.text = text;
this.color = color;
this.location = location;
this.size = size;
jl_north = new JLabel("n");
jl_east = new JLabel("e");
jl_south = new JLabel("s");
jl_west = new JLabel("w");
jl_center = new JLabel("c"); jl_center.setBackground(Color.green);
jl_n = new JLabel("n");
jl_s = new JLabel("s");
jl_nw = new JLabel("nw");
jl_ne = new JLabel("ne");
jl_sw = new JLabel("sw");
jl_se = new JLabel("se");
jl_north.setLayout(new BorderLayout());
jl_north.add(BorderLayout.WEST, jl_nw);
jl_north.add(BorderLayout.EAST, jl_ne);
jl_south.setLayout(new BorderLayout());
jl_south.add(BorderLayout.WEST, jl_sw);
jl_south.add(BorderLayout.EAST, jl_se);
this.setLayout(new BorderLayout());
this.add(BorderLayout.NORTH, jl_north);
this.add(BorderLayout.EAST, jl_east);
this.add(BorderLayout.SOUTH, jl_south);
this.add(BorderLayout.WEST, jl_west);
this.add(BorderLayout.CENTER, jl_center);
allMouseListener();
allMouseMotionListener();
allMausZeiger();
sonstigeEinstellungen();
init();
}
private void init() {
// this.setVerticalAlignment(JLabel.TOP);
// this.setHorizontalAlignment(JLabel.CENTER);
// this.setOpaque(true);
// this.setText(text);
// this.setBackground(color);
// this.setForeground(Color.black);
this.setBorder(new LineBorder(Color.black));
this.setBounds(location.x, location.y, size.x, size.y);
this.repaint();
}
// public void paintComponent(Graphics g) {
// }
public Point getXX() {
return location;
}
public void setXX(Point location) {
this.location = location;
init();
}
@Override
public void mouseClicked(MouseEvent event) {
Object obj = event.getSource();
if(obj.equals(jl_nw)) {
System.out.print("__NW__");
}
if(obj.equals(jl_north)) {
System.out.print("__NORTH__");
}
if(obj.equals(jl_ne)) {
System.out.print("__NE__");
}
if(obj.equals(jl_east)) {
System.out.print("__EAST__");
}
if(obj.equals(jl_se)) {
System.out.print("__SE__");
}
if(obj.equals(jl_south)) {
System.out.print("__SOUTH__");
}
if(obj.equals(jl_sw)) {
System.out.print("__SW__");
}
if(obj.equals(jl_west)) {
System.out.print("__WEST__");
}
if(obj.equals(jl_center)) {
System.out.print("__CENTER__");
}
}
@Override
public void mouseEntered(MouseEvent event) {
}
@Override
public void mouseExited(MouseEvent event) {
}
@Override
public void mousePressed(MouseEvent event) {
}
@Override
public void mouseReleased(MouseEvent event) {
}
// 2
@Override
public void mouseDragged(MouseEvent event) {
Object obj = event.getSource();
int mausX = event.getX();
int mausY = event.getY();
if(obj.equals(jl_north)) {
location.y += mausY;
size.y -= mausY;
init();
}
if(obj.equals(jl_south)) {
}
if(obj.equals(jl_center)) {
System.out.println("HAAAAAAAA");
}
// int x = event.getX();
// int y = event.getY();
//
// System.out.println("X " + x);
// System.out.println("Y " + y);
//
// this.setXX(new Point(size.x + x, size.y + y));
//
//// init();
// this.repaint();
}
@Override
public void mouseMoved(MouseEvent event) {
}
/******************************************************************************************/
private void allMouseMotionListener() {
jl_nw.addMouseListener(this);
jl_north.addMouseListener(this);
jl_ne.addMouseListener(this);
jl_east.addMouseListener(this);
jl_sw.addMouseListener(this);
jl_south.addMouseListener(this);
jl_se.addMouseListener(this);
jl_west.addMouseListener(this);
jl_center.addMouseListener(this);
}
private void allMouseListener() {
jl_nw.addMouseMotionListener(this);
jl_north.addMouseMotionListener(this);
jl_ne.addMouseMotionListener(this);
jl_east.addMouseMotionListener(this);
jl_sw.addMouseMotionListener(this);
jl_south.addMouseMotionListener(this);
jl_se.addMouseMotionListener(this);
jl_west.addMouseMotionListener(this);
jl_center.addMouseMotionListener(this);
}
private void allMausZeiger() {
jl_nw.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
jl_north.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
jl_ne.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
jl_east.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
jl_sw.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
jl_south.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
jl_se.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
jl_west.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
jl_center.setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
private void allRepaint() {
jl_nw.repaint();
jl_north.repaint();
jl_ne.repaint();
jl_east.repaint();
jl_sw.repaint();
jl_south.repaint();
jl_se.repaint();
jl_west.repaint();
jl_center.repaint();
}
/******************* müll ********************************************************************/
private void sonstigeEinstellungen() {
jl_north.setBackground(Color.black);
jl_east.setBackground(Color.black);
jl_south.setBackground(Color.black);
jl_west.setBackground(Color.black);
jl_center.setBackground(Color.gray);
jl_nw.setBackground(Color.orange);
jl_ne.setBackground(Color.orange);
jl_sw.setBackground(Color.orange);
jl_se.setBackground(Color.orange);
jl_north.setVerticalAlignment(JLabel.CENTER);
jl_east.setVerticalAlignment(JLabel.CENTER);
jl_south.setVerticalAlignment(JLabel.CENTER);
jl_west.setVerticalAlignment(JLabel.CENTER);
jl_center.setVerticalAlignment(JLabel.CENTER);
jl_north.setHorizontalAlignment(JLabel.CENTER);
jl_east.setHorizontalAlignment(JLabel.CENTER);
jl_south.setHorizontalAlignment(JLabel.CENTER);
jl_west.setHorizontalAlignment(JLabel.CENTER);
jl_center.setHorizontalAlignment(JLabel.CENTER);
}
}