SlideShare a Scribd company logo
1 of 6
Download to read offline
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);
}
}

More Related Content

Similar to Create JPanels, add listeners and components

Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
Design a Windows Desktop application and write the code that will exe.pdf
Design a Windows Desktop application and write the code that will exe.pdfDesign a Windows Desktop application and write the code that will exe.pdf
Design a Windows Desktop application and write the code that will exe.pdfFootageetoffe16
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfbirajdar2
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfudit652068
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTPayal Dungarwal
 
I need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdfI need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdfezhilvizhiyan
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfvenkt12345
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfpackage buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfarjuntiwari586
 
AJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docxAJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docxRenuDeshmukh5
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension Yuren Ju
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word documentnidhileena
 
A Simple Java GUI Application.pptx
A Simple Java GUI Application.pptxA Simple Java GUI Application.pptx
A Simple Java GUI Application.pptxraedriyad
 
Chap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxChap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxTadeseBeyene
 

Similar to Create JPanels, add listeners and components (20)

Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Design a Windows Desktop application and write the code that will exe.pdf
Design a Windows Desktop application and write the code that will exe.pdfDesign a Windows Desktop application and write the code that will exe.pdf
Design a Windows Desktop application and write the code that will exe.pdf
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
 
I need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdfI need help creating a java gui that draws lines, shapes, characters.pdf
I need help creating a java gui that draws lines, shapes, characters.pdf
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfpackage buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdf
 
Java File
Java FileJava File
Java File
 
AJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docxAJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docx
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word document
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
Java awt
Java awtJava awt
Java awt
 
A Simple Java GUI Application.pptx
A Simple Java GUI Application.pptxA Simple Java GUI Application.pptx
A Simple Java GUI Application.pptx
 
Chap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxChap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptx
 

More from optokunal1

Which asymmetric cryptosystem is used for digital signaturesA. DE.pdf
Which asymmetric cryptosystem is used for digital signaturesA. DE.pdfWhich asymmetric cryptosystem is used for digital signaturesA. DE.pdf
Which asymmetric cryptosystem is used for digital signaturesA. DE.pdfoptokunal1
 
Which of these theories fit into the contextual world-viewQuestio.pdf
Which of these theories fit into the contextual world-viewQuestio.pdfWhich of these theories fit into the contextual world-viewQuestio.pdf
Which of these theories fit into the contextual world-viewQuestio.pdfoptokunal1
 
Which of the following distinguishes prophase of meiosis I from proph.pdf
Which of the following distinguishes prophase of meiosis I from proph.pdfWhich of the following distinguishes prophase of meiosis I from proph.pdf
Which of the following distinguishes prophase of meiosis I from proph.pdfoptokunal1
 
What is meant by primary growth What is meant by secondary growth .pdf
What is meant by primary growth  What is meant by secondary growth .pdfWhat is meant by primary growth  What is meant by secondary growth .pdf
What is meant by primary growth What is meant by secondary growth .pdfoptokunal1
 
What to the characteristic X-ray radiation What is the originSo.pdf
What to the characteristic X-ray radiation What is the originSo.pdfWhat to the characteristic X-ray radiation What is the originSo.pdf
What to the characteristic X-ray radiation What is the originSo.pdfoptokunal1
 
What other blood cell related problems are often seen in patients wi.pdf
What other blood cell related problems are often seen in patients wi.pdfWhat other blood cell related problems are often seen in patients wi.pdf
What other blood cell related problems are often seen in patients wi.pdfoptokunal1
 
what is inductive and deductive reasoningSolutionDeductive and.pdf
what is inductive and deductive reasoningSolutionDeductive and.pdfwhat is inductive and deductive reasoningSolutionDeductive and.pdf
what is inductive and deductive reasoningSolutionDeductive and.pdfoptokunal1
 
