下面列出了javax.swing.JComponent#setSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 初始化主窗口信息
*/
public void init()
{
this.setJMenuBar((JMenuBar)XJava.getObject("xmMainMenuBar"));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add((Component)XJava.getObject("xpMain") ,BorderLayout.CENTER);
this.setEnabled(false);
JComponent v_JC = ((JComponent)XJava.getObject("xlTables"));
v_JC.setSize(50 ,v_JC.getHeight());
this.showHintInfo("请稍后,正在访问数据库..." ,Color.BLUE);
this.initListModel();
this.initTableModel();
this.initTablesInfo();
this.setEnabled(true);
this.showHintInfo("欢迎使用!");
}
public void paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(location.x, location.y, size.width, size.height);
JComponent component = mainRenderer.getComponent();
int componentWidth = component.getPreferredSize().width;
int componentX = size.width - componentWidth;
mainRenderer.move(location.x + componentX, location.y);
component.setSize(componentWidth, size.height);
component.paint(g);
if (numberPercentRenderer == null || numberPercentRenderer.valueRenderers()[1].getComponent().isVisible()) {
int freeWidth = size.width - maxRendererWidth - renderersGap();
if (freeWidth >= MIN_BAR_WIDTH) {
barRenderer.setSize(Math.min(freeWidth, MAX_BAR_WIDTH), size.height);
barRenderer.move(location.x, location.y);
barRenderer.paint(g);
}
}
}
private Image getComponentImage(JComponent component) {
// Initial component sizing & layout
component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
component.doLayout(); // layout component
// Correct component sizing & layout
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// One more iteration because of nested JTextAreas
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// Paint component into BufferedImage
BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
BufferedImage.TYPE_INT_RGB);
component.printAll(componentImage.getGraphics());
return componentImage;
}
public void paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(location.x, location.y, size.width, size.height);
JComponent component = mainRenderer.getComponent();
int componentWidth = component.getPreferredSize().width;
int componentX = size.width - componentWidth;
mainRenderer.move(location.x + componentX, location.y);
component.setSize(componentWidth, size.height);
component.paint(g);
if (barRenderer.isVisible()) {
int freeWidth = size.width - maxRendererWidth - renderersGap();
if (freeWidth >= MIN_BAR_WIDTH) {
barRenderer.setSize(Math.min(freeWidth, MAX_BAR_WIDTH), size.height);
barRenderer.move(location.x, location.y);
barRenderer.paint(g);
}
}
}
private Image getComponentImage(JComponent component) {
// Initial component sizing & layout
component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
component.doLayout(); // layout component
// Correct component sizing & layout
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// One more iteration because of nested JTextAreas
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// Paint component into BufferedImage
BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
BufferedImage.TYPE_INT_RGB);
component.printAll(componentImage.getGraphics());
return componentImage;
}
/** Create an icon.
* @param comp a component, which must be unattached to a container
* and should not be used for other purposes
*/
public ComponentIcon(JComponent comp) {
if (comp.getParent() != null) {
throw new IllegalArgumentException();
}
this.comp = comp;
Dimension size = comp.getPreferredSize();
// Careful! If you have e.g. a JLabel with empty text, width = 0 => exceptions.
// Must make sure it is at least a reasonable size.
comp.setSize(Math.max(size.width, 16), Math.max(size.height, 16));
}
private JComponent layoutComponent(JComponent component) {
// Initial component sizing & layout
if (getClientSize().width > 0) {
component.setSize(getClientSize()); // try to fit the component to ComponentMorpher
component.validate(); // layout component
// Correct component sizing & layout
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.validate(); // layout component
}
return component;
}
@Override
public void layoutContainer( Container parent ) {
int width = getWidth ();
int height = 0;
for( int i=0; i<descriptors.length; i++ ) {
CategoryDescriptor paletteCategoryDescriptor = descriptors[i];
paletteCategoryDescriptor.setPositionY( height );
JComponent comp = paletteCategoryDescriptor.getComponent();
comp.setSize( width, comp.getPreferredSize().height );
height += paletteCategoryDescriptor.getComponent().getHeight();
}
}
private static JComponent createComponent(String str) {
JComponent comp = new JLabel(str);
comp.setSize(WIDTH, HEIGHT);
comp.putClientProperty(TextAttribute.NUMERIC_SHAPING, NUMERIC_SHAPER);
comp.setFont(getFont());
return comp;
}
private JComponent layoutComponent(JComponent component) {
// Initial component sizing & layout
if (getClientSize().width > 0) {
component.setSize(getClientSize()); // try to fit the component to ComponentMorpher
component.validate(); // layout component
// Correct component sizing & layout
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.validate(); // layout component
}
return component;
}
/**
* This makes some visual changes to the component and its children. Shortly
* after calling this method, the <code>restore()</code> method needs to be
* called.
*/
private static void prep(Component c) {
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
if (jc.isOpaque()) {
jc.setOpaque(false);
jc.putClientProperty(USED_TO_BE_OPAQUE, Boolean.TRUE);
}
Dimension preferredSize = c.getPreferredSize();
if (greaterThanOrEqualTo(jc.getSize(), preferredSize) == false) {
jc.putClientProperty(SIZE, c.getSize());
jc.setSize(preferredSize);
}
}
if (c instanceof JSlider) {
JSlider s = (JSlider) c;
ChangeListener[] listeners = s.getChangeListeners();
int mid = (s.getMinimum() + s.getMaximum()) / 2;
if (mid != s.getValue()) {
s.putClientProperty(CHANGE_LISTENERS, listeners);
for (int a = 0; a < listeners.length; a++) {
s.removeChangeListener(listeners[a]);
}
s.putClientProperty(SLIDER_VALUE, new Integer(s.getValue()));
s.setValue(mid);
}
}
if (c instanceof Container) {
Container c2 = (Container) c;
for (int a = 0; a < c2.getComponentCount(); a++) {
prep(c2.getComponent(a));
}
}
if (c.isValid() == false)
c.validate();
}
/**
* Should be called shortly after <code>prep()</code>.
*/
private static void restore(Component c) {
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
Boolean b = (Boolean) jc.getClientProperty(USED_TO_BE_OPAQUE);
if (b != null && b.booleanValue()) {
jc.setOpaque(true);
}
jc.putClientProperty(USED_TO_BE_OPAQUE, null);
Dimension d = (Dimension) jc.getClientProperty(SIZE);
if (d != null) {
jc.setSize(d);
jc.putClientProperty(SIZE, null);
}
}
if (c instanceof JSlider) {
JSlider s = (JSlider) c;
ChangeListener[] listeners = (ChangeListener[]) s
.getClientProperty(CHANGE_LISTENERS);
Integer i = (Integer) s.getClientProperty(SLIDER_VALUE);
if (i != null)
s.setValue(i.intValue());
if (listeners != null) {
for (int a = 0; a < listeners.length; a++) {
s.addChangeListener(listeners[a]);
}
}
s.putClientProperty(SLIDER_VALUE, null);
s.putClientProperty(CHANGE_LISTENERS, null);
}
if (c instanceof Container) {
Container c2 = (Container) c;
for (int a = 0; a < c2.getComponentCount(); a++) {
restore(c2.getComponent(a));
}
}
}
/**
* Creates a MockComponent that resembles the argument component.
* <P>
* Note this method will traverse c and its subcomponents and may
* temporarily change properties of inner components: such as the focused
* state, the visibility, etc.
* <P>
* The goal is of this component is not to mirror the exact state of a
* component, but rather to provide a sample image of this component in its
* plain, unmodified, unused state.
*
* @param c
*/
public MockComponent(JComponent c) {
Dimension preferredSize = c.getPreferredSize();
Dimension currentSize = c.getSize();
Dimension d = new Dimension(Math.max(preferredSize.width,
currentSize.width), Math.max(preferredSize.height,
currentSize.height));
if (currentSize.width == 0 || currentSize.height == 0) {
// if the component isn't visible yet
c.setSize(d);
c.doLayout();
}
storeState(c);
image = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, d.width, d.height);
g.setComposite(AlphaComposite.SrcOver);
c.paint(g);
g.dispose();
setPreferredSize(d);
setMinimumSize(d);
setMaximumSize(d);
setOpaque(c.isOpaque());
setName(c.getName());
setToolTipText(c.getToolTipText());
restoreState(c);
}
@Override
public void render(Graphics graphics, int width, int height) {
JComponent renderComponent = getRenderComponent();
renderComponent.setSize(width, height);
renderComponent.setDoubleBuffered(false);
renderComponent.paint(graphics);
renderComponent.setDoubleBuffered(true);
}
private void installInLayeredPane(JComponent component) {
JLayeredPane layeredPane = getRootPane().getLayeredPane();
layeredPane.add(component, JLayeredPane.PALETTE_LAYER, 20);
Dimension size = component.getPreferredSize();
component.setSize(size);
component.setLocation((getWidth() - size.width) / 2,
(getHeight() - size.height) / 2);
component.revalidate();
component.setVisible(true);
}
/**
* Reinitializes this ShadowPopup using the given parameters.
*
* @param newOwner component mouse coordinates are relative to, may be null
* @param newContents the contents of the popup
* @param newX the desired x location of the popup
* @param newY the desired y location of the popup
* @param newPopup the popup to wrap
*/
private void reset(Component newOwner, Component newContents, int newX, int newY,
Popup newPopup) {
this.owner = newOwner;
this.contents = newContents;
this.popup = newPopup;
this.x = newX;
this.y = newY;
if (newOwner instanceof JComboBox) {
return;
}
// Do not install the shadow border when the contents
// has a preferred size less than or equal to 0.
// We can't use the size, because it is(0, 0) for new popups.
final Dimension contentsPrefSize = newContents.getPreferredSize();
if (contentsPrefSize.width <= 0 || contentsPrefSize.height <= 0) {
return;
}
for (Container p = newContents.getParent(); p != null; p = p.getParent()) {
if (p instanceof JWindow || p instanceof Panel) {
// Workaround for the gray rect problem.
p.setBackground(newContents.getBackground());
heavyWeightContainer = p;
break;
}
}
final JComponent parent = (JComponent) newContents.getParent();
oldOpaque = parent.isOpaque();
oldBorder = parent.getBorder();
parent.setOpaque(false);
parent.setBorder(SHADOW_BORDER);
// Pack it because we have changed the border.
if (heavyWeightContainer != null) {
heavyWeightContainer.setSize(heavyWeightContainer.getPreferredSize());
} else {
parent.setSize(parent.getPreferredSize());
}
}
private Component createNoComponent() {
JComponent noComponent = new JComponent() {};
noComponent.setSize(0, 0);
noComponent.setVisible(false);
return noComponent;
}
/**
* To override font, override createToolTip() on component creating tooltip.
* On creation of ToolTip, change to desired font.
* @param g
* @param c
*/
@Override
public void paint(Graphics g, JComponent c) {
String tipText = ((JToolTip)c).getTipText();
tipText = tipText == null ? "" : tipText;
String[] tipLines = tipText.split("\n");
Font font = c.getFont();
g.setFont(font);
FontMetrics metrics = g.getFontMetrics();
int fontHeight = metrics.getHeight();
int height = (fontHeight * tipLines.length) + 2;
int width = this.getWidestStringText(tipLines, metrics) + 10;
int fontSize = font.getSize();
fontSize = fontSize == 0 ? PGTUtil.DEFAULT_FONT_SIZE.intValue() : fontSize;
c.setFont(font.deriveFont(fontSize));
((JToolTip)c).setTipText(tipText);
Dimension size = new Dimension(width, height);
c.setSize(size);
c.getParent().setSize(size);
Insets insets = c.getInsets();
Rectangle paintTextR = new Rectangle(
insets.left,
insets.top,
size.width - (insets.left + insets.right),
size.height - (insets.top + insets.bottom));
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.black);
g.fillRect(insets.left,
insets.top,
size.width - (insets.left + insets.right),
size.height - (insets.top + insets.bottom));
g.setColor(Color.white);
for (int i = 0 ; i < tipLines.length; i++) {
g.drawString(tipLines[i], paintTextR.x + 5,
paintTextR.y + metrics.getAscent() + (i * (fontHeight)));
}
}