3

How would i be able to run this function from my main() to build the gui, and then use code from elsewhere to handle the button click and retrieve input from the text field?

package main;

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

public class Gui {

public static void mainGUI() {
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    java.net.URL imgApp = ClassLoader.getSystemResource("res/app.png");
    JFrame mainWin = new JFrame("jIRC");
    mainWin.setSize(1024, 720);
    mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWin.setIconImage(new ImageIcon(imgApp).getImage());

    Container panel = mainWin.getContentPane();
    panel.setLayout(null);

    JTextArea inputBox = new JTextArea();
    inputBox.setSize(300, 100);
    inputBox.setLocation(500, 250);

    JButton sendButton = new JButton();
    sendButton.setText("Send");
    sendButton.setFont(new Font("Helvetica", Font.ITALIC, 16));
    sendButton.setSize(72, 32);
    sendButton.setLocation(500, 500);

    panel.add(inputBox);
    panel.add(sendButton);
    mainWin.setVisible(true);
}
}

Here's my class with the main function:

public class Run{

public static void main(String[] args) {

    main.Debug.startupDebug();
    main.Gui.mainGUI();
}
}

How would I go about placing some of my code in a non-static field?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dr Zarreh
  • 57
  • 6

1 Answers1

4

You've got everything in a static method, and that won't allow you to use any of the power of object-oriented programming. Consider creating OOP-compliant classes non-static fields and methods and with public getter and setter methods, and this way other classes can affect the behavior of this class.

Edit
You posted:

public class Run{ 
  public static void main(String[] args) { 
    main.Debug.startupDebug(); 
    main.Gui.mainGUI(); 
  } 
}

But what you need to do instead is something like:

public class Run{ 
  public static void main(String[] args) { 
    GUI gui = new GUI();
    Debug debug = new Debug();

     debug.setGui(gui);
     gui.setDebug(debug);

     gui.startGui();
  } 
}

Or something similar. Again avoid using static anything.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373