Friday, July 15

Swing starting Code

package swings;
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class FrameDesign extends JFrame
{
Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize();
FrameDesign()
{
setTitle("Simple Frame");
setBounds(scrSize.width/4,scrSize.height/4,scrSize.width/2,scrSize.height/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\a.png"));

JPanel panel= new panelDesign();
getContentPane().add(panel);
}
}

class panelDesign extends JPanel
{
private JMenuBar mb;
private JMenu File,Edit,View,Format,Help;
private JMenuItem Cut,Copy,Paste,New,Open,Save,SaveAs,Close,Exit,SelectAll,Delete;
private JTextArea ta;
private JComboBox cb1;
String Fontlist[]={"Arial","Calibri","Monotype Corsiva"};


panelDesign()
{
setBackground(Color.PINK);

mb=new JMenuBar();
File=new JMenu("File");
Edit=new JMenu("Edit");
View=new JMenu("View");
Format=new JMenu("Format");
Help=new JMenu("Help");
Cut=new JMenuItem("Cut");
Copy=new JMenuItem("Copy");
Paste=new JMenuItem("Paste");
New = new JMenuItem("New");
Open = new JMenuItem("Open");
Save = new JMenuItem("Save");
SaveAs= new JMenuItem("SaveAs");
Exit=new JMenuItem("Exit");
SelectAll= new JMenuItem("SelectAll");
Delete= new JMenuItem("Delete");
cb1=new JComboBox(Fontlist);
ta=new JTextArea();

mb.add(File);
mb.add(Edit);
mb.add(View);
mb.add(Format);
mb.add(Help);


File.add(New);
File.add(Open);
File.add(Save);
File.add(SaveAs);
File.add(Exit);
Edit.add(Cut);
Edit.add(Copy);
Edit.add(Paste);
Edit.add(SelectAll);
Edit.add(Delete);
ta.setColumns(70);
ta.setRows(20);

add(mb);
add(cb1);
add(ta);

Cut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{ CutActionPerformed(evt); }
});
Copy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
CopyActionPerformed(evt);
}
});
Paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
PasteActionPerformed(evt);
}
});
SelectAll.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
SelectAllActionPerformed(evt);
}
});
Delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
DeleteActionPerformed(evt);
}
});

}
private void CutActionPerformed(ActionEvent evt)
{ ta.cut(); }
private void CopyActionPerformed(ActionEvent evt)
{ ta.copy(); }
private void SelectAllActionPerformed(ActionEvent evt)
{ ta.selectAll(); }
private void DeleteActionPerformed(ActionEvent evt)
{ ta.replaceSelection(""); }
private void PasteActionPerformed(ActionEvent evt)
{ ta.paste(); }

}

public class Main {

public static void main(String args[])
{
FrameDesign f = new FrameDesign();
f.setVisible(true);
}

}

No comments: