Programming Java

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

OBJECT-ORIENTED PROGRAMMING

GUI JAVA SWING LABORATORY

JPANEL, LAYOUT MANAGER, JBUTTON, JLABEL, JTEXTFIELD

CREATING A PROJECT

File->New Project->We choose to create a Java (1) project and the SDK (2) version, go next (3) and in the next window enter the project name (4) and create it (5).


lab1

In the next step, you need to create Swing UI Designer GUI From. In the next window, we enter the name and we can define the base layout.


lab1

View after creating GUI:
lab1

JFRAME
lab1

public class Example01 extends JFrame {
  private JPanel JPanel1;
  public static void main(String[] args) {
    Example01 SwingExample = new Example01();
    SwingExample.setVisible(true); //*wyświetlanie ramki *//*
  }
  //konstruktor
  public Example01(){
    //I sposób poprzez dziedzczenie z JFrame
    super("Moje pierwsze okieno :) ");
    this.setContentPane(this.JPanel1); // wyświetlenei na ekranie
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//metoda pozwalająca na
    zamknięcie okna
    //this.pack();
    /*upakowanie okna zgodnie z preferowanymi rozmiarami komponentów,
    które są zawarte w Frame, rozmair będzie się doaposowywał do rozmiaru */
  }
}

LAYOUT MANAGER

In Java, we can place components in the following layout:

a) BorderLayout
lab1

b) CardLayout
lab1

c) FlowLayout
lab1

d) GridBagLayout
lab1

e) GridLayoutManager IntelliJ Allows you to place components in a grid of columns and rows, and their size can be different.
lab1

JBUTTON public class JButton extends AbstractButton implements Accessible Commonly used Constructors:

Constructor Description   ——————- ——————————————————-
JButton() It creates a button with no text and icon.      
JButton(String s) It creates a button with the specified text.      
JButton(Icon i) It creates a button with the specified icon object.      

Commonly used Methods of AbstractButton class:

Methods Description
void setText(String s) It is used to set specified text on button.
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void addActionListener(ActionListener a) It is used to add the action listener to this object.


lab1

Example 1.

You need to create two buttons that will allow you to exit the program and display information.


lab1

After adding the button, use Create Listener Ctlt+O, then select ActionListener, and then automatically create the actionPerformed method

Method codes:

button_msg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Hello");
 }
});
buttonClose.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 dispose(); //output method
 }
});
buttonDate.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 //System.out.println(new Date());
 JOptionPane.showMessageDialog(null,new Date());
 }
});

JLABEL public class JLabel extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:

Constructor Description
JLabel() Creates a JLabel instance with no image and with an empty string for the title.
JLabel(String s) Creates a JLabel instance with the specified text.
JLabel(Icon i) Creates a JLabel instance with the specified image.
JLabel(String s, Icon i, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment.

Commonly usedMethods:

Methods Description
String getText() It returns the text string that a label displays.
void setText(String text) It defines the single line of text this component will display.
void setHorizontalAlignment(int alignment) It sets the alignment of the label’s contents along the X axis.
Icon getIcon() It returns the graphic image that the label displays.
int getHorizontalAlignment() It returns the alignment of the label’s contents along the X axis.

Example 2.

You should create a GUI according to the drawing below and implement the following events:

• The Close button closes the window.

• Display the date – displays the current date in the JLabel.


lab1

JTEXTFIELD public class JTextField extends JTextComponent implements SwingConstants Commonly used Constructors:

Constructor Description
JTextField() Creates a new TextField
JTextField(String text) Creates a new TextField initialized with the specified text.
JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns.
JTextField(int columns) Creates a new empty TextField with the specified number of columns.

Commonly usedMethods:

Methods Description
void addActionListener(ActionListener l) It is used to add the specified action listener to receive action events from this textfield.
Action getAction() It returns the currently set Action for this ActionEvent source, or null if no Action is set.
void setFont(Font f) It is used to set the current font.
void removeActionListener(ActionListener l) It is used to remove the specified action listener so that it no longer receives action events from this textfield.

Example 3.

After entering a and b and pressing the sum or difference button, the result of the operation will appear, close Closes the window.
lab1

Task 1.

_A GUI application for converting Celsius to Farenheit should be proposed.

The application should have the following functionalities:_

• Typing temp in Celsius

• The field in which the result will appear after pressing the convert button.

Task 2.

Using the components you have learned, you should design an application/clique that will demonstrate the use of available methods for the components discussed.