-4
 //calling class
     import javax.swing.JFrame;
      class jcheckkbox {
          public static void main(String args[]) {
jRadio roof = new jRadio();
 roof.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 roof.setSize(300, 200);
 roof.setVisible(true);



               //secondary class
     import java.awt.*;
     import javax.swing.*;
     import java.awt.event.*;



     public class jcheckbox extends JFrame {
            private JCheckBox cd;
            private JCheckBox md;
            private JTextField vcd;

     public jcheckbox() {
   super("Beer bar");
setLayout(new FlowLayout());

vcd = new JTextField("this is a code", 20);
vcd.setFont(new Font("Serif", Font.PLAIN, 22));
vcd.setToolTipText("yahoo");
add(vcd);

cd = new JCheckBox("bold");
md = new JCheckBox("italic");
add(md);
add(cd);

handler dahandler = new handler();
 cd.addItemListener(dahandler);
 md.addItemListener(dahandler);

   }
      private class handler implements ItemListener {
         public void itemStateChanged(ItemEvent event) {
    Font cool = null;
    if (md.isSelected() && cd.isSelected())
     cool = new Font("Serif", Font.BOLD + Font.ITALIC, 25);
    else if (md.isSelected())
     cool = new Font("Serif", Font.BOLD, 30);
    else if (md.isSelected())
     cool = new Font("Sans_Serif", Font.ITALIC, 30);

     vcd.setFont(cool);

  }}}

how to write a program in just one class i mean no need calling class for setsize or defaultcloseoperation etc because two classes are harder to compile when making a .jar or .exe out of it,i know there is another way but i want to use this method as it is a lot more easier to make buttons,textfields comboboxes with this method

2 Answers2

1

If your whole program is within a couple of hundred lines then you can create multiple classes within a file. A file is typically used to host one class, but you can have static classes withing the file
As per some of the comments it is bad practice to put everything in one class. A class should only do one thing and helps modularize your program.
As per your code sample above you are obviously a beginner. I would strongly recommend that you go to the Java Tutorial and take a look around.
If you have any further questions then Google for them, if they have not been answered, then feel free to post a question here.

Community
  • 1
  • 1
Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
  • ok so can you tell me how to make a jar file out of 2 classes in hand – Himanshu Saxena Mar 25 '12 at 14:48
  • @Himanshu are you using an IDE ? Most IDEs (Integrated Development Environments) Have helpers to create a jar file. if not the command is jar -cf jar-file file1.class file2.class filen.class – Romain Hippeau Mar 25 '12 at 15:06
0

I really didn't understand the questions but here is my answer as I understand first you can include the main method on your jcheckbox class. Second you can add this functions you hinted in the constructor

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);

public jcheckbox()() {
super("Beer bar");
setLayout(new FlowLayout());

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);

vcd = new JTextField("this is a code", 20);
vcd.setFont(new Font("Serif", Font.PLAIN, 22));
vcd.setToolTipText("yahoo");
add(vcd);

cd = new JCheckBox("bold");
md = new JCheckBox("italic");
add(md);
add(cd);


handler dahandler = new handler();
cd.addItemListener(dahandler);
md.addItemListener(dahandler);

}
Ibraheem Osama
  • 324
  • 3
  • 6