开发者

Java Swing Visual Editor

开发者 https://www.devze.com 2023-01-02 21:52 出处:网络
How would i call this function in my main? private JFrame getMainpageframe1() { if (mainpageframe1 == null) {

How would i call this function in my main?

    private JFrame getMainpageframe1() {
    if (mainpageframe1 == null) {
        mainpageframe1 = new JFrame();
    开发者_Python百科    mainpageframe1.setSize(new Dimension(315, 306));
        mainpageframe1.setContentPane(getMainpage());
        mainpageframe1.setTitle("Shopping For Less: Main Page");
        mainpageframe1.setVisible(true);
        mainpageframe1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    return mainpageframe1;
}

public static void main(String[] args) {        
   //call that function to output the JFrame?
}

thanks


For one, you'll want to place your GUI stuff on the EDT. The Java library provides you with some helper methods that'll make your life a whole lot easier with SwingUtilities.

Second I'd try to refactor the code a bit and possibly move the JFrame you are building into a separate class. In this code example I made it part of the same class that contains the main method and I am extending JFrame here.

public class YourApp extends JFrame {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        YourApp app = new YourApp();
        app.setupFrame();
      }
    });
  }

  private setupFrame() {
    this.setSize(new Dimension(315, 306));
    this.setContentPane(getMainpage());
    this.setTitle("Shopping For Less: Main Page");
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 }


YOurClass instance = new YourClass();
instance.getMainpageframe1();

However this code is really really bad. You should move at least the setVisible() out of it - othwerwise the code will block at the point and the method would not return until the frame is not visible anymore.

YOurClass instance = new YourClass();
instance.getMainpageframe1().setVisible(true);


public class YourClass {
    public static void main(String[] args) {
        YourClass instance = new YourClass();
        JFrame frame = instance.getMainpageframe1();
    }
}

Reason why you have to create an instance of the whole class in main is because you can't invoke a non-static function from a static function.


public class YourClass{

  JFrame mainpageframe1;

  private JFrame getMainpageframe1() {
    if (mainpageframe1 == null) {
      mainpageframe1 = new JFrame();
      mainpageframe1.setSize(new Dimension(315, 306));
      mainpageframe1.setContentPane(getMainpage());
      mainpageframe1.setTitle("Shopping For Less: Main Page");
      mainpageframe1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainpageframe1.setVisible(true);
    }
    return mainpageframe1;
  }

  public static void main(String[] args) {
    YourClass yourClass = new YourClass();
    yourClass.getMainpageframe1();
  }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消