0% found this document useful (0 votes)
8 views5 pages

Lab 2

The document contains two Java Swing programs. The first program allows users to change the font size and color of the text 'ACEM' using combo boxes. The second program displays different phases of the moon based on the user's selection from a combo box, showing corresponding images for each phase.

Uploaded by

techpanause
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)
8 views5 pages

Lab 2

The document contains two Java Swing programs. The first program allows users to change the font size and color of the text 'ACEM' using combo boxes. The second program displays different phases of the moon based on the user's selection from a combo box, showing corresponding images for each phase.

Uploaded by

techpanause
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/ 5

1.

Write a program using Swing to display ACEM and change its font color and size by selecting it from
the combo box.

package labb2;

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

public class FontChanger {

public static void main(String[] args) {

JFrame frame = new JFrame("Font Changer");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

JPanel panel = new JPanel();


panel.setLayout(new GridLayout(3, 1));

JLabel acemLabel = new JLabel("ACEM", SwingConstants.CENTER);


acemLabel.setFont(new Font("Times New Roman", Font.BOLD, 29));
panel.add(acemLabel);

JPanel sizePanel = new JPanel();


sizePanel.add(new JLabel("Font Size:"));
Integer[] fontSizes = {12, 16, 20, 24, 28, 32, 36, 40};
JComboBox<Integer> fontSizeComboBox = new JComboBox<>(fontSizes);
fontSizeComboBox.setSelectedItem(24);
sizePanel.add(fontSizeComboBox);
panel.add(sizePanel);

JPanel colorPanel = new JPanel();


colorPanel.add(new JLabel("Font Color:"));
String[] colors = {"Black", "Red", "Blue", "Green"};
JComboBox<String> colorComboBox = new JComboBox<>(colors);
colorComboBox.setSelectedItem("Black");
colorPanel.add(colorComboBox);
panel.add(colorPanel);

frame.add(panel);
frame.setVisible(true);

fontSizeComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int newSize = (int) fontSizeComboBox.getSelectedItem();
acemLabel.setFont(new Font(acemLabel.getFont().getName(), Font.PLAIN,
newSize));
}
});

colorComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String color = (String) colorComboBox.getSelectedItem();
switch (color) {
case "Red":
acemLabel.setForeground(Color.RED);
break;
case "Blue":
acemLabel.setForeground(Color.BLUE);
break;
case "Green":
acemLabel.setForeground(Color.GREEN);
break;
default:
acemLabel.setForeground(Color.BLACK);
break;
}
}
});
}
}

OUTPUT:

Before

After
2. Write a program using Swing to display different phases of moon when selected from the combo box.

package lab2B;

import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LAB2B extends JFrame {

private static final long serialVersionUID = 1L;


private JPanel contentPane;
private JComboBox<String> comboBox;
private JLabel moonLabel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LAB2B frame = new LAB2B();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LAB2B() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new FlowLayout());

setContentPane(contentPane);

String[] phases = {"phase1", "phase2", "phase3"};


comboBox = new JComboBox<>(phases);
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedPhase = (String) comboBox.getSelectedItem();
displayMoonPhase(selectedPhase);
}
});
contentPane.add(comboBox);

moonLabel = new JLabel();


contentPane.add(moonLabel);
}

private void displayMoonPhase(String phase) {


ImageIcon icon = null;
switch (phase) {
case "phase1":
icon = new
ImageIcon("C:\\Users\\hemc6\\Downloads\\Crescent_Moon.jpeg");

break;
case "phase2":
icon = new ImageIcon("C:\\Users\\hemc6\\Downloads\\images.jpeg");
break;
case "phase3":
icon = new ImageIcon("C:\\Users\\hemc6\\Downloads\\full-moon.jpg");
break;
}
Image image = icon.getImage();
Image newimg = image.getScaledInstance(200, 200,
java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
moonLabel.setIcon(icon);
}
}

OUTPUT:

You might also like