Vix Blog
No Result
View All Result
  • Economia
  • Educação
  • Segurança
  • Mundo
  • Negócios
  • Notícias
  • Tecnologia
  • DMCA
NEWSLETTER
  • Economia
  • Educação
  • Segurança
  • Mundo
  • Negócios
  • Notícias
  • Tecnologia
  • DMCA
No Result
View All Result
Vix Blog
No Result
View All Result
Home Entretenimento

How to Write Your First Java GUI Application 

Carolina por Carolina
25 de março de 2022
Tempo de leitura:5min de leitura
0

Java uses a set of features provided by JFC (Java Foundation Classes) to create graphical user interfaces (GUIs). JFCs are part of the Java platform and are therefore cross-platform.

These classes provide features such as pluggable look-and-feel, internationalization, and Swing GUI components.

This tutorial concentrates on how to use Swing components to create your first Java GUI application.

Swing Components

Swing is a JFC package that contains lightweight GUI components. It has 18 public packages. Fortunately, most of your applications will only need a few of them. In privado, javax.swing and javax.swing.event (less often).

For your application to appear on-screen, all the GUI components need to be part of a containment hierarchy. A containment hierarchy is a tree of components whose root is a top-level container.

Swing has three top-level container classes:

  • JFrame, used to create the main window/frame
  • JDialog, used to create dialog boxes
  • JApplet, used to add Swing components to applets

The focus of this tutorial will be how to use the JFrame top-level container. Each top-level container generally consists of a content pane and optionally, a menu bar.

Related: Types of Programming Errors and How to Avoid Them

The content pane is a space into which you can place visible components of a top-level container. A menu bar contains a list of items on your GUI.

MAKEUSEOF VIDEO OF THE DAY

Creating a Frame

A frame is the main window of your application and generally has a title and borders. To create a frame, use the JFrame container class:

JFrame frame = new JFrame("My Title");

The class has a constructor that allows you to define the title of your window. However, instantiating a frame alone is not enough to make your window appear on-screen.

You need to set the setVisible() method to true for your application to appear. With this minimal code, your application can now appear:

import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("HelloWorld");
frame.setVisible(true);
}
}

If you ran the code above, you most probably took at least a minute darting around your screen while looking for the application window. You most probably found a barely visible window at the edge of your computer screen.

Apart from this, you’ll notice that the program doesn’t stop running when you click the cancel button on your application window. You can notice this from your command line.

Related: A Beginner’s Guide to Using Interfaces in Java

The window will close, but the program will continue running in the background. This is because the default behavior is to hide the frame on exit. More useful behavior might be to exit the program or shift control to another program.


To solve these two issues, you’ll need to use the setSize() and setDefaultCloseOperation() methods. See the code below:

import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("HelloWorld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}

In the example above, the window-closing event defined is EXIT_ON_CLOSE. This means that your program will exit the application when you close the frame. There are other window-closing events that you can use:

RELATED POSTS

Uma vez que parar de compartilhar recibos de leitura no iMessage

Os 4 melhores aplicativos para iPhone para fãs da Terreno-média

As 5 melhores ferramentas para um trabalho produtivo

  • HIDE_ON_CLOSE: This is the default operation which hides the window on closing, but leaves it displayable.
  • DO_NOTHING_ON_CLOSE: This will perform no action when the user requests to close the window. Instead, a registered WindowListener handles closing with its windowClosing() method.
  • DISPOSE_ON_CLOSE: This hides the windows and also frees the resources the program had allocated to it.


Your First Python GUI Application

Now that you’ve learned how to create your first Java GUI application, it’s time to explore how to do so in Python. Python offers you the simplicity of less code to create your application.

It’s worth your time to learn how to use Python’s Tkinter library to create your GUI applications.


Desktop user interface with Python logo
Start Creating Desktop Apps in Python With the Tkinter GUI Library

Want more from your Python projects? Learn how to create desktop apps with Tkinter.

Read Next


About The Author

Jerome Davidson
(36 Articles Published)

Jerome is a Staff Writer at MakeUseOf. He covers articles on Programming and Linux. He’s also a crypto enthusiast and is always keeps tabs on the crypto industry.

More
From Jerome Davidson

Subscribe to our newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

Click here to subscribe

Novidades no seu e-mail!

Não enviamos spam! Leia mais em nossa Política de privacidade

Verifique sua caixa de entrada ou a pasta de spam para confirmar sua assinatura.

ShareTweetPin
Carolina

Carolina

Relacionado Posts

Uma vez que parar de compartilhar recibos de leitura no iMessage
Entretenimento

Uma vez que parar de compartilhar recibos de leitura no iMessage

24 de março de 2022
Os 4 melhores aplicativos para iPhone para fãs da Terreno-média
Entretenimento

Os 4 melhores aplicativos para iPhone para fãs da Terreno-média

24 de março de 2022
As 5 melhores ferramentas para um trabalho produtivo
Entretenimento

As 5 melhores ferramentas para um trabalho produtivo

24 de março de 2022
Uma vez que emendar o driver de vídeo parou de responder no Windows 11/10
Entretenimento

Uma vez que emendar o driver de vídeo parou de responder no Windows 11/10

24 de março de 2022
Uma selecção de destaque para Linux
Entretenimento

Uma opção de destaque para Linux

24 de março de 2022
Uma selecção de destaque para Linux
Entretenimento

Uma selecção de destaque para Linux

24 de março de 2022
Next Post

What Is the Wiper Malware? Is It Worse Than a Ransomware Attack?

You Can Now Queue Albums and Tracks on the Bandcamp Mobile App

You Can Now Queue Albums and Tracks on the Bandcamp Mobile App

Deixe um comentário Cancelar resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

No Result
View All Result

Últimas Notícias

7 maneiras de emendar a instrumento Windows Miracast quando ela funciona mal

24 de março de 2022
Porquê enganar aplicativos para pensar que seu Android não está enraizado

Porquê enganar aplicativos para pensar que seu Android não está enraizado

24 de março de 2022
Uma vez que parar de compartilhar recibos de leitura no iMessage

Uma vez que parar de compartilhar recibos de leitura no iMessage

24 de março de 2022
Porquê usar guias no Vivaldi para organizar sua navegação

Uma vez que usar gestos do mouse no Vivaldi para velejar mais rápido

24 de março de 2022
Uma vez que trabalhar em mansão sem experiência

Uma vez que trabalhar em mansão sem experiência

24 de março de 2022

Novidades no seu e-mail!

Não enviamos spam! Leia mais em nossa Política de privacidade

Verifique sua caixa de entrada ou a pasta de spam para confirmar sua assinatura.

Categorias

  • Entretenimento
  • Mundo
  • Segurança
Vix Blog

Somos entusiastas da tecnologia com a missão de ensinar ao mundo como usar e compreender a tecnologia em suas vidas.
SAIBA MAIS »

Entre em contato conosco enviando um e-mail para [email protected]

Posts recentes

  • 7 maneiras de emendar a instrumento Windows Miracast quando ela funciona mal
  • Porquê enganar aplicativos para pensar que seu Android não está enraizado
  • Uma vez que parar de compartilhar recibos de leitura no iMessage

Categorias

  • Entretenimento
  • Mundo
  • Segurança

Links Importantes

  • Quem Somos
  • Blog
  • Fale Conosco
  • Política de Privacidade
  • DMCA

© 2021 VixBlog - Notícias e dicas para o seu dia dia.

No Result
View All Result
  • Economia
  • Educação
  • Segurança
  • Mundo
  • Negócios
  • Notícias
  • Tecnologia
  • DMCA

© 2021 VixBlog - Notícias e dicas para o seu dia dia.