Tuesday, 8 May 2018

swing - Java JFRAME button then new gui





So, I try to make a Java Program, that when you run it, the first screen will be welcome, under it button "login", under it "register". And now I need to figure how if I press one of these buttons, how can I call new GUI, which I will define somewhere. (e.g) i call register button and it calls new gui where is normal things that asks you when you register(login,email,pass,date of birth)



EDIT: problem solved, but there is a one more thing. How can I close the first window?



This is my code so far:



import javax.swing.*;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import java.awt.Dimension;
public class Gui extends JFrame
{
private JLabel lab1,lab2;

private JButton butt1,butt2;
private JPanel p1,p2;
public static void main(String[] args)
{
Gui okno = new Gui();
//vytáhne z defaultního monitoru width a height
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
//velikost okna

Dimension appSize = new Dimension(210,250);
okno.setPreferredSize(appSize);
//nastavení na stred
okno.setLocation((width/2)-105,(height/2)-125);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
//okno.setLocationRelativeTo(null); todle dá do středu obrazovky jen první body x a y od kterejch se to odvíjí
okno.setResizable(false);
okno.pack();


}

public Gui(){
super("Jméno hry vole");
setLayout(new BorderLayout(20,20));
/////////////////////////////////////////////////////////////////
// p1
p1 = new JPanel();
add(p1,BorderLayout.NORTH);
lab1 = new JLabel("Welcome",SwingConstants.CENTER); //centr labelu

lab2 = new JLabel("Created by DECHKR",SwingConstants.CENTER); //centr labelu
lab1.setFont(new Font("Serif", Font.PLAIN, 36)); //velikost fontu
p1.add(lab1);
//p2
p2 = new JPanel(new GridLayout(2,1,0,5));
add(p2,BorderLayout.SOUTH);

Dimension d = new Dimension(210,75);
butt1 = new JButton("Login");
butt1.setPreferredSize(d);

butt2 = new JButton("Register");
butt2.setPreferredSize(d);
p2.add(butt1);
p2.add(butt2);


butt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
//Gui okno = new Gui();
//System.exit(0); endne celej jvm proces




}

});

butt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){



}

});
}

Answer



I am still learning, but want to try and help.I would try making a new JFrame in the actionPerformed method of the listener. I also would probably make separate classes for your button action listeners otherwise you won't be able to perform separate task. I.E.



 class button1Listener implements ActionListener {

public void actionPerformed(ActionEvent ev){
JFrame frame = new JFrame();
(ETC CODE...)
}
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...