Advance java pratice question
Advance java pratice question
B. Java JLabel
CODE:-
import javax.swing.*; OUTPUT:-
public class JLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JLabel Example");
JLabel label = new JLabel("Hello, World!");
label.setBounds(50, 50, 200, 30);
frame.add(label);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
C. Java JTextField
CODE:- OUTPUT:-
import javax.swing.*;
public class JTextFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField Example");
D. Java TextArea
CODE:- OUTPUT:-
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
}
E. JPasswordFieldExample
CODE:- OUTPUT:-
import javax.swing.*;
public class JPasswordFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JPasswordField Example");
F. JcheckBox
CODE:- OUTPUT:-
import javax.swing.*;
}
G. Java JradioButton
CODE:-
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class JRadioButtonExample {
OUTPUT:-
H. Java JComboBox
CODE:- OUTPUT:-
import javax.swing.*;
public class JComboBoxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox Example");
I. Java JList
CODE:- OUTPUT:-
import javax.swing.*;
public class JListExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JList Example");
String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
JList<String> list = new JList<>(data);
list.setBounds(50, 50, 150, 100);
frame.add(list);
frame.setSize(250, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
}
J. Java MenuItem & JMenuJava JPopupMenu
CODE:- OUTPUT:-
import javax.swing.*;
public class JMenuItemJMenuExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JMenuItem and JMenu Example");
Java PopMenuExample
CODE:-
import javax.swing.*;
public class JPopupMenuExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JPopupMenu Example");
frame.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
popupMenu.show(frame, evt.getX(), evt.getY());
}
});
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
OUTPUT:-
K. Java JcolorChooser
CODE:- OUTPUT:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JColorChooserDemo extends JFrame {
JPanel panel;
Color bgColor = Color.LIGHT_GRAY;
public JColorChooserDemo() {
panel = new JPanel(new BorderLayout());
JButton btnColor = new JButton("Change Color");
panel.add(btnColor,BorderLayout.SOUTH);
btnColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Color color = JColorChooser.showDialog(JColorChooserDemo.this,
"Choose a color", bgColor);
if (color != null) { // new color selected
bgColor = color;
}
panel.setBackground(bgColor);
}
});
setContentPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JColorChooser Demo");
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JColorChooserDemo();
}
});
}
}
L. Java JtabbedPane
CODE:-
import java.awt.BorderLayout;
import javax.swing.*;
public class JTabbedPaneExample {
public static void main(String[] args) {
OUTPUT:-
M.Java JSlider
CODE:- OUTPUT:-
import javax.swing.*;
public class JSliderExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JSlider Example");
N. JFileChooser
CODE:- OUTPUT:-
import javax.swing.*;
import java.awt.event.*;
public class JFileChooserExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JFileChooser Example");
JButton button = new JButton("Open File Chooser");
O. JavaToggleButton
CODE:- OUTPUT:-
import javax.swing.*;
public class JToggleButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JToggleButton Example");
JToggleButton toggleButton = new JToggleButton("Toggle Me");
toggleButton.setBounds(50, 50, 150, 30);
frame.add(toggleButton);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}:
P. Java JToolBar
CODE:- OUTPUT:-
import javax.swing.*;
public class JToolBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JToolBar Example");
Q. Java JprogressBar
CODE:- OUTPUT:-
import javax.swing.*;
progressBar.setValue(50);
progressBar.setStringPainted(true);
frame.add(progressBar);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}
2. Write a JAVA Swing program using event Handling A
Frame with three buttons Red blue and green and change
the background color according to button pressed by user.
CODE:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mycolor implements ActionListener
{
Button b1,b2,b3,b4;
Container cn;
JFrame f;
mycolor()
{
f = new JFrame("Event Handling");
cn = f.getContentPane();
f.setVisible(true);
f.setSize(500,500);
b1=new Button("RED");
b2=new Button("BLUE");
b3=new Button("GREEN");
b4=new Button("YELLOW");
f.add(b1);f.add(b2);f.add(b3);f.add(b4);
f.setLayout(null);
b1.setBounds(100,100,100,50);
b2.setBounds(100,200,100,50);
b3.setBounds(100,300,100,50);
b4.setBounds(100,400,100,50);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="RED")
{
cn.setBackground(Color.red);
}
else if(e.getActionCommand()=="BLUE")
{
cn.setBackground(Color.blue);
}
else if(e.getActionCommand()=="GREEN")
{
cn.setBackground(Color.green);
}
else
{ cn.setBackground(Color.yellow);
}
}
public static void main(String[] args) {
mycolor mm = new mycolor();
}
}
OUTPUT:-
3. Design A Login Window as shown below and make it Active.
If user id and password are correct then design of question
4 should open. If password are incorrect then invalid
password message should be displayed.
CODE:- OUTPUT:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class login implements ActionListener
{
JButton b1,b2;
TextField t1,t2;
Label l1,l2;
JFrame f;
Container cn;
login()
{
f = new JFrame ("LoGiN PaGe");
f.setVisible(true);
f.setSize(350,350);
cn = f.getContentPane();
cn.setBackground(new Color(153,0,153));
b1 = new JButton("OK");
b2 = new JButton("Cancel");
f.add(b1); f.add(b2);
f.setLayout(null);
b1.setBounds(50,190,100,50);
b2.setBounds(200,190,100,50);
b1.setBackground(Color.CYAN);
b2.setBackground(Color.CYAN);
l1 = new Label("UserName");
f.add(l1);
l1.setBounds(30,65,150,50);
t1 = new TextField();
f.add(t1);
t1.setBounds(180,70,130,30);
l2 = new Label("Password");
f.add(l2);
l2.setBounds(30,115,150,50);
t2 = new TextField();
f.add(t2);
t2.setBounds(180,120,130,30);
l1.setForeground(Color.CYAN);
l2.setForeground(Color.CYAN);
Font ft = new Font("ARIAL",Font.ITALIC+Font.BOLD,18);
l1.setFont(ft);
l2.setFont(ft);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()== "OK")
{
if(t1.getText().equals("sumit") && t2.getText().equals("sumit"))
{factinfo info = new factinfo();
info.setVisible(true);}
else{
f.dispose();
}
public static void main(String args[])
{
new login();
}}
OUTPUT:-