Total members 11801 |It is currently Wed Oct 23, 2019 6:01 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





BoxLayout Example Java
java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.BoxLayout;
import com.sun.java.swing.JComponent;
import com.sun.java.swing.JPanel;

public class BoxLayoutDemo {
static int NUM_SQUARES = 5;
static float[] alignment = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};
static float[] hue = {0.0f, 0.2f, 0.4f, 0.6f, 0.8f};

public static void main(String[] args) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

/* Create the squares. */
Square square = null;
for (int i = 0; i < BoxLayoutDemo.NUM_SQUARES; i++) {
square = new Square();
square.setAlignmentX(alignment[i]);
square.setAlignmentY(0.5f);
square.normalHue = Color.getHSBColor(hue[i], 0.4f, 0.85f);
panel.add(square);
}

/* Create the instructions. */
Label label = new Label("Click a box to change its X alignment.");

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};

Frame f = new Frame("Test Box Layout");
f.add("Center", panel);
f.add("South", label);
f.addWindowListener(l);
f.pack();
f.show();
}
}


class Square extends JComponent {
public Color normalHue;
private final Dimension preferredSize;

public Square() {
int pixelsPerSide = (int)(50.0 * Math.random()) + 50;
preferredSize = new Dimension(pixelsPerSide, pixelsPerSide);

MouseListener l = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Dimension size = getSize();
float alignment = (float)(e.getX())
/ (float)size.height;

// Round to the nearest 1/20th.
int tmp = Math.round(alignment * 20.0f);
alignment = (float)tmp / 20.0f;

setAlignmentX(alignment);
invalidate();
getParent().validate();
}
};
addMouseListener(l);
}

public boolean isOpaque() {
return true;
}

public void paint(Graphics g) {
Dimension size = getSize();
float alignmentX = getAlignmentX();

g.setColor(normalHue);
g.fill3DRect(0, 0, size.width, size.height, true);

/* Draw a horizontal white line at the alignment point.*/
g.setColor(Color.white);
int x = (int)(alignmentX * (float)size.height) - 1;
g.drawLine(x, 0, x, size.height - 1);

/* Say what the alignment point is. */
g.setColor(Color.black);
g.drawString(Float.toString(alignmentX), 3, size.height - 3);
}

public Dimension getPreferredSize() {
return preferredSize;
}

public Dimension getMaximumSize() {
return preferredSize;
}

public Dimension getMinimumSize() {
return preferredSize;
}
}





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : BoxLayout usage example - Box-Layout
 containers use a border Layout as their default layout     -  
 Frame with Null Layout- set layout NULL     -  
 specify a container's layout     -  
 Layout with Nested JPanels     -  
 FlowLayout with window-Buttons Layout Example     -  
 Window-Frame-with Diagonal Layout     -  
 Slice and Export a Website Layout     -  
 Java's layout managers provide over traditional windowing     -  
 Usage of ins tag     -  
 usage of kbd tag     -  



Topic Tags

Java Swing






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com