button not showing in Jframe - java

btn3 is not showing when I run it in Netbeans.
when i ran it in NotePad it shows third button.
i cant really figure what the problem is.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Regform2 extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;
Regform2() {
setTitle("Registration Form in Java");
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn3 = new JButton("nxt");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(300, 310, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
btn3.setBounds(300, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
add(btn3);
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else if (e.getSource() == btn2) {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
} else {
this.dispose();
NewJFrame nw = new NewJFrame();
nw.setVisible(true);
}
}
public static void main(String args[]) {
new Regform2();
}}

if you put setlayout(null) at first then setVisible(true),the btn3 will show in jframe. like this:
setLayout(null);
setVisible(true);
setSize(700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You need to add a JPanel. To this panel you add your components.
And don't forget to add your panel to your frame like this:
JPanel contentPane = new JPanel();
content.add(new JButton("Test"));
this.setDefaultCloseOperation(3);
this.setContentPane(contentPanel);
this.setSize(Width, Height);
this.setResizable(false);
this.setVisible(true);

this code will work... please define the codes of main frame at beginning of the
constructor, such as
setTitle();
setSize();
setLayout();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Regform2 extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;
Regform2() {
setTitle("Registration Form in Java");
setSize(700, 700);
setLayout(null);
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn3 = new JButton("Next");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(300, 310, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
btn3.setBounds(290, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
add(btn3);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else if (e.getSource() == btn2) {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
} else {
this.dispose();
//NewJFrame nw = new NewJFrame();
//nw.setVisible(true);
}
}
public static void main(String args[]) {
new Regform2();
}
}

Related

JTextField not visible until mouse over

I am trying to make a simple calculator program with Java. When I added a JTextField however it made all the button and the field itself invisible until I hover over it. If I comment out the text field everything goes back to normal and all the button are visible.
Here is my code:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JFrame {
// Numbers
JButton btn_zero;
JButton btn_one;
JButton btn_two;
JButton btn_three;
JButton btn_four;
JButton btn_five;
JButton btn_six;
JButton btn_seven;
JButton btn_eight;
JButton btn_nine;
// Operators
JButton btn_add;
JButton btn_subtract;
JButton btn_multiply;
JButton btn_divide;
JButton btn_equals;
JButton btn_decimal;
JButton btn_pm;
JButton btn_clear;
// Panel
JPanel buttonPanel;
// Dimensions
final int WIDTH = 340;
final int HEIGHT = 500;
public Calculator() {
// Characteristics of frame
super("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Insets frameInsets = getInsets();
int frameWidth = WIDTH + (frameInsets.left + frameInsets.right);
int frameHeight = HEIGHT + (frameInsets.top + frameInsets.bottom);
setPreferredSize(new Dimension(frameWidth, frameHeight));
//setLayout(null);
pack();
setVisible(true);
// Add values to all buttons
btn_zero = new JButton("0");
btn_one = new JButton("1");
btn_two = new JButton("2");
btn_three = new JButton("3");
btn_four = new JButton("4");
btn_five = new JButton("5");
btn_six = new JButton("6");
btn_seven = new JButton("7");
btn_eight = new JButton("8");
btn_nine = new JButton("9");
btn_add = new JButton("+");
btn_subtract = new JButton("-");
btn_multiply = new JButton("×");
btn_divide = new JButton("÷");
btn_equals = new JButton("=");
btn_decimal = new JButton(".");
btn_pm = new JButton("±");
btn_clear = new JButton("C");
// Adds the panel
buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(frameWidth, frameHeight));
buttonPanel.setLayout(null);
// Textfield
JTextField AnswerBox = new JTextField ("");
AnswerBox.setBounds(0, 0, 320, 70);
buttonPanel.add(AnswerBox);
//Buttons
btn_decimal.setBounds(70, 100, 50, 50);
buttonPanel.add(btn_decimal);
btn_pm.setBounds(130, 100, 50, 50);
buttonPanel.add(btn_pm);
btn_clear.setBounds(190, 100, 50, 50);
buttonPanel.add(btn_clear);
btn_add.setBounds(250, 100, 50, 50);
buttonPanel.add(btn_add);
btn_subtract.setBounds(250, 160, 50, 50);
buttonPanel.add(btn_subtract);
btn_multiply.setBounds(250, 220, 50, 50);
buttonPanel.add(btn_multiply);
btn_divide.setBounds(250, 280, 50, 50);
buttonPanel.add(btn_divide);
btn_equals.setBounds(10, 350, 290, 50);
buttonPanel.add(btn_equals);
btn_zero.setBounds(190, 160, 50, 170);
buttonPanel.add(btn_zero);
btn_one.setBounds(10, 160, 50, 50);
buttonPanel.add(btn_one);
btn_two.setBounds(70, 160 , 50, 50);
buttonPanel.add(btn_two);
btn_three.setBounds(130, 160, 50, 50);
buttonPanel.add(btn_three);
btn_four.setBounds(10, 220, 50, 50);
buttonPanel.add(btn_four);
btn_five.setBounds(70, 220, 50, 50);
buttonPanel.add(btn_five);
btn_six.setBounds(130, 220, 50, 50);
buttonPanel.add(btn_six);
btn_seven.setBounds(10, 280, 50, 50);
buttonPanel.add(btn_seven);
btn_eight.setBounds(70, 280, 50, 50);
buttonPanel.add(btn_eight);
btn_nine.setBounds(130, 280, 50, 50);
buttonPanel.add(btn_nine);
buttonPanel.setVisible(true);
add(buttonPanel);
}
}
Also the code is also right now just showing the buttons of the calculator, and not actually doing anything, because I want to focus on fixing this bug.
Call revalidate() after you add all the widgets to recalculate the layout of the container (in your case a JFrame):
buttonPanel.setVisible(true);
add(buttonPanel);
revalidate();
Calling revalidate() at the end would work or you can simply put
setVisible(true)
to the end of your constructor.

How to load Text from TextField, checkbox, RadioButton, ComboBox to one Big Text Field

I'm writing a simple program for recruiting employees.
I have few questions with JtextField, few checkboxes with text, a RadioButton with text and a Combobox.
I need the data entered in the text windows, checkbox, drop-down list to be thrown into the large window on the right.
At the moment there are only data from the text windows, but not from the top just in the middle of the field.
package appka;
import javax.swing.*;
import java.awt.Checkbox;
import java.awt.event.*;
public class CzwartaApp {
public static void main(String[] args) {
// Klasy obiektow
JFrame frame = new JFrame("Czwarta Aplikacja");
JButton btnSubmit = new JButton("Wypisz");
JButton btnExit = new JButton("Wyjdz");
JLabel lbl1 = new JLabel("imię");
JLabel lbl2 = new JLabel("nazwisko");
JLabel lbl3 = new JLabel("stanowisko");
JLabel lbl4 = new JLabel("E-mail");
JLabel lbl5 = new JLabel("");
JLabel lbl6 = new JLabel("Jakie znasz języki programowania?");
JLabel lbl7 = new JLabel("Wybierz poziom języka angielskiego?");
JLabel lbl8 = new JLabel("Wybierz kurs programowania?");
JLabel lbl9 = new JLabel("Dane Kontaktowe");
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
JTextField tf5 = new JTextField();
Checkbox checkbox1 = new Checkbox("Java");
Checkbox checkbox2 = new Checkbox("Python");
Checkbox checkbox3 = new Checkbox("Inne");
JRadioButton btn1 = new JRadioButton();
btn1.setText("podstawowy");
JRadioButton btn2 = new JRadioButton();
btn2.setText("średnio-zaawansowany");
JRadioButton btn3 = new JRadioButton();
btn3.setText("zaawansowany");
ButtonGroup group = new ButtonGroup ();
group.add(btn1); group.add(btn2); group.add(btn3);
JComboBox kurs = new JComboBox();
kurs.addItem("FrontEnd Developer");
kurs.addItem("BackEnd Developer");
// Pozycjonowanie
lbl1.setBounds(20, 20, 100, 20);
tf1.setBounds(20, 70, 100, 20);
lbl2.setBounds(20, 120, 100, 20);
tf2.setBounds(20, 170, 100, 20);
lbl3.setBounds(20, 220, 100, 20);
tf3.setBounds(20, 270, 100, 20);
lbl4.setBounds(20, 320, 100, 20);
tf4.setBounds(20, 370, 100, 20);
tf5.setBounds(500, 20, 250, 300);
lbl5.setBounds(20, 420, 300, 20);
lbl6.setBounds(200, 20, 250, 20);
lbl7.setBounds(200, 220, 250, 20);
lbl8.setBounds(200, 350, 250, 20);
lbl9.setBounds(500, 50, 200, 20);
btnSubmit.setBounds(20, 470, 100, 20);
btnExit.setBounds(220, 470, 100, 20);
checkbox1.setBounds(200, 70, 250, 20);
checkbox2.setBounds(200, 120, 250, 20);
checkbox3.setBounds(200, 170, 250, 20);
btn1.setBounds(200, 250, 250, 20);
btn2.setBounds(200, 280, 250, 20);
btn3.setBounds(200, 310, 250, 20);
kurs.setBounds(200, 380, 250, 20);
// Obsluga zdarzen
btnSubmit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String imie = tf1.getText();
String nazwisko = tf2.getText();
String stanowisko = tf3.getText();
String email = tf4.getText();
tf5.setText(imie+" "+nazwisko+" "+stanowisko+" "+email);
}
});
btnExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// Dodanie do okna
frame.add(btnSubmit); frame.add(btnExit);
frame.add(tf1); frame.add(tf2); frame.add(tf3);frame.add(tf4);frame.add(tf5);
frame.add(lbl1); frame.add(lbl2); frame.add(lbl3); frame.add(lbl4);frame.add(lbl5);
frame.add(lbl6);frame.add(lbl7);frame.add(lbl8);frame.add(lbl9);
frame.add(checkbox1); frame.add(checkbox2); frame.add(checkbox3);
frame.add(btn1); frame.add(btn2); frame.add(btn3);
frame.getContentPane().add(kurs);
frame.setSize(800, 600);
frame.setLayout(null);
frame.setVisible(true);
}
}
Here is the corrected code.
import java.awt.Checkbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CzwartaApp {
public static void main(String[] args) {
// Klasy obiektow
JFrame frame = new JFrame("Czwarta Aplikacja");
JButton btnSubmit = new JButton("Wypisz");
JButton btnExit = new JButton("Wyjdz");
JLabel lbl1 = new JLabel("imie");
JLabel lbl2 = new JLabel("nazwisko");
JLabel lbl3 = new JLabel("stanowisko");
JLabel lbl4 = new JLabel("E-mail");
JLabel lbl5 = new JLabel("");
JLabel lbl6 = new JLabel("Jakie znasz jezyki programowania?");
JLabel lbl7 = new JLabel("Wybierz poziom jezyka angielskiego?");
JLabel lbl8 = new JLabel("Wybierz kurs programowania?");
JLabel lbl9 = new JLabel("Dane Kontaktowe");
final JTextField tf1 = new JTextField();
final JTextField tf2 = new JTextField();
final JTextField tf3 = new JTextField();
final JTextField tf4 = new JTextField();
final JTextArea tf5 = new JTextArea();
final Checkbox checkbox1 = new Checkbox("Java");
final Checkbox checkbox2 = new Checkbox("Python");
final Checkbox checkbox3 = new Checkbox("Inne");
JRadioButton btn1 = new JRadioButton();
btn1.setText("podstawowy");
JRadioButton btn2 = new JRadioButton();
btn2.setText("srednio-zaawansowany");
JRadioButton btn3 = new JRadioButton();
btn3.setText("zaawansowany");
ButtonGroup group = new ButtonGroup();
group.add(btn1);
group.add(btn2);
group.add(btn3);
final JComboBox kurs = new JComboBox();
kurs.addItem("FrontEnd Developer");
kurs.addItem("BackEnd Developer");
// Pozycjonowanie
lbl1.setBounds(20, 20, 100, 20);
tf1.setBounds(20, 70, 100, 20);
lbl2.setBounds(20, 120, 100, 20);
tf2.setBounds(20, 170, 100, 20);
lbl3.setBounds(20, 220, 100, 20);
tf3.setBounds(20, 270, 100, 20);
lbl4.setBounds(20, 320, 100, 20);
tf4.setBounds(20, 370, 100, 20);
tf5.setBounds(500, 20, 250, 300);
lbl5.setBounds(20, 420, 300, 20);
lbl6.setBounds(200, 20, 250, 20);
lbl7.setBounds(200, 220, 250, 20);
lbl8.setBounds(200, 350, 250, 20);
lbl9.setBounds(500, 50, 200, 20);
btnSubmit.setBounds(20, 470, 100, 20);
btnExit.setBounds(220, 470, 100, 20);
checkbox1.setBounds(200, 70, 250, 20);
checkbox2.setBounds(200, 120, 250, 20);
checkbox3.setBounds(200, 170, 250, 20);
btn1.setBounds(200, 250, 250, 20);
btn2.setBounds(200, 280, 250, 20);
btn3.setBounds(200, 310, 250, 20);
kurs.setBounds(200, 380, 250, 20);
// Obsluga zdarzen
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String imie = tf1.getText();
String nazwisko = tf2.getText();
String stanowisko = tf3.getText();
String email = tf4.getText();
StringBuffer buffer = new StringBuffer();
buffer.append( "imie : ").append( imie ).append("\n");
buffer.append( "nazwisko : ").append( nazwisko ).append("\n");
buffer.append( "stanowisko : ").append( stanowisko ).append("\n");
buffer.append( "E-mail : ").append( email ).append("\n");
buffer.append( "Jakie znasz jezyki programowania? : ");
if( checkbox1.getState() ) {
buffer.append( checkbox1.getLabel() ).append(",");
}
if( checkbox2.getState() ) {
buffer.append( checkbox2.getLabel() ).append(",");
}
if( checkbox3.getState() ) {
buffer.append( checkbox3.getLabel() );
}
buffer.append("\n");
buffer.append( "Wybierz kurs programowania? ").append( kurs.getSelectedItem().toString() ).append("\n");
tf5.setText(buffer.toString());
}
});
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Dodanie do okna
frame.add(btnSubmit);
frame.add(btnExit);
frame.add(tf1);
frame.add(tf2);
frame.add(tf3);
frame.add(tf4);
frame.add(tf5);
frame.add(lbl1);
frame.add(lbl2);
frame.add(lbl3);
frame.add(lbl4);
frame.add(lbl5);
frame.add(lbl6);
frame.add(lbl7);
frame.add(lbl8);
frame.add(lbl9);
frame.add(checkbox1);
frame.add(checkbox2);
frame.add(checkbox3);
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.getContentPane().add(kurs);
frame.setSize(800, 600);
frame.setLayout(null);
frame.setVisible(true);
}
}

