0% found this document useful (0 votes)
6 views2 pages

Swing

The document contains a Java Swing application for managing a movie collection, featuring a user interface with fields for title, year, length, and genre. It includes buttons for clearing the fields, printing output, and closing the application, as well as an event listener that responds to year selection. The main functionality is encapsulated in the MovieManagementUI class, which initializes the UI components and sets up the layout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Swing

The document contains a Java Swing application for managing a movie collection, featuring a user interface with fields for title, year, length, and genre. It includes buttons for clearing the fields, printing output, and closing the application, as well as an event listener that responds to year selection. The main functionality is encapsulated in the MovieManagementUI class, which initializes the UI components and sets up the layout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package klausur.

swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class MovieManagementUI extends JFrame {

private JTextField title;


private JComboBox<Integer> year;
private JTextField length;
private JComboBox<String> genre;

private MovieManagementUI() {
this.setTitle("My Movie Collection");

JPanel mainPanel = new JPanel(new GridLayout(0, 3));

title = new JTextField(15);


Integer[] yearValues = {2015, 2016, 2017, 2018, 2019, 2020};
year = new JComboBox<Integer>(yearValues);
length = new JTextField(3);
String[] genreValues =
{"Action", "Horror", "Komödie", "Liebesfilm"};
genre = new JComboBox<String>(genreValues);

//initialize Values
title.setText("The Avengers: Endgame");
year.setSelectedItem(2019);
length.setText("182");
genre.setSelectedItem("Action");

JButton clearButton = new JButton("Clear");


JButton printButton = new JButton("Ausgabe");
JButton closeButton = new JButton("Schließen");

mainPanel.add(wrapComponentOnFlowPanel(new JLabel("Titel:")));
mainPanel.add(wrapComponentOnFlowPanel(title));
mainPanel.add(new JPanel());
mainPanel.add(wrapComponentOnFlowPanel(new JLabel("Jahr:")));
mainPanel.add(wrapComponentOnFlowPanel(year));
mainPanel.add(printButton);
mainPanel.add(wrapComponentOnFlowPanel(new JLabel("Dauer:")));
mainPanel.add(length);
mainPanel.add(wrapComponentOnFlowPanel(closeButton));
mainPanel.add(wrapComponentOnFlowPanel(new JLabel("Genre:")));
mainPanel.add(wrapComponentOnFlowPanel(genre));
mainPanel.add(wrapComponentOnFlowPanel(clearButton));

this.add(mainPanel);

year.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int releaseYear = (int) e.getItem();
if (releaseYear < 1980) {
System.out.println("Richtig alter Film!");
}
}
}
});

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);

public static void main(String[] args) {


new MovieManagementUI();
}

private JPanel wrapComponentOnFlowPanel(Component component) {


int layoutAlignment = (component instanceof JButton)
? FlowLayout.CENTER : FlowLayout.LEFT;
JPanel panel = new JPanel(new FlowLayout(layoutAlignment));
panel.add(component);
return panel;
}

You might also like