Engineeringserver.com

A community for computer science students & developers about software development, game development, game design, games, anime preview & reviews and more!


How to center your application on the screen?

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
Loading ... Loading ...
291 views

To center your application you can do this:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = this.getSize().width;
int h = this.getSize().height;
int x = (screenSize.width-w)/2;
int y = (screenSize.height-h)/2;
this.setLocation(x, y);

Or the way that I prefer to do it:
setLocationRelativeTo(null);

Both ways work fine and will center the application on your screen.


Your Ad Here

How to create a statusbar in a JFrame

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.5 out of 5)
Loading ... Loading ...
571 views

This is an example how to create a statusbar in a JFrame using Java

First create a JLabel component like this:
JLabel status = new JLabel("This is a statusBar example");

And add the following code below in your program:
status.setPreferredSize(new Dimension(100, 16));
getContentPane().add(status, BorderLayout.SOUTH);


Your Ad Here

Java menu’s in a JFrame

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
431 views

The code shows you how to create a menu in a JFrame.


import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuDemo extends JFrame{
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu(”Menu”);
JMenuItem openItem = new JMenuItem(”Open file”);
JMenuItem exitItem = new JMenuItem(”Exit”);
public MenuDemo(){
getContentPane().setLayout(null);
menuBar.add(menu);
menu.add(openItem);
menu.add(exitItem);
add(menuBar);
setJMenuBar(menuBar);
setTitle(”Menu demo”);
setSize(200,200);
setVisible(true);
}
public static void main(String[] args){
new MenuDemo();
}
}


Your Ad Here