Remember to adhere to the
Java Naming Conventions, especially with your class name. That would mean gui -> Gui [And that isn't a very good name either]
Some possible class names:
- FrameTest
- XbatzFrame
- FirstFrame
- FrameExample
Also, that code should be called on the EDT, because it is very gui-based. Finally, you should comment your code! This would make it look a bit more like:
import javax.swing.*;
import java.awt.FlowLayout;
/**
* This class demonstrates how to create a basic
* JFrame. For example purposes, this frame will
* have a label saying "Hello World!"
*
* @see javax.swing.JFrame
* @author Timothy Moore
*/
public class FrameExample extends JFrame
{
/**
* Creates the frame example and makes it visible
*
* @throws AssertionError if not called on the EDT
*/
protected FrameExample()
{
setTitle("Frame Example");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Middle of the screen
prepareComponents(); // Throws the assertion error
setVisible(true); // This will paint the entire frame
}
/**
* Prepares the components for display.
*
* @throws AssertionError if not called on the EDT
*/
private void prepareComponents()
{
assert SwingUtilities.isEventDispatchThread();
setLayout(new FlowLayout(FlowLayout.CENTER));
add(new JLabel("Hello World!"));
}
/**
* @param args not used
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
FrameExample example = new FrameExample();
}
});
}
}
Hope you get the idea, I added the example because so few people put code on the Event Dispatch Thread these days. Note that I subclass JFrame, instead of create one using its constructor. This is a much more versatile technique.
Some links: