Schriftgröße in JLabel ändern

  • Themenstarter Themenstarter ash34
  • Beginndatum Beginndatum
A

ash34

Gast
Hallo,

ich habe eine Klasse Box geschrieben, die von JLabel erbt. Die Klasse erzeugt eine Box. In diese Box wird ein Text geschrieben. Genau diesen Text würde ich gern in der Größe ändern.

Java:
public class Box extends JLabel {

	  /**
	 * 
	 */
	
	private static final long serialVersionUID = -2804169159814223929L;
	private int x, y;
    private int width, height;
    private Stroke stroke = new BasicStroke(1); // setting thickness of the strokes
    private Rectangle bounds;
    private Color color;
    private String boxValue = ""; // modifiable text (like 192.168.2.1...)
      
      /**
       * Constructor
       * @param width
       * @param height
       * @param x
       * @param y
       */
      
    public Box(int width, int height, int x, int y, Color color) {
          this.width = width;
          this.height = height;
          this.color = color;
          this.setLocation(x, y);
          this.setFont(new Font("Dialog", 0, 50));
      }
      
     public void setLocation(int x, int y) {
          this.x = x;
          this.y = y;
          bounds = new Rectangle(x, y, width, height);
      }
      
     public Rectangle getBounds() {
          return bounds;
      }
      
     public Color getColor(Box box){
    	  return this.color;
      }
      
     public int getWidth(){
    	  return this.width;
      }

     public int getHeight(){
    	  return this.height;
      }
      
     public int getX(){
    	  return this.x;
      }
      
     public int getY(){
    	  return this.y;
      }
      
     public void setWidth(int width){
    	  this.width = width;
      }
      
     public void setColor(Color color){
    	  this.color = color;
      }      
      
     public void setValue(String str){
    	  this.boxValue = str;
      }
      
     public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D)g.create();
       
          g2.setStroke(stroke);
          g2.setColor(color);
          g2.fillRect(x, y, width, height);
          g2.setColor(Color.BLACK);
          g2.drawRect(x, y, width, height);
          g.drawString(this.getText(), x+2, y+11);
          g.drawString(boxValue, x+20, y+35);
          g2.dispose();
  }
}

Java:
 this.setFont(new Font("Dialog", 0, 50));

Bringt leider nicht den gewünschten Effekt.

Erzeugt wird jede Box in einer Klasse BoxPanel.

Java:
public void fillBoxPanel(){
	    	
		String[] boxText = {"Checksum","Code","Data","HdrLen","Hdr Checksum",
							"FOS","Identifier","Total Length",
							"Length/Type","Options","Pad","Packet ID","Padding",
							"Protocol","Präambel","Prüfsumme","SeqNumr",
							"SourAddr(Eth)","SourAddr(IP)","TarAddr(Eth)",
							"TarAddr(IP)","TOS","TTL","Type","Vers"};
		
	    	
		int n = 0;						// horizontal gap between boxes
		int m = 40; 					// vertical gap between boxes
		int rowCount = 12; 				// amount of boxes in one row 
		int x = 45;						// vertical gap for the first box (related to (0,0))
		int y = 1;						// horizontal gap for the first box (related to (0,0))
		int width = standardWidth;		// width of a normal box
		int height = standardHeight;	// height of a normal box
		this.color = Color.WHITE;		// color of a normal box

//				creating boxes, setting color and text to each of the boxes
	    		
		for(int i=0; i<boxText.length; i++){
	    			
			if(i < rowCount){					
				Box box = new Box(width, height, x+n, y, color);
				box.setText(boxText[i]);
				box.setColor(color);
				this.addBox(box);
				n = width*(i+1);
	   			}
	    			
			if(i == rowCount){
	    		n=0;
	    		Box box = new Box(width, height, x+n, y+m, color);
	    		box.setText(boxText[i]);
	    		box.setColor(color);
	    		this.addBox(box);
	    		n = width*(i-12);
	    		}
	    			
	    	if(i > rowCount){
	    		Box box = new Box(width, height, x+n, y+m, color);
	    		box.setText(boxText[i]);
	    		box.setColor(color);
	    		this.addBox(box);
	    		n = width*(i-12);
	    		}
	    }
	}
 
In deiner überlagerten paintComponent(Graphics g) Methode wird der Font des Graphis Argumentes nicht gesetzt.
Was spricht gegen die Verwendung eines normalen JLabels mit einer Border?
 
Was spricht gegen die Verwendung eines normalen JLabels mit einer Border?


Gute Frage..ich habe diese Klasse Box hier im Forum gefunden und dann nach meinen Bedürfnissen verändert...

Der Aufwand dürfte auch nicht viel geringer sein, wenn ich ein normales JLabel benutze. Dort müsste ich auch die Hintergrundfarbe, etc ändern..
 
Hmm...wenn ich ne ruhige Minute finde, schreib ich das vielleicht mal um..
Der Gedanke ist auf jeden Fall interessant..
 

Zurück
Oben