1 Write a shortsnippetofcodethatcreates a J Panel objectcall

1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, and creates a border around it containing the title \"this is a JPanel.\"

2) Write a keyPressed method that behaves as follows. If the user presses the up arrow, the method should output \"You pressed up\"using the System.out.printin method. Ifthe user presses the down arrow, the method should output \"You pressed down\" using the System.out.printin method.

3) Write a short class that represents a panelwith a single radio button that has the option \"Yes\"and the option \"No.\" Bydefault, the Yes buttonshould be checked.

4) Suppose we have created a class called MyGUI, which represents a GUI. Write a programthat creates a JFrame object, adds a MyGUI object to the frame and makes it visible.

5) Write a short class that represents a panelwith a single sliderthat has values from 0 to 250, with largetick marks in incrementsof 50 and smalltick marks in increments of 10.

Solution

1)

/**
* The java Demo1 that cretes a titled border
* for jpanel.
* */
//Demo1.java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class Demo1
{
   public static void main(String[] args)
   {
       JFrame frame=new JFrame();
      
       //Create an instance of Jpanel
       JPanel p1=new JPanel();
       //Create an instance of TitledBorder
       TitledBorder border = new TitledBorder(\"this is a JPanel\");
        border.setTitleJustification(TitledBorder.CENTER);
  
       //add border to panel ,p1
       p1.setBorder(border);
       //add panle to frame
       frame.add(p1);
       frame.setSize(250, 250);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }
}

2)
/**
* The java Demo2 that display a message
* for arrow up is pressed and arrow down
* key is pressed
*
* */
//Demo1.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class Demo2
{
   public static void main(String[] args)
   {
       JFrame frame=new JFrame();
      
       //Create an instance of Jpanel
       JPanel p1=new JPanel();
       //Create an instance of TitledBorder
       TitledBorder border = new TitledBorder(\"this is a JPanel\");
        border.setTitleJustification(TitledBorder.CENTER);
  
       //add border to panel ,p1
       p1.setBorder(border);
       p1.setFocusable(true);
       //Add a KeyListener interface to the panel,p1
       p1.addKeyListener(new KeyListener() {
          
           @Override
           public void keyTyped(KeyEvent e) {
               // TODO Auto-generated method stub
              
           }
          
           @Override
           public void keyReleased(KeyEvent e) {
               // TODO Auto-generated method stub
              
           }
          
           @Override
           /*Write to display pressed up
           and pressed down */
           public void keyPressed(KeyEvent e) {
               int keyCode = e.getKeyCode();
                switch( keyCode )
                {
                    case KeyEvent.VK_UP:
                        System.out.println(\"You pressed up\");
                        break;
                    case KeyEvent.VK_DOWN:
                       System.out.println(\"You pressed down\");
                        break;
                  
                 }
              
           }
       });
       //add panle to frame
       frame.add(p1);
       frame.setSize(250, 250);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }
}

3)


/**
* the java program that deomonstrates
* the radio buttons .
* */
//Demo3.java
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Demo3
{
   public static void main(String[] args)
   {
       JFrame frame=new JFrame(\"Demo3\");
      
       //Create an instance of Jpanel
       JPanel p1=new JPanel();
      
       ButtonGroup bg=new ButtonGroup();
       //Set by default Yes is checked
       JRadioButton yesbtn=new JRadioButton(\"Yes\",true);      
       JRadioButton nobtn=new JRadioButton(\"No\");
      
       //add button to the button group
       bg.add(yesbtn);      
       bg.add(nobtn);
      
       p1.add(yesbtn);
       p1.add(nobtn);
     
      
       //add panle to frame
       frame.add(p1);
       frame.setSize(250, 250);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }
}

4)


///MyGUI.java
//Dummy class
import javax.swing.JFrame;
public class MyGUI extends JFrame{

}

/*Code snipper that adds the instance of MyGUI
* class that extends the JFrame. */
//Demo4.java
import javax.swing.JFrame;
public class Demo4
{
   public static void main(String[] args)
   {
       JFrame frame=new JFrame(\"Demo3\");
       //Cretae an intance of MyGUI
       MyGUI gui=new MyGUI();      
       //add panle to frame
       frame.add(gui);
       frame.setSize(250, 250);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }
}

5)


/**
* The java program Demo5 that demonstrates the
* slider example.
* */
//Demo4.java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JSlider;
public class Demo4
{
   public static void main(String[] args)
   {
       JFrame frame=new JFrame(\"Demo4\");
       JPanel sliderPanel=new JPanel();
       //Create an instance of the JSlider
       JSlider hbar = new JSlider(JScrollBar.HORIZONTAL, 0, 250, 0);

       // Major Tick 50 - Minor 10
       hbar.setMinorTickSpacing(50);
       hbar.setMajorTickSpacing(10);
       //set tick visible true
       hbar.setPaintTicks(true);
       hbar.setSnapToTicks(true);

       sliderPanel.add(hbar);


       frame.add(sliderPanel);
       frame.setSize(250, 250);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }

}

1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, and creates a border around it containing the title \
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, and creates a border around it containing the title \
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, and creates a border around it containing the title \
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, and creates a border around it containing the title \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site