What are the major evolutionary feats shared by all 3 phyla( Platyhe.pdf
What are the major evolutionary feats shared by all 3 phyla( Platyhe.pdfWhat are the major evolutionary feats shared by all 3 phyla( Platyhe.pdf
What are the major evolutionary feats shared by all 3 phyla( Platyhe.pdfoptokunal1
 
What are some of the advantages in the use of AOA notation as oppose.pdf
What are some of the advantages in the use of AOA notation as oppose.pdfWhat are some of the advantages in the use of AOA notation as oppose.pdf
What are some of the advantages in the use of AOA notation as oppose.pdfoptokunal1
 
Verify the identity.StartFraction sine left parenthesis alpha plus.pdf
Verify the identity.StartFraction sine left parenthesis alpha plus.pdfVerify the identity.StartFraction sine left parenthesis alpha plus.pdf
Verify the identity.StartFraction sine left parenthesis alpha plus.pdfoptokunal1
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdfoptokunal1
 
This is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdfThis is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdfoptokunal1
 
The concentration of carbon dioxide in the atmosphere today is about .pdf
The concentration of carbon dioxide in the atmosphere today is about .pdfThe concentration of carbon dioxide in the atmosphere today is about .pdf
The concentration of carbon dioxide in the atmosphere today is about .pdfoptokunal1
 
Research and discuss two operating systems and how incident response.pdf
Research and discuss two operating systems and how incident response.pdfResearch and discuss two operating systems and how incident response.pdf
Research and discuss two operating systems and how incident response.pdfoptokunal1
 
Proteins can be attached to a membrane in a variety of ways. How doe.pdf
Proteins can be attached to a membrane in a variety of ways. How doe.pdfProteins can be attached to a membrane in a variety of ways. How doe.pdf
Proteins can be attached to a membrane in a variety of ways. How doe.pdfoptokunal1
 
Problem 1-What is environmental engineering (20 pts) Below you wil.pdf
Problem 1-What is environmental engineering (20 pts) Below you wil.pdfProblem 1-What is environmental engineering (20 pts) Below you wil.pdf
Problem 1-What is environmental engineering (20 pts) Below you wil.pdfoptokunal1
 
NetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdf
NetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdfNetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdf
NetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdfoptokunal1
 
Mosses and liverworts are early colonizers during ecological success.pdf
Mosses and liverworts are early colonizers during ecological success.pdfMosses and liverworts are early colonizers during ecological success.pdf
Mosses and liverworts are early colonizers during ecological success.pdfoptokunal1
 
Many programming languages, especially older ones, provide no langua.pdf
Many programming languages, especially older ones, provide no langua.pdfMany programming languages, especially older ones, provide no langua.pdf
Many programming languages, especially older ones, provide no langua.pdfoptokunal1
 
Inferior nasal concha Lacrimal bone Mandible Maxilla Nasal bone .pdf
Inferior nasal concha  Lacrimal bone  Mandible  Maxilla  Nasal bone  .pdfInferior nasal concha  Lacrimal bone  Mandible  Maxilla  Nasal bone  .pdf
Inferior nasal concha Lacrimal bone Mandible Maxilla Nasal bone .pdfoptokunal1
 

More from optokunal1 (20)

Which asymmetric cryptosystem is used for digital signaturesA. DE.pdf
Which asymmetric cryptosystem is used for digital signaturesA. DE.pdfWhich asymmetric cryptosystem is used for digital signaturesA. DE.pdf
Which asymmetric cryptosystem is used for digital signaturesA. DE.pdf
 
Which of these theories fit into the contextual world-viewQuestio.pdf
Which of these theories fit into the contextual world-viewQuestio.pdfWhich of these theories fit into the contextual world-viewQuestio.pdf
Which of these theories fit into the contextual world-viewQuestio.pdf
 
Which of the following distinguishes prophase of meiosis I from proph.pdf
Which of the following distinguishes prophase of meiosis I from proph.pdfWhich of the following distinguishes prophase of meiosis I from proph.pdf
Which of the following distinguishes prophase of meiosis I from proph.pdf
 
What is meant by primary growth What is meant by secondary growth .pdf
What is meant by primary growth  What is meant by secondary growth .pdfWhat is meant by primary growth  What is meant by secondary growth .pdf
What is meant by primary growth What is meant by secondary growth .pdf
 
What to the characteristic X-ray radiation What is the originSo.pdf
What to the characteristic X-ray radiation What is the originSo.pdfWhat to the characteristic X-ray radiation What is the originSo.pdf
What to the characteristic X-ray radiation What is the originSo.pdf
 
What other blood cell related problems are often seen in patients wi.pdf
What other blood cell related problems are often seen in patients wi.pdfWhat other blood cell related problems are often seen in patients wi.pdf
What other blood cell related problems are often seen in patients wi.pdf
 
what is inductive and deductive reasoningSolutionDeductive and.pdf
what is inductive and deductive reasoningSolutionDeductive and.pdfwhat is inductive and deductive reasoningSolutionDeductive and.pdf
what is inductive and deductive reasoningSolutionDeductive and.pdf
 
What are the major evolutionary feats shared by all 3 phyla( Platyhe.pdf
What are the major evolutionary feats shared by all 3 phyla( Platyhe.pdfWhat are the major evolutionary feats shared by all 3 phyla( Platyhe.pdf
What are the major evolutionary feats shared by all 3 phyla( Platyhe.pdf
 
What are some of the advantages in the use of AOA notation as oppose.pdf
What are some of the advantages in the use of AOA notation as oppose.pdfWhat are some of the advantages in the use of AOA notation as oppose.pdf
What are some of the advantages in the use of AOA notation as oppose.pdf
 
Verify the identity.StartFraction sine left parenthesis alpha plus.pdf
Verify the identity.StartFraction sine left parenthesis alpha plus.pdfVerify the identity.StartFraction sine left parenthesis alpha plus.pdf
Verify the identity.StartFraction sine left parenthesis alpha plus.pdf
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdf
 
This is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdfThis is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdf
 
The concentration of carbon dioxide in the atmosphere today is about .pdf
The concentration of carbon dioxide in the atmosphere today is about .pdfThe concentration of carbon dioxide in the atmosphere today is about .pdf
The concentration of carbon dioxide in the atmosphere today is about .pdf
 
Research and discuss two operating systems and how incident response.pdf
Research and discuss two operating systems and how incident response.pdfResearch and discuss two operating systems and how incident response.pdf
Research and discuss two operating systems and how incident response.pdf
 
Proteins can be attached to a membrane in a variety of ways. How doe.pdf
Proteins can be attached to a membrane in a variety of ways. How doe.pdfProteins can be attached to a membrane in a variety of ways. How doe.pdf
Proteins can be attached to a membrane in a variety of ways. How doe.pdf
 
Problem 1-What is environmental engineering (20 pts) Below you wil.pdf
Problem 1-What is environmental engineering (20 pts) Below you wil.pdfProblem 1-What is environmental engineering (20 pts) Below you wil.pdf
Problem 1-What is environmental engineering (20 pts) Below you wil.pdf
 
NetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdf
NetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdfNetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdf
NetWork Design Question2.) How does TCP prevent Congestion Dicuss.pdf
 
Mosses and liverworts are early colonizers during ecological success.pdf
Mosses and liverworts are early colonizers during ecological success.pdfMosses and liverworts are early colonizers during ecological success.pdf
Mosses and liverworts are early colonizers during ecological success.pdf
 
Many programming languages, especially older ones, provide no langua.pdf
Many programming languages, especially older ones, provide no langua.pdfMany programming languages, especially older ones, provide no langua.pdf
Many programming languages, especially older ones, provide no langua.pdf
 
Inferior nasal concha Lacrimal bone Mandible Maxilla Nasal bone .pdf
Inferior nasal concha  Lacrimal bone  Mandible  Maxilla  Nasal bone  .pdfInferior nasal concha  Lacrimal bone  Mandible  Maxilla  Nasal bone  .pdf
Inferior nasal concha Lacrimal bone Mandible Maxilla Nasal bone .pdf
 

Recently uploaded

Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

Create JPanels, add listeners and components

  • 1. 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);
  • 2. //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
  • 3. 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
  • 4. 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);
  • 5. 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)
  • 6. /** * 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); } }