I'm trying to make a GUI, and I can accomplish it all in one method, but I would like to make the code simpler and make multiple methods. However, I can't get it to work. I am new to Java programming.
public class Main { public static void main(String[] args) { FirstWindow fw = new FirstWindow(); fw.setVisible(true); fw.setSize(600,400); } } public class FirstWindow extends JFrame { public FirstWindow() { checkbox c = new checkbox(); c(); } } public class checkbox extends JFrame { public checkbox() { //code } }
This question is related to
java
swing
user-interface
methods
multiple-instances
I'm not sure what you're trying to do, but here's something to consider: c();
won't do anything. c
is an instance of the class checkbox
and not a method to be called. So consider this:
public class FirstWindow extends JFrame { public FirstWindow() { checkbox c = new checkbox(); c.yourMethod(yourParameters); // call the method you made in checkbox } } public class checkbox extends JFrame { public checkbox(yourParameters) { // this is the constructor method used to initialize instance variables } public void yourMethod() // doesn't have to be void { // put your code here } }
Source: Stackoverflow.com