Program Terminates without running in eclipse

When I run this code ( having simple buttons and a LOGIN button with an action listener), it terminates without running and without showing screen.
I have tried System.exit(0); in main function to overcome this terminating issue but all in vain
public class HOme extends JFrame{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
Color cardinal = new Color(194, 35, 38);
int w=155;
int h=50;
public HOme(String title) {
super(title);
getContentPane().setSize(width,height);
getContentPane().setBackground(Color.WHITE);
getContentPane().setLayout(null);
final JPanel panel2 = new JPanel();
panel2.setBounds(364, 33, 664, 344);
getContentPane().add(panel2);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.WHITE);
panel3.setBounds(81, 382, 947, 243);
getContentPane().add(panel3);
panel3.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnHome.setForeground(Color.WHITE);
btnHome.setBackground(cardinal);
btnHome.setBounds(517, 33, w, h);
btnHome.setContentAreaFilled(false);
btnHome.setOpaque(true);
panel3.add(btnHome);
JButton btnClients = new JButton("Clients");
btnClients.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnClients.setForeground(Color.WHITE);
btnClients.setBounds(690, 33, w, h);
btnClients.setBackground(cardinal);
btnClients.setContentAreaFilled(false);
btnClients.setOpaque(true);
panel3.add(btnClients);
JButton btnClose = new JButton("Close");
btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnClose.setForeground(Color.WHITE);
btnClose.setBounds(690, 198, w, h);
btnClose.setBackground(cardinal);
btnClose.setContentAreaFilled(false);
btnClose.setOpaque(true);
panel3.add(btnClose);
JButton btnLogin = new JButton("Admin Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Login l=new Login();
panel2.add(l);
}
});
btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnLogin.setForeground(Color.WHITE);
btnLogin.setBounds(517, 116, w, h);
btnLogin.setBackground(cardinal);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
panel3.add(btnLogin);
JPanel panel1 = new JPanel();
panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(204, 51, 0), null));
panel1.setBackground(Color.WHITE);
panel1.setBounds(81, 33, 263, 344);
getContentPane().add(panel1);
panel1.setLayout(null);
JButton btnStartMonitoring = new JButton("");
btnStartMonitoring.setIcon(new ImageIcon(path1));
btnStartMonitoring.setBackground(cardinal);
btnStartMonitoring.setForeground(Color.WHITE);
btnStartMonitoring.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnStartMonitoring.setBounds(10, 274, 239, 59);
panel1.add(btnStartMonitoring);
JLabel lblLogo = new JLabel("New label");
lblLogo.setIcon(new ImageIcon(path2));
lblLogo.setBounds(0, 11, 263, 253);
panel1.add(lblLogo);
}
public static void main(String args[]) {
new HOme("HOme");
//System.exit(0);
}
}
Edited
I have a login class extended from JPanel. When I click on Login Button from Home. It is not showing Login panel
Login.class
public class Login extends JPanel {
private JTextField txtPassword;
private JTextField txtID;
Color cardinal = new Color(194, 35, 38);
int w=155;
int h=50;
public Login() {
setBackground(Color.WHITE);
setLayout(null);
JLabel lblLogin = new JLabel("Login ");
lblLogin.setBackground(Color.ORANGE);
lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
lblLogin.setFont(new Font("Trajan Pro", Font.BOLD, 36));
lblLogin.setBounds(125, 0, 424, 59);
lblLogin.setBackground(cardinal);
//lblLogin.setContentAreaFilled(false);
lblLogin.setOpaque(true);
lblLogin.setForeground(Color.white);
add(lblLogin);
JLabel lblId = new JLabel("ID");
lblId.setHorizontalAlignment(SwingConstants.RIGHT);
lblId.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblId.setBounds(181, 127, 66, 28);
add(lblId);
JLabel lblPassword = new JLabel("Password");
lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
lblPassword.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblPassword.setBounds(136, 188, 111, 28);
add(lblPassword);
txtPassword = new JTextField();
lblPassword.setLabelFor(txtPassword);
txtPassword.setBounds(266, 183, 256, 41);
lblPassword.setForeground(cardinal);
add(txtPassword);
txtPassword.setColumns(10);
txtID = new JTextField();
lblId.setLabelFor(txtID);
txtID.setBounds(266, 123, 256, 39);
lblId.setForeground(cardinal);
add(txtID);
txtID.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.setForeground(Color.WHITE);
btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnLogin.setBounds(324, 294, w, h);
btnLogin.setBackground(cardinal);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
add(btnLogin);
setVisible(true);
}
You are not making your JFrame visible.
You can do either -
In your constructor, make it visible by adding the following line at the end -
setVisible(true);
Or in your main() function you can do -
HOme h = new HOme("HOme");
h.setVisible(true);
Answer to second part:
Import MouseListener, import java.awt.event.MouseListener;
Construct a MouseListener somewhere, include action of the button
Where you define btnLogin, add a line btnLogin.addMouseListener(<name of MouseListener>);
An example: http://www.java2s.com/Code/Java/Swing-JFC/ButtonActionSample.htm
Add something like setVisible(true); at the end of the HOme method.

Java frame trouble

I need help with my program because I'll want to put a password and username to my program, so if the username is test && password is 12345 my program will appear a new frame but unfortunately my second didn't work for my label, button and etc here is my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwapFields extends JFrame{
public static void main(String[] args) {
SwapFields frameTabel = new SwapFields();
}
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
JLabel lab = new JLabel("Username :");
JLabel pas = new JLabel("Password :");
JLabel cos;
// JPanel panel = new JPanel();
JButton y1;
JButton y2;
SwapFields() {
super("Enter Your Account !");
setSize(300, 200);
setLocation(500, 280);
panel.setLayout(null);
txuser.setBounds(90, 30, 150, 20);
pass.setBounds(90, 65, 150, 20);
blogin.setBounds(110, 100, 80, 20);
lab.setBounds(15, 28, 150, 20);
pas.setBounds(15, 63, 150, 20);
panel.add(lab);
panel.add(pas);
panel.add(blogin);
panel.add(txuser);
panel.add(pass);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
public void actionlogin() {
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String puname = txuser.getText();
String ppaswd = pass.getText();
if (puname.equals("test") && ppaswd.equals("12345")) {
JFrame frame = new JFrame("Customer");
JPanel panel1 = new JPanel();
frame.setVisible(true);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cos = new JLabel("Do you have a Customer ?");
y1 = new JButton("Yes");
y2 = new JButton("No");
panel1.setLayout(null);
cos.setBounds(70, 30, 150, 20);
y1.setBounds(80, 65, 150, 20);
y2.setBounds(140, 65, 150, 20);
y1.setSize(55, 30);
y2.setSize(55, 30);
panel1.add(y1);
panel1.add(y2);
panel1.add(cos);
frame.getContentPane().add(panel1);
// this is the code ill change this is the second frame .
dispose();
} else {
JOptionPane.showMessageDialog(null, "Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
}
});
}
}
change
panel.setLayout(null);
to
panel.setLayout(new Flowlayout());

Why joptionpane popup two times

I have two problem now.
My First Problem is when i click the confirm button, it will insert the data two times into ms access database.
My Second Problem is why joptionpane also popup two times.
I have the main login page when open the program.
When i click the sign up button, it will remove all the component inside the center jpanel.
Then it will add again component to use in registration form like jtextfield,jlabel,icon for username,password,tel no and others.I already checked, that the statement to execute insert statement and joptionpanes only have one times only.
This is the full code of my program
package userForm;
import java.sql.*;
import java.util.logging.Handler;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class loginForm extends JFrame implements ActionListener{
private JPanel centerPanel,bottomPanel;
private JLabel titleLabel, descriptionLabel;
private JLabel usernameLabel, passwordLabel, signInLabel, iconUsername, iconPassword;
private JLabel signUpLabel, fullNameLabel, staffIDLabel, usernameRegLabel, passwordRegLabel, telNoLabel, emailLabel;
private JLabel iconFullName, iconStaffID, iconUsernameReg, iconPasswordReg, iconTelNo, iconEmail;
private JTextField usernameField, fullNameField, staffIDField, usernameRegField, telNoField, emailField;
private JPasswordField passwordField, passwordRegField;
private JButton loginButton, signUpButton,confirmButton,exitButton;
private String username;
private String password;
userDatabase db;
userDatabase db1;
handler handle;
public loginForm(String title) {
db=new userDatabase();
handle =new handler();
//create label to use in topPanel
titleLabel = new JLabel("ABC Burger Inventory System");
titleLabel.setFont(new Font("Calibri", Font.TRUETYPE_FONT, 23));
titleLabel.setForeground(Color.white);
Border empty = new EmptyBorder(30, 20, 0, 0);
titleLabel.setBorder(empty);
descriptionLabel = new JLabel("Please Login To Use This System");
Border empty1 = new EmptyBorder(-30, 20, 0, 0);
descriptionLabel.setBorder(empty1);
descriptionLabel.setFont(new Font("Calibri", Font.HANGING_BASELINE, 14));
descriptionLabel.setForeground(Color.white);
//create label to use in centerPanel
signInLabel = new JLabel("SIGN IN");
usernameLabel = new JLabel("Username:");
passwordLabel = new JLabel("Password");
//create textfield to use in center Panel
usernameField = new JTextField("Required");
passwordField = new JPasswordField("Required");
//create label to use in registration form
signUpLabel = new JLabel("SIGN UP");
fullNameLabel = new JLabel("Full Name:");
staffIDLabel = new JLabel("Staff ID:");
usernameRegLabel = new JLabel("Username:");
passwordRegLabel = new JLabel("Password");
telNoLabel = new JLabel("Tel No:");
emailLabel = new JLabel("Email:");
//create textfield to use in registration form
fullNameField = new JTextField(30);
staffIDField = new JTextField(30);
usernameRegField = new JTextField(30);
passwordRegField = new JPasswordField(30);
telNoField = new JTextField(30);
emailField = new JTextField(30);
//create button to use in bottom Panel
loginButton = new JButton("Login");
signUpButton = new JButton("Sign Up");
confirmButton = new JButton("Confirm");
confirmButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
//create panel to use in frame
topPanelWithBackground topPanel = new topPanelWithBackground();
topPanel.setLayout(new GridLayout(2,1));
centerPanel = new JPanel();
centerPanel.setLayout(null);
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 10));
//add component to top panel
topPanel.add(titleLabel);
topPanel.add(descriptionLabel);
//add component to center panel
signInLabel.setBounds(270, 30, 100, 20);
signInLabel.setFont(new Font("Calibri", Font.TRUETYPE_FONT, 20));
centerPanel.add(signInLabel);
usernameLabel.setBounds(200, 60, 100, 20);
usernameLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(usernameLabel);
ImageIcon imageUser = new ImageIcon("user.png");
iconUsername = new JLabel(imageUser, JLabel.CENTER);
iconUsername.setBounds(160, 90, 32, 32);
centerPanel.add(iconUsername);
usernameField.setBounds(200, 90, 200, 32);
usernameField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(usernameField);
passwordLabel.setBounds(200, 140, 100, 20);
passwordLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(passwordLabel);
ImageIcon imagePassword = new ImageIcon("password.png");
iconUsername = new JLabel(imagePassword, JLabel.CENTER);
iconUsername.setBounds(160, 170, 32, 32);
centerPanel.add(iconUsername);
passwordField.setBounds(200, 170, 200, 32);
passwordField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(passwordField);
loginButton.setFont(new Font("Calibri", Font.BOLD, 14));
loginButton.addActionListener(handle);
bottomPanel.add(loginButton);
signUpButton.setFont(new Font("Calibri", Font.BOLD, 14));
signUpButton.addActionListener(this);
bottomPanel.add(signUpButton);
//add confirm button
exitButton.setFont(new Font("Calibri", Font.BOLD, 14));
exitButton.addActionListener(this);
bottomPanel.add(exitButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(topPanel, BorderLayout.NORTH );
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
//frame behaviour
super.setTitle(title);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit
setSize(600,500);
}
class handler implements ActionListener{
public void actionPerformed(ActionEvent as) {
if(as.getSource()==loginButton){
centerPanel.removeAll();
centerPanel.revalidate();
centerPanel.repaint();
//add component to center panel
signInLabel.setBounds(270, 30, 100, 20);
signInLabel.setFont(new Font("Calibri", Font.TRUETYPE_FONT, 20));
centerPanel.add(signInLabel);
usernameLabel.setBounds(200, 60, 100, 20);
usernameLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(usernameLabel);
ImageIcon imageUser = new ImageIcon("user.png");
iconUsername = new JLabel(imageUser, JLabel.CENTER);
iconUsername.setBounds(160, 90, 32, 32);
centerPanel.add(iconUsername);
usernameField.setBounds(200, 90, 200, 32);
usernameField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(usernameField);
passwordLabel.setBounds(200, 140, 100, 20);
passwordLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(passwordLabel);
ImageIcon imagePassword = new ImageIcon("password.png");
iconUsername = new JLabel(imagePassword, JLabel.CENTER);
iconUsername.setBounds(160, 170, 32, 32);
centerPanel.add(iconUsername);
passwordField.setBounds(200, 170, 200, 32);
passwordField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(passwordField);
char[] temp_pwd=passwordField.getPassword();
String pwd=null;
pwd=String.copyValueOf(temp_pwd);
System.out.println("Username,Pwd:"+usernameField.getText()+","+pwd);
//The entered username and password are sent via "checkLogin()" which return boolean
if(db.checkLogin(usernameField.getText(), pwd))
{
newFrame regFace =new newFrame();
regFace.setVisible(true);
dispose();
}
else if(usernameField.getText().equals("") || passwordField.getText().equals("")){
JOptionPane.showMessageDialog(null, "Please fill out the form","Error!!",
JOptionPane.ERROR_MESSAGE);
}
else
{
//a pop-up box
JOptionPane.showMessageDialog(null, "Login failed!","Failed!!",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
#Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==signUpButton){
centerPanel.removeAll();
//sign up label
signUpLabel.setBounds(270, 30, 100, 20);
signUpLabel.setFont(new Font("Calibri", Font.TRUETYPE_FONT, 20));
centerPanel.add(signUpLabel);
//fullname label,icon and field
fullNameLabel.setBounds(80, 60, 100, 20);
fullNameLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(fullNameLabel);
ImageIcon imageFullname = new ImageIcon("fullname.png");
iconUsernameReg = new JLabel(imageFullname, JLabel.CENTER);
iconUsernameReg.setBounds(40, 90, 32, 32);
centerPanel.add(iconUsernameReg);
fullNameField.setBounds(80, 90, 200, 32);
fullNameField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(fullNameField);
//staffID label,icon and field
staffIDLabel.setBounds(80, 140, 100, 20);
staffIDLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(staffIDLabel);
ImageIcon imageStaffID = new ImageIcon("staffID.png");
iconStaffID = new JLabel(imageStaffID, JLabel.CENTER);
iconStaffID.setBounds(40, 170, 32, 32);
centerPanel.add(iconStaffID);
staffIDField.setBounds(80, 170, 200, 32);
staffIDField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(staffIDField);
//usernameReg label,icon and field
usernameRegLabel.setBounds(80, 220, 100, 20);
usernameRegLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(usernameRegLabel);
ImageIcon imageUsernameReg = new ImageIcon("user.png");
iconUsernameReg = new JLabel(imageUsernameReg, JLabel.CENTER);
iconUsernameReg.setBounds(40, 250, 32, 32);
centerPanel.add(iconUsernameReg);
usernameRegField.setBounds(80, 250, 200, 32);
usernameRegField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(usernameRegField);
//passwordReg label,icon and field
passwordRegLabel.setBounds(350, 60, 100, 20);
passwordRegLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(passwordRegLabel);
ImageIcon imagePasswordReg = new ImageIcon("password.png");
iconPasswordReg = new JLabel(imagePasswordReg, JLabel.CENTER);
iconPasswordReg.setBounds(310, 90, 32, 32);
centerPanel.add(iconPasswordReg);
passwordRegField.setBounds(350, 90, 200, 32);
passwordRegField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(passwordRegField);
//telNo label,icon and field
telNoLabel.setBounds(350, 140, 100, 20);
telNoLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(telNoLabel);
ImageIcon imagetelNo = new ImageIcon("phone.png");
iconTelNo = new JLabel(imagetelNo, JLabel.CENTER);
iconTelNo.setBounds(310, 170, 32, 32);
centerPanel.add(iconTelNo);
telNoField.setBounds(350, 170, 200, 32);
telNoField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(telNoField);
//Email label,icon and field
emailLabel.setBounds(350, 220, 100, 20);
emailLabel.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(emailLabel);
ImageIcon imageEmail = new ImageIcon("mail.png");
iconEmail = new JLabel(imageEmail , JLabel.CENTER);
iconEmail .setBounds(310, 250, 32, 32);
centerPanel.add(iconEmail );
emailField.setBounds(350, 250, 200, 32);
emailField.setFont(new Font("Calibri", Font.BOLD, 14));
centerPanel.add(emailField);
//add confirm button
confirmButton.setFont(new Font("Calibri", Font.BOLD, 14));
confirmButton.addActionListener(this);
bottomPanel.add(confirmButton);
centerPanel.revalidate();
centerPanel.repaint();
}
else if(ae.getSource()==confirmButton){
char[] temp_pwd1=passwordRegField.getPassword();
String pwd=null;
pwd=String.copyValueOf(temp_pwd1);
if(fullNameField.getText().equals("") || staffIDField.getText().equals("") || usernameRegField.getText().equals("") || passwordRegField.getPassword().length == 0 || telNoField.getText().equals("") || emailField.getText().equals("")){
JOptionPane.showMessageDialog(null, "Please Fill Out Any Field", "Error",
JOptionPane.ERROR_MESSAGE);
}
else {
db1 = new userDatabase();
try {
db1.insertData(fullNameField.getText(), staffIDField.getText(), usernameRegField.getText(), pwd, telNoField.getText(), emailField.getText());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else if(ae.getSource()==exitButton){
System.exit(0);
}
}
}
When you click the sign up button you are adding listener to the confirm button again. So when you click the confirm button it is executed twice.
Remove the following line in the actionPerformed method of LoginForm
confirmButton.addActionListener(this);

Categories