Java Code Examples for javax.swing.JFrame#getWidth()

The following examples show how to use javax.swing.JFrame#getWidth() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: FCMFrame   File: FrameTool.java    License: Apache License 2.0 6 votes vote down vote up
/**
   * 屏幕居中方法
   */
  public static void setCenter(JFrame jframe) {
      Toolkit kit = Toolkit.getDefaultToolkit();
      Dimension screenSize = kit.getScreenSize();
      int screenHeight = screenSize.height;
      int screenWidth = screenSize.width;
      jframe.setSize(screenWidth*2/3, screenHeight*2/3);
      int frameH = jframe.getHeight();
      int frameW = jframe.getWidth();
      jframe.setLocation((screenWidth - frameW) / 2, (screenHeight - frameH) / 2);
try {
	String src = "images/logo.gif";
	File f = new File(src);
	Image image = ImageIO.read(f);
	jframe.setIconImage(image);
} catch (IOException e) {
	e.printStackTrace();
}
  }
 
Example 2
Source Project: Java   File: UiUtil.java    License: Artistic License 2.0 6 votes vote down vote up
/**
 * 设置窗体居中
 * @param jf 
 */
public static void setFrameCenter(JFrame jf){
    /*
    思路:
        获取屏幕的宽和高
        获取窗体的宽和高
    */
    //获取工具类
    Toolkit took = Toolkit.getDefaultToolkit();
    
    //获取屏幕的宽和高
    Dimension screen = took.getScreenSize();
    
   double screenWidth =  screen.getWidth();
   double screenHeight = screen.getHeight();
   
   //获取窗体的宽和高
   int jfWidth = jf.getWidth();
   int jfHeight = jf.getHeight();
   
   //设置坐标
   int width = (int)(screenWidth-jfWidth)/2;
   int height = (int)(screenHeight-jfHeight)/2;
   
  jf.setLocation(width,height);
}
 
Example 3
Source Project: Java   File: UiUtil.java    License: Artistic License 2.0 6 votes vote down vote up
/**
 * 设置窗体居中
 * @param jf 
 */
public static void setFrameCenter(JFrame jf){
    /*
    思路:
        获取屏幕的宽和高
        获取窗体的宽和高
    */
    //获取工具类
    Toolkit took = Toolkit.getDefaultToolkit();
    
    //获取屏幕的宽和高
    Dimension screen = took.getScreenSize();
    
   double screenWidth =  screen.getWidth();
   double screenHeight = screen.getHeight();
   
   //获取窗体的宽和高
   int jfWidth = jf.getWidth();
   int jfHeight = jf.getHeight();
   
   //设置坐标
   int width = (int)(screenWidth-jfWidth)/2;
   int height = (int)(screenHeight-jfHeight)/2;
   
  jf.setLocation(width,height);
}
 
Example 4
Source Project: Java   File: UiUtil.java    License: Artistic License 2.0 6 votes vote down vote up
/**
 * 设置窗体居中
 * @param jf 
 */
public static void setFrameCenter(JFrame jf){
    /*
    思路:
        获取屏幕的宽和高
        获取窗体的宽和高
    */
    //获取工具类
    Toolkit took = Toolkit.getDefaultToolkit();
    
    //获取屏幕的宽和高
    Dimension screen = took.getScreenSize();
    
   double screenWidth =  screen.getWidth();
   double screenHeight = screen.getHeight();
   
   //获取窗体的宽和高
   int jfWidth = jf.getWidth();
   int jfHeight = jf.getHeight();
   
   //设置坐标
   int width = (int)(screenWidth-jfWidth)/2;
   int height = (int)(screenHeight-jfHeight)/2;
   
  jf.setLocation(width,height);
}
 
Example 5
private static void showUI() {
    frame = new JFrame();
    frame.setSize(new Dimension(400, 400));
    frame.setLocationRelativeTo(null);

    final Container content = frame.getContentPane();
    content.setLayout(new BorderLayout(0, 0));
    Container lastPanel = content;
    for (int i = 0; i < 500; i++) {
        final JPanel p = new JPanel();
        p.setLayout(new BorderLayout(0, 0));
        lastPanel.add(p);
        lastPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("click");
                go.countDown();
            }
        });
        lastPanel = p;
    }

    lastPanel.setBackground(Color.GREEN);
    frame.setVisible(true);

    Point loc = frame.getLocationOnScreen();
    center.x = loc.x + frame.getWidth() / 2;
    center.y = loc.y + frame.getHeight() / 2;
}
 
