Reputation: 106
So, I'm new to to the Java language, and I have come to study about the JFrame class. In a lot of tutorials, I see this way of creating a new JFrame window:
class Something extends JFrame{
public static void main(String[] args) {
new Something();
}
Something() {
... //Code here, the elements of the default JFrame window.
}
}
So my question is: I always have to give the specifications of the JFrame window in the constructor of my class? Isn't there another way? Or this way is only unfamiliar with me? Also, if I want to change the created JFrame later in the main() method, it seems like my code is a bit anarchic. Thanks for your answer!
Upvotes: 1
Views: 4489
Reputation: 17171
You can configure your JFrame the same way you can configure any object. If you have a reference to the JFrame, you can call its setters and all non-private methods on it.
Your question touches on the best-practice way to configure objects. I would look for frameworks that handle this for you. In general my belief is any important settings should be externally configurable in a properties file, or system properties (i.e. not code), and configuration should be done by a different object than the one being configured. You want your configuration to be centralized so that all windows look similar etc.
As a beginner to the language you could roll something small to do this for you:
public interface WindowConfigurer {
void configure(JFrame window);
}
public class DefaultWindowConfigurer implements WindowConfigurer {
private Font font = // Default font
public void configure(JFrame window) {
window.setFont(font);
}
public void setFont(Font font) {
this.font = font;
}
}
public class EntryPoint {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Something something = new Something();
WindowConfigurer windowConfigurer = new DefaultWindowConfigurer();
windowConfigurer.configure(something);
something.setVisible(true);
}
});
}
}
public class Something extends JFrame {
public Something() {
// .. add buttons, controls, etc.
}
}
The interface WindowConfigurer
sets up a contract that implementations will "configure" a given JFrame. You can create a default implementation like I've done. The great part of abstracting that configuration is that you may want different configurations, and you may even want to change the configuration at runtime. For example what if your user is elderly? You may want to change the font to be bigger or use high contrast colors. So you might make an implementation of WindowConfigurer
called WindowConfigurerHighContrast
that does anything necessary to the JFrame to make it easy to read. This is the same kind of separation of concerns of HTML and CSS. You should read up on MVC pattern.
For every JFrame in your application, you'll simply have to run configure()
to get the JFrame in the same style as every other window.
Upvotes: 3
Reputation: 4084
I find it more convenient to subclass JPanel, and create the JFrame in main:
public class MyClass extends JPanel {
private JFrame frame; // just so I have access to the main frame
public MyClass( JFrame f ) {
frame = f;
// other things here
}
public void close() {
// do things for closing
int status = <something>;
System.exit( status );
}
public static void main( final String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run() {
// Create and set up the window.
final JFrame jf = new JFrame();
jf.setName( "MyClass" );
final MyClass item = new MyClass( jf);
jf.add( item);
jf.pack();
jf.setVisible( true );
jf.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing( final WindowEvent arg0 ) {
item.close();
}
} );
}
} );
}
}
Upvotes: 0