How to link this frame with another frame when I click a button that opens another frame?- Java

StackOverflow https://stackoverflow.com/questions/23449280

  •  14-07-2023
  •  | 
  •  

Domanda

I am facing a bit problem as I am a newbe in Java. I made two frame, the first one's name is test and the second one is Login. In the test Jframe, there are three buttons. The third one which name is "Admin" is for taking me to the second frame. How can I make the third buttonwork to take me to the second frame without closing the first frame?

Thanks in advance.

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class test extends JFrame {

private static final int width = 600, height = 480;
private static final int buttonWidth = 200, buttonHeight = 100;

static JFrame frame;
static JButton button;
static JButton button2;
static JButton button3;


public static void main(String[] args) {
createWindow();
createButtons();
addEverything();
frame.setVisible(true);

}

private static void createWindow() {
frame = new JFrame("Welcome to Easy Hire");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
}

private static void createButtons() {

button = new JButton("Hire A Taxi");
button.setLocation(0, 0);

button2 = new JButton("User Login");
button2.setLocation(200, 0);

button3 = new JButton("Admin");
button3.setLocation(400, 0);

}

private static void addEverything() {
addButtons();
}

private static void addButtons() {
frame.getContentPane().setLayout(null);
frame.getContentPane().add(button);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);

button.setSize(200,50);
button2.setSize(200,50);
button3.setSize(200,50);
}
}
È stato utile?

Soluzione

Suggestions:

  • To give JButtons behaviors, add ActionListeners to them via the addActionListener(...) method. I don't see that you've tried this.
  • You should avoid having more than one JFrame in an application as the JFrame is usually the application's "mother ship".
  • If you need to show a dialog window, such as a log-in window, use a dialog, a modal JDialog to be exact.
  • If you need help showing this, show your attempt at doing this.
  • Avoid null layouts and absolute positioning of components. This way leads to madness.

Useful Java Tutorials:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top