Example 6
private static void showUI() {
    frame = new JFrame();
    frame.setSize(new Dimension(400, 400));
    frame.setLocationRelativeTo(null);

    final Container content = frame.getContentPane();
    content.setLayout(new BorderLayout(0, 0));
    Container lastPanel = content;
    for (int i = 0; i < 500; i++) {
        final JPanel p = new JPanel();
        p.setLayout(new BorderLayout(0, 0));
        lastPanel.add(p);
        lastPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("click");
                go.countDown();
            }
        });
        lastPanel = p;
    }

    lastPanel.setBackground(Color.GREEN);
    frame.setVisible(true);

    Point loc = frame.getLocationOnScreen();
    center.x = loc.x + frame.getWidth() / 2;
    center.y = loc.y + frame.getHeight() / 2;
}
 
Example 7
private static void showUI() {
    frame = new JFrame();
    frame.setSize(new Dimension(400, 400));
    frame.setLocationRelativeTo(null);

    final Container content = frame.getContentPane();
    content.setLayout(new BorderLayout(0, 0));
    Container lastPanel = content;
    for (int i = 0; i < 500; i++) {
        final JPanel p = new JPanel();
        p.setLayout(new BorderLayout(0, 0));
        lastPanel.add(p);
        lastPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("click");
                go.countDown();
            }
        });
        lastPanel = p;
    }

    lastPanel.setBackground(Color.GREEN);
    frame.setVisible(true);

    Point loc = frame.getLocationOnScreen();
    center.x = loc.x + frame.getWidth() / 2;
    center.y = loc.y + frame.getHeight() / 2;
}
 
Example 8
public void set(JFrame window) {
    if (window != null) {
        // Save one window's settings
        String name = window.getClass().getSimpleName();
        Rectangle r = rectangle (name);
        r.x = window.getX();
        r.y = window.getY();
        r.width = window.getWidth();
        r.height = window.getHeight();
    }
}
 
Example 9
private static void showUI() {
    frame = new JFrame();
    frame.setSize(new Dimension(400, 400));
    frame.setLocationRelativeTo(null);

    final Container content = frame.getContentPane();
    content.setLayout(new BorderLayout(0, 0));
    Container lastPanel = content;
    for (int i = 0; i < 500; i++) {
        final JPanel p = new JPanel();
        p.setLayout(new BorderLayout(0, 0));
        lastPanel.add(p);
        lastPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("click");
                go.countDown();
            }
        });
        lastPanel = p;
    }

    lastPanel.setBackground(Color.GREEN);
    frame.setVisible(true);

    Point loc = frame.getLocationOnScreen();
    center.x = loc.x + frame.getWidth() / 2;
    center.y = loc.y + frame.getHeight() / 2;
}
 
Example 10
/**
 * 
 * frame������
 * 
 * @param jf
 */
public static void setFrameCenter (JFrame jf) {
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Dimension screen = toolkit.getScreenSize();// �����ݶ���
	int x = (screen.width - jf.getWidth()) / 2;
	int y = (screen.height - jf.getHeight()) / 2 - 32;
	jf.setLocation(x, y);
}
 
Example 11
Source Project: swift-explorer   File: SwiftExplorer.java    License: Apache License 2.0 5 votes vote down vote up
private static void openMainWindow(final MainPanel cp) throws IOException {
    JFrame frame = new JFrame(Configuration.INSTANCE.getAppName());
    
    Dimension screenSize =  java.awt.Toolkit.getDefaultToolkit().getScreenSize() ;
    
    float ratio = (float) 0.8 ;
    Dimension windowSize = new Dimension ((int)(screenSize.getWidth() * ratio), (int)(screenSize.getHeight() * ratio)) ;

    frame.setSize(windowSize.getSize());
    frame.setLocationByPlatform(true);
    frame.setIconImage(ImageIO.read(SwiftExplorer.class.getResource("/icons/logo.png")));
    frame.getContentPane().add(cp);
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if (cp.onClose()) {
                System.exit(0);
            }
        }
    });
    cp.setOwner(frame);
    frame.setJMenuBar(cp.createMenuBar());
    
    // center the frame
    int x = (int) ((screenSize.getWidth() - frame.getWidth()) / 2);
    int y = (int) ((screenSize.getHeight() - frame.getHeight()) / 2);
    frame.setLocation(x, y);
    
    frame.setVisible(true);
}
 
Example 12
Source Project: aurous-app   File: Utils.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Center a frame on the main display
 *
 * @param frame
 *            The frame to center
 */
public static void centerFrameOnMainDisplay(final JFrame frame) {
	final GraphicsEnvironment ge = GraphicsEnvironment
			.getLocalGraphicsEnvironment();
	final GraphicsDevice[] screens = ge.getScreenDevices();
	if (screens.length < 1) {
		return; // Silently fail.
	}
	final Rectangle screenBounds = screens[0].getDefaultConfiguration()
			.getBounds();
	final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2);
	final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2);
	frame.setLocation(x, y);
}
 
Example 13
/**
 * Center a frame on the main display
 *
 * @param frame
 *            The frame to center
 */
