Reputation: 1
Hello Guys I need a password that I want to be writen in the JPasswordField (JPasswordFieldAnon) and attach submit action to the two JButtons (AnonButton and AnonButton1).
Here is the code:
package javafx;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class Anonymous {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Anonymous window = new Anonymous();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Anonymous() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setTitle("Anonymous Terminal. Enter Password");
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(952, 785);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel JLabelAnon = new JLabel("We are Anonymous. We are Legion. We do not forgive. We do not forget. Expect Us.");
JLabelAnon.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
JLabelAnon.setBackground(Color.BLACK);
JLabelAnon.setFont(new Font("hooge 05_53", Font.PLAIN, 13));
JLabelAnon.setForeground(Color.GREEN);
JLabelAnon.setBounds(116, 566, 695, 14);
frame.getContentPane().add(JLabelAnon);
JPasswordField JPasswordFieldAnon = new JPasswordField("Enter Password: ", 1);
JPasswordFieldAnon.setBackground(new Color(0, 128, 0));
JPasswordFieldAnon.setToolTipText("Enter Password\r\n");
JPasswordFieldAnon.setText("");
JPasswordFieldAnon.setName("Anonymous Password");
JPasswordFieldAnon.setBounds(367, 535, 206, 22);
frame.getContentPane().add(JPasswordFieldAnon);
JButton AnonButton = new JButton("Expect Us!");
AnonButton.setBackground(new Color(34, 139, 34));
AnonButton.setName("Log In");
AnonButton.setBounds(579, 535, 115, 22);
frame.getContentPane().add(AnonButton);
JButton AnonButton1 = new JButton("Expect Us!");
AnonButton1.setName("Log In");
AnonButton1.setBackground(new Color(34, 139, 34));
AnonButton1.setBounds(246, 535, 115, 22);
frame.getContentPane().add(AnonButton1);
JTextPane textPane1 = new JTextPane();
textPane1.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
textPane1.setEditable(false);
textPane1.setBackground(Color.BLACK);
textPane1.setBounds(108, 563, 703, 20);
frame.getContentPane().add(textPane1);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("F:\\FBI-Terminal_1.png"));
lblNewLabel.setBounds(0, 0, 948, 759);
frame.getContentPane().add(lblNewLabel);
}
public void actionPerformed(ActionEvent e) {
}
}
My problem is that I tried around 5 days now just trying a lot methods...
Upvotes: 0
Views: 170
Reputation: 17971
Some notes about your code.
Your password field variable does not have scope enough to retrieve the typed password later because it's a local variable within initialize()
method. I'd define it as a class variable.
You have to attach an ActionListener
to the buttons to actually "submit" the password.
You have an actionPerformed()
method but the implementation of ActionListener
interface is missing in your class header. This is why we should use @Override
annotation when we implement or override existing methods.
See these tutorials:
Swing is designed to work with Layout Managers. and the use of methods such as setLocation(...)
, setBounds(...)
or setXxxSize(...)
is highly discouraged.
Upvotes: 2