.


:




:

































 

 

 

 


NetBeans




 

 

, (title). javax.swing.JPanel

import javax.swing.*;

Object JPanel.

myBean JTitledPanel, Fields ( Bean Patterns!) sampleProperty title, PROP_SAMPLE_PROPERTY , title firePropertyChange.

Bean Patterns , Rename () sampleProperty title. , getSampleProperty setSampleProperty getTitle setTitle.

title , Bean Pattern, .

private String title=;

java.awt.Graphics, java.awt.geom.Rectangle2D JTitledPanel.java paint:

 

public void paint(Graphics g){

super.paint(g);

FontMetrics fontMetrics=g.getFontMetrics();

Rectangle2D rect = fontMetrics.getStringBounds(title, g);

g.drawString(title,(int)Math.round((this.getWidth()-rect.getWidth())/2),

10);

}

, Graphics, FontMetrics Rectangle2D,

import java.awt.*;

import java.awt.geom.Rectangle2D;

 

, fontMetrics rect, drawString :

 

g.drawString(title,

(int)Math.round((this.getWidth() -

g.getFontMetrics().getStringBounds(title,g).getWidth()

)/2),

10);

 

. , - .

 

repaint() setTitle. , .

:

 

/*

* JTitledPanel.java

*

* Created on 30 2006 ., 23:16

*/

 

package java_gui_example;

 

import java.beans.*;

import java.io.Serializable;

import javax.swing.*; //

import java.awt.*; //

import java.awt.geom.Rectangle2D; //

 

/**

* @author .

*/

public class JTitledPanel extends JPanel implements Serializable {

 

private String title=""; //

 

private PropertyChangeSupport propertySupport;

 

public JTitledPanel() {

super();

propertySupport = new PropertyChangeSupport(this);

}

 

public String getTitle() {

return title;

}

 

public void setTitle(String value) {

String oldValue = title;

title = value;

propertySupport.firePropertyChange(title, oldValue, title);

repaint(); //

}

 

 

public void addPropertyChangeListener(PropertyChangeListener listener) {

propertySupport.addPropertyChangeListener(listener);

}

 

public void removePropertyChangeListener(PropertyChangeListener listener){

propertySupport.removePropertyChangeListener(listener);

}

 

public void paint(Graphics g){ //

super.paint(g);

FontMetrics fontMetrics=g.getFontMetrics();

Rectangle2D rect = fontMetrics.getStringBounds(title, g);

g.drawString(title,(int)Math.round((this.getWidth() -

rect.getWidth())/2), 10);

}

 

}

 

, , JTitledPanel.java , Tools Add to Palette. , .

 

,

 

Beans ( ) OK. .

 

, . , , . , - :

 

 

Graphics g=jTitledPanel1.getGraphics();

FontMetrics fontMetrics=g.getFontMetrics();

Rectangle2D rect = fontMetrics.getStringBounds("", g);

g.drawString("",10,30);

 

, (Build main project F11).





:


: 2017-02-28; !; : 1180 |


:

:

, ; , .
==> ...

1986 - | 1782 -


© 2015-2024 lektsii.org - -

: 0.013 .