public void centerFrameOnMainDisplay(final JFrame frame) {
	final GraphicsEnvironment ge = GraphicsEnvironment
			.getLocalGraphicsEnvironment();
	final GraphicsDevice[] screens = ge.getScreenDevices();
	if (screens.length < 1) {
		return; // Silently fail.
	}
	final Rectangle screenBounds = screens[0].getDefaultConfiguration()
			.getBounds();
	final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2);
	final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2);
	frame.setLocation(x, y);
}
 
Example 14
Source Project: aurous-app   File: Utils.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Center a frame on the main display
 *
 * @param frame
 *            The frame to center
 */
public static void centerFrameOnMainDisplay(final JFrame frame) {
	final GraphicsEnvironment ge = GraphicsEnvironment
			.getLocalGraphicsEnvironment();
	final GraphicsDevice[] screens = ge.getScreenDevices();
	if (screens.length < 1) {
		return; // Silently fail.
	}
	final Rectangle screenBounds = screens[0].getDefaultConfiguration()
			.getBounds();
	final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2);
	final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2);
	frame.setLocation(x, y);
}
 
Example 15
public static void savePrefs(JFrame frame, ColourChooser chooser, LayerManager layers)
{
	Preferences prefs = Preferences.userNodeForPackage(UserPreferences.class);
	
	if((frame.getExtendedState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH)
	{
		prefs.putBoolean(WINDOW_MAXIMIZED, true);
	}
	else
	{
		windowWidth = frame.getWidth();
		windowHeight = frame.getHeight();
		prefs.putInt(WINDOW_WIDTH, windowWidth);
		prefs.putInt(WINDOW_HEIGHT, windowHeight);
		prefs.putBoolean(WINDOW_MAXIMIZED, false);
	}
	colourPickerX = chooser.getX();
	colourPickerY = chooser.getY();
	colourPickerMode = chooser.getMode();
	prefs.putInt(COLOUR_PICKER_X, colourPickerX);
	prefs.putInt(COLOUR_PICKER_Y, colourPickerY);
	prefs.putInt(COLOUR_PICKER_MODE, colourPickerMode);
	prefs.putBoolean(COLOUR_PICKER_VISIBLE, chooser.isVisible());
	
	layersX = layers.dialog.getX();
	layersY = layers.dialog.getY();
	layersWidth = layers.dialog.getWidth();
	layersHeight = layers.dialog.getHeight();
	prefs.putInt(LAYERS_X, layersX);
	prefs.putInt(LAYERS_Y, layersY);
	prefs.putInt(LAYERS_WIDTH, layersWidth);
	prefs.putInt(LAYERS_HEIGHT, layersHeight);
	prefs.putBoolean(LAYERS_VISIBLE, layers.isVisible());
	
	prefs.putBoolean(COLOUR_PICKER_VISIBLE, chooser.isVisible());
	
	prefs.putBoolean(BACKGROUND_DARK, Menu.DARK_BACKGROUND);
}
 
Example 16
public static void main(String[] args) throws IOException {
	TreeJPanel tjp = new TreeJPanel();
	String ptbTreeString = "(NP-2 (NP-1 (QP-1 (CD-1 One) (JJR-1 more)) (NN-2 try)) (PP-0 (IN-1 with) (NP-2 (NP-1 (NN-1 something)) (ADVP-0 (RB-1 longer)))))";// "(ROOT (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (NN test))) (. .)))";
	if (args.length > 0) {
		ptbTreeString = args[0];
	}
	Tree<String> tree = (new Trees.PennTreeReader(new StringReader(
			ptbTreeString))).next();// new StringReader(ptbTreeString), new
									// LabeledScoredTreeFactory(new
									// StringLabelFactory()))).readTree();
	tjp.setTree(tree);
	JFrame frame = new JFrame();
	frame.getContentPane().add(tjp, BorderLayout.CENTER);
	frame.addWindowListener(new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	});
	frame.pack();
	// Image img = frame.createImage(frame.getWidth(),frame.getHeight());
	frame.setVisible(true);
	frame.setVisible(true);
	frame.setSize(tjp.preferredX, tjp.preferredY);

	int t = 1;
	t++;

	BufferedImage bi = new BufferedImage(tjp.width(), tjp.height(),
			BufferedImage.TYPE_INT_ARGB);
	Graphics2D g2 = bi.createGraphics();
	// int rule = AlphaComposite.SRC_OVER;
	// AlphaComposite ac = AlphaComposite.getInstance(rule, 0f);
	// g2.setComposite(ac);

	g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 1.0f));
	Rectangle2D.Double rect = new Rectangle2D.Double(0, 0,
			frame.getWidth(), frame.getHeight());
	g2.fill(rect);

	g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
			1.0f));

	tjp.paintComponent(g2); // paint the graphic to the offscreen image
	// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
	// 1.0f));
	// g2.drawImage(src, null, 0, 0);
	// g2.dispose();

	ImageIO.write(bi, "png", new File("example.png")); // save as png format
														// DONE!

	// System.exit(1);

}