Você está na página 1de 6

import javax.swing.

JFrame;
public class FontViewer2 {
public static void main(String[] args)
{
JFrame frame = new FontViewer2Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}
import
import
import
import
import
import
import
import
import
import

java.awt.BorderLayout;
java.awt.Font;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
javax.swing.JPanel;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JMenu;
javax.swing.JMenuBar;
javax.swing.JMenuItem;

public class FontViewer2Frame extends JFrame {


private static final int FRAME_WIDTH=300;
private static final int FRAME_HEIGHT=400;
private JLabel sampleField;
private String facename;
private int fontstyle;
private int fontsize;
public FontViewer2Frame()
{
sampleField=new JLabel("Big Java");
add(sampleField,BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(createFileMenu());
menuBar.add(createFontMenu());
facename ="Serif";
fontsize =24;
fontstyle = Font.PLAIN;
setSampleFont();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public JMenu createFileMenu()
{
JMenu menu = new JMenu("File");
menu.add(createFileExitItem());
return menu;

}
public JMenuItem createFileExitItem()
{
JMenuItem item = new JMenuItem("Exit");
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;
}
public JMenu createFontMenu()
{
JMenu menu = new JMenu("Font");
menu.add(createFaceMenu());
menu.add(createSizeMenu());
menu.add(createStyleMenu());
return menu;
}
public JMenu createFaceMenu()
{
JMenu menu = new JMenu("Face");
menu.add(createFaceItem("Serif"));
menu.add(createFaceItem("SansSerif"));
menu.add(createFaceItem("Monospaced"));
return menu;
}
public JMenu createSizeMenu()
{
JMenu menu = new JMenu("Size");
menu.add(createSizeItem("Smaller", -1));
menu.add(createSizeItem("Larger", 1));
return menu;
}

public JMenu createStyleMenu()


{
JMenu menu = new JMenu("Style");
menu.add(createStyleItem("Plain", Font.PLAIN));
menu.add(createStyleItem("Bold", Font.BOLD));
menu.add(createStyleItem("Italic", Font.ITALIC));
menu.add(createStyleItem("Bold Italic", Font.BOLD + Font.ITALIC));
return menu;
public JMenuItem createFaceItem(final String name)
{

JMenuItem item = new JMenuItem(name);


class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
facename = name;
setSampleFont();
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;

public JMenuItem createSizeItem(String name, final int ds)


{
JMenuItem item = new JMenuItem(name);
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
fontsize = fontsize + ds;
setSampleFont();
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;
}
public JMenuItem createStyleItem(String name, final int style)
{
JMenuItem item = new JMenuItem(name);
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
fontstyle = style;
setSampleFont();
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;
}
public void setSampleFont()
{
Font f = new Font(facename, fontstyle, fontsize);
sampleField.setFont(f);
sampleField.repaint();
}
}

Ndrtoni nj aplikacion grafik me menu Ngjyre q ka si elemente menush E


kuqe,Jeshile dhe Blu si dhe nj panel n qendr. Paneli n qendr t ndryshoj
ngjyr sipas zgjedhjes s prdoruesit .
import
import
import
import

java.awt.BorderLayout;
java.awt.Color;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;

import
import
import
import
import

javax.swing.JFrame;
javax.swing.JMenu;
javax.swing.JMenuBar;
javax.swing.JMenuItem;
javax.swing.JPanel;

public class NdryshimNgjyre extends JFrame


{
private static final int FRAME_WIDTH=300;
private static final int FRAME_HEIGHT=400;
private String ngjyra;
private ActionListener listener;
JPanel panel;
public NdryshimNgjyre()
{
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
menubar.add(createNgjyra());
menubar.add(createFileMenu());
setSize(FRAME_WIDTH, FRAME_HEIGHT);
panel = new JPanel();
add(panel, BorderLayout.CENTER);
}
public JMenu createFileMenu()
{
JMenu menu = new JMenu("File");
menu.add(createFileExitItem());
return menu;
}
public JMenuItem createFileExitItem()
{
JMenuItem item = new JMenuItem("Exit");
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("menu is clicked");
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);

return item;
}
public JMenu createNgjyra()
{
JMenu menu = new JMenu("Ngjyra");
menu.add( krijoNgjyreKuqe("Kuqe", Color.RED));
menu.add( krijoNgjyreKuqe("Blu", Color.BLUE));
return menu;
}
public JMenuItem krijoNgjyreKuqe(String s, Color ngjyra)
{
JMenuItem item = new JMenuItem(s);
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//getContentPane().setBackground(ngjyra);
panel.setBackground(ngjyra);
System.out.println("u klikua ngjyre:"+ s);
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;
}
}

2. Ndrtoni nje program q paraqet nj numr random (deri n 10) drejtkndshash


n pozicionerandom. Siguroni butonat Me pak dhe Me shume. Sa her q
prdoruesi klikon tek Me pak numri duhet t prgjysmohet.

import
import
import
import
import

java.awt.BorderLayout;
java.awt.GridLayout;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.util.Random;

import
import
import
import

javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JPanel;

public class GjeneroDrejtkendesha extends JFrame


{
private JLabel label;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 600;

private JButton shume;


private JButton pak;
private JPanel panel;
private int vlera;
public GjeneroDrejtkendesha()
{
Random rr = new Random ();
vlera = rr.nextInt(10)+1;
label = new JLabel ("numri eshte :"+vlera);
add(label, BorderLayout.SOUTH);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
shume = new JButton ("Shume");
panel = new JPanel();
panel.add(shume);
add(panel, BorderLayout.NORTH);
pak = new JButton ("Pak");
//panel = new JPanel();
panel.add(pak);
add(panel, BorderLayout.CENTER);
ActionListener listener = new ButonListener();
shume.addActionListener(listener);
pak.addActionListener(listener);
}
private class ButonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o.equals(shume))
{
vlera = vlera*2;
label.setText("vlera e re eshte :"+vlera);
}
else
{
vlera=vlera/2;
label.setText("vlera e re eshte :"+vlera);
}
}
}
}