2017-01-26 7 views
0

は、私はこのような主なクラスがあります。レイアウト/ GUIを他のクラスからインポート/使用する方法は?

package ijsberenSpel; 

public class Main{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

} 

をそして私は、このようなレイアウト(GUI)クラスを持っている:

package ijsberenSpel; 

import java.awt.FlowLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

public class Layout extends JFrame { 

    private static final long serialVersionUID = 1L; 

    private JLabel label; 
    private JButton button; 
    private JTextField textfield; 

    public Layout() { 
     setLayout(new FlowLayout()); 

     label = new JLabel("Hello World"); 
     add(label); 

     textfield = new JTextField(15); 
     add(textfield); 

     button = new JButton("Submit"); 
     add(button); 
    } 

    public static void layout (String args[]){ 
     Layout gui = new Layout(); 
     gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     gui.setSize(500, 500); 
     gui.setVisible(true); 
     gui.setTitle("ijsberenspel"); 
    } 
} 

私は私のメインクラスのすべてのレイアウトクラスからコードを持っている場合うまく動作しますが、私は他のクラスでレイアウト/ GUIなどをしたいです。

どうすればよいですか?

答えて

1

単にあなたがこれを使用することができます:

は、あなたのクラスの新しいインスタンスを作成します。

Layout layout = new Layout(); 

設定し、それが目に見える:

layout.setVisible(true); 

gui.setVisible(true); 

は、このような

gui.setTitle("ijsberenspel"); 

後になる必要があります。これは、あなたのGUIである

public static void main(String args[]){ 

ない

public static void layout (String args[]){ 
+0

はい、私はすでに私の答えに追加:) –

1

gui.setTitle("ijsberenspel"); 
gui.setVisible(true); 

また、主な方法は次のようにする必要がありますクラス:

public class Layout extends JFrame {  

    private static final long serialVersionUID = 1L; 

    private JLabel label; 
    private JButton button; 
    private JTextField textfield; 

    public Layout() { 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(500, 500);   
     setTitle("ijsberenspel"); 
     setLayout(new FlowLayout()); 

     label = new JLabel("Hello World"); 
     add(label); 

     textfield = new JTextField(15); 
     add(textfield); 

     button = new JButton("Submit"); 
     add(button); 
    } 
} 

これは、mainメソッドを持つあなたのメインクラスです:

package ijsberenSpel; 

public class Main{  

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       Layout layout = new Layout(); 
       layout.setVisible(true); 
      } 
     }); 
    } 

} 
関連する問題