Skip to content

Instantly share code, notes, and snippets.

@avinashseth
Created May 13, 2019 06:14
Show Gist options
  • Save avinashseth/a6ce600421e2f4e1d3e950e70dbfe103 to your computer and use it in GitHub Desktop.
Save avinashseth/a6ce600421e2f4e1d3e950e70dbfe103 to your computer and use it in GitHub Desktop.
package testinggui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;
import javax.swing.JToggleButton;
public class LoginWindow {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginWindow window = new LoginWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Male", "Female", "Others"}));
comboBox.setSelectedIndex(2);
comboBox.setBounds(59, 45, 108, 21);
frame.getContentPane().add(comboBox);
JLabel lblFeedback = new JLabel("");
JButton btnClickMe = new JButton("Click Me");
btnClickMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int getIndexValue = comboBox.getSelectedIndex();
String getValueOfComboBox = comboBox.getSelectedItem().toString();
lblFeedback.setText(getValueOfComboBox + " index value is " + getIndexValue);
}
});
btnClickMe.setBounds(276, 130, 85, 21);
frame.getContentPane().add(btnClickMe);
lblFeedback.setBounds(167, 214, 237, 13);
frame.getContentPane().add(lblFeedback);
JCheckBox chckbxClickOnMe = new JCheckBox("Click on me");
chckbxClickOnMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chckbxClickOnMe.isSelected()) {
lblFeedback.setText("Clicked");
} else {
lblFeedback.setText("Yet to click");
}
}
});
chckbxClickOnMe.setBounds(49, 113, 95, 21);
frame.getContentPane().add(chckbxClickOnMe);
JToggleButton tglbtnNewToggleButton = new JToggleButton("New toggle button");
tglbtnNewToggleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(tglbtnNewToggleButton.isSelected()) {
tglbtnNewToggleButton.setText("Go offline");
lblFeedback.setText("You are online");
lblFeedback.setForeground(Color.GREEN);
} else {
tglbtnNewToggleButton.setText("Go online");
lblFeedback.setText("You are offline");
lblFeedback.setForeground(Color.RED);
}
}
});
tglbtnNewToggleButton.setBounds(50, 189, 117, 21);
frame.getContentPane().add(tglbtnNewToggleButton);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment