[Go to site: main page, start]

0% found this document useful (0 votes)
7 views7 pages

Java Swing Button Frame Examples

The document contains multiple Java Swing programs demonstrating the creation of GUI components. It includes examples of a frame with buttons, a frame with buttons arranged using BorderLayout, a panel with buttons using FlowLayout, and a login form with username and password fields. Each program showcases different layouts and event handling for button clicks.

Uploaded by

kc.abis236kar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Java Swing Button Frame Examples

The document contains multiple Java Swing programs demonstrating the creation of GUI components. It includes examples of a frame with buttons, a frame with buttons arranged using BorderLayout, a panel with buttons using FlowLayout, and a login form with username and password fields. Each program showcases different layouts and event handling for button clicks.

Uploaded by

kc.abis236kar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Create a Frame with Buttons in Java using Swing.

package [Link];
import [Link].*;
import [Link].*;

public class StartingFrame extends JFrame {


private Button submit;
private JButton cancel;
public StartingFrame() {
setLayout(null);
setSize(700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

submit=new Button("Submit");
cancel=new JButton("Cancel");

[Link](10,100,100,40);
[Link](10, 150, 100, 40);
add(submit);
add(cancel);

setVisible(true);
}

public static void main(String[] args) {


new StartingFrame();
}
}

Output
Write a Java program using Swing to create a frame
with five buttons arranged using BorderLayout and
display a message in the console when each button is
clicked.
package [Link];
import [Link].*;
import [Link].*;
import [Link].*; /

public class SecondFrame implements ActionListener {


private JButton east;
private JButton west;
private JButton north;
private JButton south;
private JButton center;

public SecondFrame() {
JFrame frame = new JFrame("This is second frame");
[Link](new BorderLayout());
[Link](800, 800);
[Link](JFrame.EXIT_ON_CLOSE);

east = new JButton("East");


[Link](east, [Link]);

west = new JButton("West");


[Link](west, [Link]);

north = new JButton("North");


[Link](north, [Link]);

south = new JButton("South");


[Link](south, [Link]);

center = new JButton("Center");


[Link](center, [Link]);

// Add ActionListeners to all buttons


[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);

[Link](true);
}
@Override
public void actionPerformed(ActionEvent e) {
if ([Link]() == east) {
[Link]("East button clicked");
} else if ([Link]() == west) {
[Link]("West button clicked");
} else if ([Link]() == north) {
[Link]("North button clicked");
} else if ([Link]() == south) {
[Link]("South button clicked");
} else if ([Link]() == center) {
[Link]("Center button clicked");
}
}

public static void main(String[] args) {


new SecondFrame();
}
}

Output
Write a Java program using Swing to create a panel
with multiple buttons arranged using FlowLayout inside
a frame.
package [Link];
import [Link].*;
import [Link].*;

public class PanelTest extends JFrame{


private JPanel panel;
private JButton east;
private JButton submit;
public PanelTest() {
//by default frame has border layout
setLayout(new BorderLayout());
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel=new JPanel();
//by default panmel has flow layout
[Link](new FlowLayout());
[Link]([Link]);

[Link](new JButton("first"));

east = new JButton("East");


[Link](0, 100, 150, 40);
[Link](east);

submit=new JButton("Submit");
[Link](100, 250, 100, 200);
[Link](submit);

JButton btn1=new JButton("Hello there");


[Link](btn1);

add(panel);
setVisible(true);
}
public static void main(String []args) {
new PanelTest();
}

}
Write a Java program using Swing to create a login
form with username and password fields and a submit
button that displays a message when clicked.
package [Link];
import [Link];
import [Link];
import [Link].*;

public class LoginForm extends JFrame implements ActionListener {


private JTextField username;
private JPasswordField password;
private JLabel uname, pwd;
private JButton submit;

public LoginForm() {
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 800);

uname = new JLabel("Username:");


[Link](100, 100, 100, 30);

username = new JTextField();


[Link](200, 100, 150, 30);

pwd = new JLabel("Password:");


[Link](100, 150, 100, 30);

password = new JPasswordField();


[Link](200, 150, 150, 30);

submit = new JButton("Submit");


[Link](150, 200, 100, 30);

// Add components
add(uname);
add(username);
add(pwd);
add(password);
add(submit);

// Register ActionListener
[Link](this);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String name = [Link]();
String pass = new String([Link]());

if ([Link]() || [Link]()) {
[Link](this, "Username or password is empty");
return;
}

[Link]("Username: " + name + " | Password: " + pass);


}

public static void main(String[] args) {


new LoginForm();
}
}

Output
Output

You might also like