panel - panel
public class TestNo3 {
public static void main(String[] args) {
No3 tiga = new No3();
tiga.setVisible(true);
}
}
public static void main(String[] args) {
No3 tiga = new No3();
tiga.setVisible(true);
}
}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author admin
*/
public class No3 extends JFrame implements ActionListener {
private static final int FRAME_WIDTH = 1000;
private static final int FRAME_HEIGHT = 800;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private JMenu utama1;
private JMenu utama2;
private String nm2,pass2;
private JMenuItem psswd, exit;
private JButton okButton;
private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 30;
private JPanel panel1, panel2;
Container contentPane;
JLabel nama, password;
JTextField nm, pass;
public No3() {
setTitle("Password");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
createpanel1();
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(utama1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void quitAction() {
System.exit(0);
}
private void createpanel1() {
utama1 = new JMenu("File");
psswd = new JMenuItem("Menu Password");
psswd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
passwordAction();
}
});
utama1.add(psswd);
utama1.addSeparator();
exit = new JMenuItem("Quit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
quitAction();
}
});
utama1.add(exit);
}
public void passwordAction() {
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2));
nama = new JLabel();
nama.setText("Input your UserName : ");
nm = new JTextField();
nm.setColumns(20);
password = new JLabel();
password.setText("Input your Password: ");
pass = new JTextField();
pass.setColumns(20);
panel1.add(nama);
panel1.add(nm);
panel1.add(password);
panel1.add(pass);
contentPane.add(panel1);
panel1.revalidate();
panel1.setVisible(true);
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
okButton = new JButton("OK");
okButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
okButton.addActionListener(this);
panel2.add(okButton);
contentPane.add(panel2);
panel2.revalidate();
panel2.setVisible(true);
}
public void actionPerformed(ActionEvent l) {
nm2 = nm.getText();
pass2 = pass.getText();
if (nm2.equals("root") && pass2.equals("admin")) {
JOptionPane.showMessageDialog(null, "password ok");
} else {
JOptionPane.showMessageDialog(null, "masukkan salah");
}
}
}
Komentar