Java: Why isn't this JFrame button displaying? - java

Basically, all i wan't is for this button to display. It was working in another program I had earlier but it doesn't seem to be working in this one and i have no idea why. If anyone could help that would be great.
public void fixtureList()
{
JButton editButton;
setLayout(null);
editButton = new JButton("Edit");
editButton.setBounds(200, 200, 100, 100);
add(editButton);
}
public void loginPanel()
{
setLayout(null);
JButton loginButton;
loginButton = new JButton("Login");
loginButton.setBounds(10, 10, 100, 100);
add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
}
});
}

You forget to call loginPanel(). Try:
Main window = new Main();
window.setTitle("PE Fixtures v1.0");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.loginPanel();
window.setSize(250, 430);
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
Although, because you are subclassing JFrame, I would suggest doing most of that work in the constructor.

You need to call loginPanel() method within your main method, it is not being used at the moment.

Related

jbutton as method actionlistener doesnt work

so I created JButton as a method like this
private JButton loginButton() {
JButton button = new JButton("Login");
button.setFocusable(false);
button.addActionListener(this);
button.setFont(new Font("MV Boli", Font.PLAIN, 25));
return button;
}
when I try to test if the action listener works, it does nothing. Is this the wrong way how to create JButton and that's why it doesn't wrong or there is something wrong with my code?
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton()) {
System.out.println("Test");
}
}
this.setContentPane(mainPanel());
}
private JPanel mainPanel() {
JPanel panel = new JPanel();
panel.add(loginButton(), BorderLayout.SOUTH);
panel.setVisible(true);
return panel;
}
Your loginButton() is creating a new button each time it is called. So, when you have
panel.add(loginButton(), BorderLayout.SOUTH);
in your mainPanel() method, you create the first button. But when you have
if (e.getSource() == loginButton())
in your actionPerformed() method you create a second button, which is different from the first one you created earlier. That's why your code will not enter the if() block.

Composition over Inheritance

i saw an example of ActionListener use in code by implementing ActionListener.
but here i wanna use functionality of ActionListener by using Ref.Var. of ActionListener.
JButton createButton(){
ActionListener al;
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.setLayout(null);
frame.add(button);
return button;
}
look at ActionListener reference here . how to use this ref.var on button to listen event on button
JButton createButton(){
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
// run code;
}
};
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.addActionListener(al);
frame.add(button);
return button;
}
OR
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//run code;
}
} );
It's basically exactly the same as having the containing class implement ActionListener: you provide an implementation, and you configure your button to listen to it.

getting two instances of JDialog

I am a creating a certain project in java.
I added a button to sign up the user on one jframe and on button click first jframe dispose and second opens up where i have added a back button to go to first frame and login button. When login button is clicked a jdialog appears.
(I extends the jdialog class)
I have found out that if i click back button and then again click on sign up button, second jframe opens up which is normal but when i click login two instances of jdialog appears.
Here is the code for first JFrame button
frontsignbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new SignUpNew().buildDesign();
frontframe.dispose();
}
});
And for second
login.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Login();
}
});
And JDialog code
public class Login extends JDialog {
private JLabel usernametext = new JLabel("User Name");
private JLabel userpasswordtext = new JLabel("Password");
private JLabel message = new JLabel();
private JTextField userfield = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton submit = new JButton("Submit");
{
setTitle("Login");
setLayout(null);
setSize(400, 300);
setLocation(400, 300);
setVisible(true);
setResizable(false);
usernametext.setBounds(20, 20, 80, 10);
userpasswordtext.setBounds(20, 80, 80, 10);
userfield.setBounds(150, 20, 100, 20);
password.setBounds(150, 80, 100, 20);
submit.setBounds(75, 140, 100, 30);
message.setBounds(140, 240, 230, 30);
add(usernametext);
add(userpasswordtext);
add(userfield);
add(password);
add(submit);
add(message);
}
Any idea why this happens. I want to fix this.

Adding components by constructor

I have problem with adding Label by constructor, when I make it by method it's no problem
private void addLabel() {
System.out.println("asd");
JLabel label = new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
System.out.println("asd2");
}
But when i try to do this same by new class and constructor i doesn't work...
Main frame:
public class Frame extends JFrame {
JButton button = new JButton("new");
AddButton button2 = new AddButton();
public Frame() {
setLayout(null);
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.setBounds(40, 10, 50, 25);
add(button);
button2.setBounds(40, 40, 100, 25);
add(button);
}
public static void main(String[] args) {
Frame ap = new Frame();
ap.setVisible(true);
}
AddButton class:
public class AddButton extends JPanel {
JLabel label = new JLabel("asd");
public AddButton() {
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}
Ok i got it, I tried to add "button" two times istead button and button2 :D
Your constructor doesn't make sense, that's not how you should use constructors - constructors are used to create an instance of a class.
When you write
AddButton button2 = new AddButton();
then button2 is of type AddButton, and add doesn't accept this type of object.
You can Edit like this
public class AddButton extends JPanel {
JLabel label;
public AddButton() {
label=new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}

JAR file messes GUI?

I've got some homework to make a simple login window.
After I finished with my coding I tried exporting the file into a runnable jar file, and after running the jar file I saw that it had messed up the GUI.
The JPasswordField fills up my entire JFrame for some reason. I have no clue what might be the problem because it runs fine from the IDE. any help?
here is the code (works fine in eclipse IDE):`
public class Login extends JFrame {
private static final long serialVersionUID = 1L;
private static JTextField user = new JTextField();
private static JPasswordField pass = new JPasswordField();
private static JButton Loginbtn = new JButton("Login");
protected String[] args;
private static JFrame frame = new JFrame("Log In");
public Login(){
Loginbtn.addActionListener(new ActionListener(){
#SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e){
if(user.getText().equals("Admin") && pass.getText().equals("Nimda")){
//System.out.println("Hello ADMIN!");
JOptionPane.showMessageDialog(null, "Logged In!");
MyLog.main(args);
frame.setVisible(false);
}
else{
//System.out.println("Login error!");
JOptionPane.showMessageDialog(null, "Login Error!");
}
}
});
}
public static void main(String[] args) {
#SuppressWarnings("unused")
Login login = new Login();
JLabel username = new JLabel("Username");
JLabel password = new JLabel("Password");
frame.setSize(260, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
username.setBounds(20, 60, 100, 20);
password.setBounds(20, 85, 100, 20);
pass.setBounds(85, 85, 150, 20);
user.setBounds(85, 60, 150, 20);
Loginbtn.setBounds(88, 110, 50, 20);
frame.add(username);
frame.add(password);
frame.add(Loginbtn);
frame.add(user);
frame.add(pass);
}
}
thanks in advance guys!
cheers!
Calling setVisible on the JFrame before adding your components to it causes the problem most likely. You should add all the components that you want to the JFrame before calling setVisible, as setVisible validates those components (lays them out).
What setVisible does from the API, you'll see it talks about validating the components
What validate does from API

Categories