How do I replace these static JFrame variables? - java

I'm a freshman with all the GUI stuff and I want to know why do I have to declare all the JFrames with "static" in my code(otherwise it wont compile) and how can I modify my code to compile without declaring them "static"?
public class Ventana extends JFrame {
private static JFrame frameInicio = new JFrame("Ingresar Datos");
private static JFrame frameCrearUsuario = new JFrame("Crear Usuario");
private static JFrame frameMenu = new JFrame("Menu Del Juego");
private static JFrame frameJuego = new JFrame("El Ahorcado");
public static void main(String[] args) {
Inicio();
}
public static void Inicio(){
frameCrearUsuario.dispose();
frameMenu.dispose();
frameInicio.setSize(450,200);
frameInicio.setLocationRelativeTo(null);
frameInicio.add(new FrameInicio());
frameInicio.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameInicio.setVisible(true);
}
public void CrearUsuario(){
frameInicio.dispose();
frameCrearUsuario.setSize(450,300);
frameCrearUsuario.setLocationRelativeTo(null);
frameCrearUsuario.add(new FrameCrearUsuario());
frameCrearUsuario.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameCrearUsuario.setVisible(true);
}
public void Menu(){
frameInicio.dispose();
frameJuego.dispose();
frameMenu.setSize(660,290);
frameMenu.setLocationRelativeTo(null);
frameMenu.add(new FrameMenu());
frameMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameMenu.setVisible(true);
}
public void Juego(){
frameMenu.dispose();
frameJuego.setSize(1100,800);
frameJuego.setLocationRelativeTo(null);
frameJuego.add(new FrameJuego());
frameJuego.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameJuego.validate();
frameJuego.setVisible(true);
}

It's becuase your public static void Inicio() is static. You can't call a non static property from a static method
Here's a common way to use a JFrame. Create a constructor for the JFrame and add all the components inside of it. Then just Create the JFrame object in the main method
public class Ventana extends JFrame {
JPanel panel = new JPanel();
JButton button = new JButton("button");
JTextArea jta = new JTextArea();
public Vantan(){
add(panel);
add(button);
add(jta);
}
public static void main(String[] args){
JFrame frame = new Ventana();
frame.setTitle("Frame);
frame.setSize(500, 500);
frame.pack();
frame.setDefaulCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This way you don't need the static properties as in your case.

Related

Add element to JFrame from other class

I have two classes in one JFrame creation:
public class Window extends JFrame {
public void createWindow() throws IOException {
setTitle(GeneralValues.PROGRAM_NAME);
setSize(1100, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
And in the second I have a JLabel:
public class InitialConfiguration {
Window w = new Window();
public void main() {
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100);
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
}
}
My main method:
public class Main {
Window w = new Window();
InitialConfiguration ic = new InitialConfiguration();
public static void main(String[] args) {
w.createWindow();
ic.main();
}
}
I wanted to add a label from the InitialConfiguration class to the frame from the Window class but unfortunately this code creates a frame but does not add the label from the InitialConfiguration class. Can it be done at all?
Your issue is, that you went wrong with the instances of your Window. Your InitialConfiguration has a member variable of Window, to which the label is being added. But this is not the same instance of the Window that you have in your Main class.
So all in all, you create and show the window, but never show the second window you actually added your label to.
So altogether, if you really want to keep your structure this way (I would advise against that but you may have your reasons), passing the correct Window instance as a method parameter should fix it.
public class InitialConfiguration {
// Removed second Window here
public void main(Window w) { // rename this method please
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100); // use layout managers
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
}
}
And your "Main":
public class Main {
Window w = new Window(); // rename the class Window please
InitialConfiguration ic = new InitialConfiguration();
public static void main(String[] args) {
w.createWindow();
ic.main(w);
}
}
You didn't start the Jframe at window class and I don't know your main class so I made it for your understanding. Check this codes;
(Edited)
Main: Example code:
public class MainMenu{
public static void main(String[] args) {
Windows w = new Windows();
InitialConfiguration ini = new InitialConfiguration(w);
w.createWindow();
ini.main();
}
}
ICONFIG class example code:
public class InitialConfiguration {
Window w;
public InitialConfiguration(Windows w){
this.w = w;
}
public void main() {
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100);
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
w.repaint();
}
}
Window class example code:
public class Window extends JFrame {
public void createWindow(){
setTitle("Your title");
setSize(1100, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}

Can I acces variables and/or methods of the class a JFrame was created in from the panel?

shortened code:
public class Test{
private JFrame frame;
private TestPanel panel;
Object obj;
public static void main(String args[]) {
Test test = new Test();
}
public Test() {
try {
// Setup GUI
frame = new JFrame("Test");
panel = new TestPanel();
frame.add(panel);
}
}
}
Is it possible to directly access obj from panel?
If yes: How?
Thank you in advance for your help. :)
No; Not without passing the reference to object to the panel.

Can't resolve an object initiated in a public method?

I'm just starting out with java and I was working on a new GUI. I made a method that makes my JFrame, sets its default location, close operation, background colour etc.
I made a separate method for this to keep it out of the main code for the sake of tidiness. I already know how to solve this method if I just put all of those things in the main method.
This is the code:
public class Main {
public static void makeWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
}
public static void main(String args[]){
makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
I get an error in my main method saying that "mainWindow" can't be resolved. Why is this? is it because I made the mainWindow object in the other method? Is there a way to resolve this issue without putting everything from the makeWindow() method into the main method?
That won't work because mainWindow only exists in the context of your makeWindow method. You can make it a static field instead:
public class Main {
private static JFrame mainWindow; // declare it here so it can be seen from your main method.
public static void makeWindow(){
mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
}
public static void main(String args[]){
makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
EDIT
As #Xing commented (credits shared), you can return the mainWindow from your makeWindow method:
public class Main {
public static JFrame makeWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
return mainWindow;
}
public static void main(String args[]){
JFrame mainWindow = makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
create mainWindow instance/ reference globally
private static JFrame mainWindow;
public static void makeWindow(){
mainWindow = new JFrame();
}

How to use CardLayout, with Menu as action listener?

I want to change the JPanel of a JFrame, using the CardLayout class.
I have already run this example and it works.
Now I want to use as action listener, the JMenuItem; so If I press that JMenuItem, I want to change it, with a specific panel. So this is the JFrame:
public class FantaFrame extends JFrame implements Observer {
private static final long serialVersionUID = 1L;
private JPanel cardPanel = new JPanel();
private CardLayout cardLayout = new CardLayout();
public FantaFrame(HashMap<String, JPanel> fantaPanels) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("FantaCalcio App");
setSize(500, 500);
cardPanel.setLayout(cardLayout);
setPanels(fantaPanels);
}
public void update(Observable o, Object arg) {
cardLayout.show(cardPanel, arg.toString());
}
private void setPanels(HashMap<String, JPanel> fantaPanels) {
for (String name : fantaPanels.keySet()) {
cardPanel.add(fantaPanels.get(name), name);
}
}
}
Those are the Menu, the Controller and the Main:
private void pressed(){
home.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
controller.changePanel(home.getText());
}
});
}
public class Controller extends Observable {
public void changePanel(String panel){
setChanged();
notifyObservers(panel);
}
}
public static void main(String[] args) {
fantaPanels.put("Login", new LoginPanel());
Controller controller = new Controller();
MenuBarApp menuApp = new MenuBarApp(controller);
FantaFrame frame = new FantaFrame(fantaPanels);
frame.setJMenuBar(menuApp);
controller.addObserver(frame);
frame.setVisible(true);
}
The problem is that the JPanel doesn't change. What do you think is the problem ?
I've already debugged it, and in the update() method, the correct String value arrives.
You never add the cardPanel JPanel, the one using the CardLayout and displaying the "cards" to anything. You need to add it to your JFrame's contentPane for it to display anything. i.e.,
public FantaFrame(HashMap<String, JPanel> fantaPanels) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("FantaCalcio App");
setSize(500, 500);
cardPanel.setLayout(cardLayout);
add(cardPanel, BorderLayout.CENTER); // ****** add this line ******
setPanels(fantaPanels);
}

Controlling JComponents from other classes

Is it possible to control instances like variables, jcomponents, timer in other external classes?
For example this is my Class1
public class Class1 extends JFrame {
JLabel lbl = new JLabel("Hello");
public Class1() {
super("Class1");
Container c = getContentPane();
setLayout(null);
c.add(lbl);
lbl.setBounds(0,0,100,20);
Class2.process();
setSize(200,100);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String var[]) {
new Class1();
}
}
You can see there's Class2.process();
Here's the other class externally but in the same folder
public class Class2 {
public static void process() {
// I want to control lbl from Class1 class inside this method like
// lbl.setVisible(false);
}
public static void main(String args[]) {
//
}
}
Is it possible? Sorry. I can't find answers on other website.
You have to pass the instance of JLabel e.g.;
JLabel lbl = new JLabel("Hello"););
Class2.process(lbl);
public class Class2 {
public static void process(JLabel lbl) {
// I want to control lbl from Class1 class inside this method like
lbl.setVisible(false); // this will change your Class1 Jlabel
}

Categories