/* * @(#)ScalePanel.java 1.0 03/08/31 * * You can modify the template of this file in the * directory ..\JCreator\Templates\Template_1\Project_Name.java * * You can also create your own project template by making a new * folder in the directory ..\JCreator\Template\. Use the other * templates as examples. * */ package myprojects.scalepanel;import javax.swing.*; import java.awt.*; import java.awt.event.*;public class ScalePanel extends JPanel { public ScalePanel() { setPreferredSize( new Dimension( 300, 200 ) ); setBackground( Color.white ); setLayout( null ); JTextField text = new JTextField( "haha" ); text.setEnabled( false ); text.setBounds( 50, 50, 100, 30 ); add( text ); setEnabled( false ); } private static void scaleComponent(Component comp, double scale) { int w = comp.getWidth(); int h = comp.getHeight(); comp.setSize((int) (w * scale), (int) (h * scale)); if(comp instanceof Container) { Container container = (Container) comp; Component[] comps = container.getComponents(); for(int i=0; i<comps.length; i++) scaleComponent(comps[i], scale); } } public static void main( String[] args ) { final JFrame f = new JFrame(); f.setSize( 500, 400 ); final ScalePanel panel = new ScalePanel(); final JScrollPane jsp = new JScrollPane( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ); jsp.getViewport().setView( panel ); Container pane = f.getContentPane(); pane.add( jsp ); JButton b = new JButton( "scale" ); b.addActionListener( new AbstractAction() { public void actionPerformed( ActionEvent e ) { double scale = 2; scaleComponent(panel, scale); } }); pane.add( b, BorderLayout.SOUTH ); f.show(); } }
* @(#)ScalePanel.java 1.0 03/08/31
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
package myprojects.scalepanel;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class ScalePanel extends JPanel
{
public ScalePanel()
{
setPreferredSize( new Dimension( 300, 200 ) );
setBackground( Color.white );
setLayout( null );
JTextField text = new JTextField( "haha" );
text.setEnabled( false );
text.setBounds( 50, 50, 100, 30 );
add( text );
setEnabled( false );
} private static void scaleComponent(Component comp, double scale)
{
int w = comp.getWidth();
int h = comp.getHeight();
comp.setSize((int) (w * scale), (int) (h * scale));
if(comp instanceof Container)
{
Container container = (Container) comp;
Component[] comps = container.getComponents();
for(int i=0; i<comps.length; i++)
scaleComponent(comps[i], scale);
}
} public static void main( String[] args )
{
final JFrame f = new JFrame();
f.setSize( 500, 400 );
final ScalePanel panel = new ScalePanel();
final JScrollPane jsp = new JScrollPane( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
jsp.getViewport().setView( panel );
Container pane = f.getContentPane();
pane.add( jsp ); JButton b = new JButton( "scale" );
b.addActionListener( new AbstractAction() {
public void actionPerformed( ActionEvent e )
{
double scale = 2;
scaleComponent(panel, scale);
}
});
pane.add( b, BorderLayout.SOUTH ); f.show();
}
}