2013-02-21 34 views
0

我已將我的Keypad類分開,並且想從其他類(gui)運行它,所以我可以在我的GUI類中BTN等),然後在我的小鍵盤底部。Java如何將其他類的應用程序添加到GUI類中

當我嘗試Keypad kp = new Keypad();我幾乎得到我想要的,但他們顯示在單獨的窗口我希望他們在同一個窗口。

這就是鍵盤類:

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class KeypadWork extends JFrame implements ActionListener { 

private JButton buttonR = new JButton("Reset"); 
private JButton button0 = new JButton("0"); 
private JButton buttonE = new JButton("Enter"); 



public KeypadWork() { 
    setTitle("Keypad"); 

    setLayout(new GridLayout(4, 3, 2, 2)); 
    for (int i = 1; i < 10; i++) { 
     addButton(new JButton(String.valueOf(i))); 
    } 

    addButton(buttonR); 
    addButton(button0); 
    addButton(buttonE); 

    this.pack(); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLocationRelativeTo(null); 
    setResizable(false); 
    setVisible(true); 

} 

private void addButton(JButton button) { 
    button.addActionListener(this); 
    add(button); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 

} 
} 
+2

請分享您的代碼, – 2013-02-21 16:05:26

+0

當我添加我的主類'鍵盤kp = new Keypad();'出現這兩個窗口,我想看到我的主窗口和鍵盤在主類gui – knowbody 2013-02-21 16:09:42

+1

如果你不希望'KeyPadWork'實例在一個單獨的窗口中,那麼你不應該把它作爲'JFrame'。如果你想在另一個窗口中使用普通的AWT Container.add(Component)方法,可以擴展'JPanel'並將'KeyPadWork'實例添加到其他'JFrame'中。 – Alderath 2013-02-21 16:12:11

回答

0

這就是解決方案,謝謝@Alderath

如果你不希望KeyPadWork實例是在一個單獨的窗口,那麼你不應該」讓它成爲JFrame。如果您想要在另一個窗口內,請使用常規AWT Container.add(Component)方法替代擴展JPanel並將KeyPadWork實例添加到其他JFrame

非常感謝!

相關問題