2013-09-26 137 views
0

我知道這是不好的建議,但我使用絕對佈局,並試圖從面板到面板,並很好奇如何做到這一點。從另一個JPanel調用JPanel

public class Mainscreen extends JFrame { 

private JPanel Home; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
EventQueue.invokeLater(new Runnable() { 
public void run() { 
try { 
    Mainscreen frame = new Mainscreen(); 
frame.setVisible(true); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
}); 
} 


public Mainscreen() { 
final Dataentrylog DEL = new Dataentrylog(); 

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setResizable(false); 
setBounds(100, 100, 618, 373); 
Home = new JPanel(); 
Home.setBackground(new Color(255, 250, 250)); 
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true)); 
setContentPane(Home); 
Home.setLayout(null); 

    JButton btnNewButton = new JButton("Data Entry login"); 
     btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14)); 
     btnNewButton.setForeground(new Color(0, 0, 0)); 
     btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground")); 

     btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 

     Home.setVisible(false); 
     setContentPane(DEL); 
     setLayout(null);  



     } 
     }); 
     btnNewButton.setBounds(44, 214, 204, 61); 
     Home.add(btnNewButton); 


} 



} 

調用此JPanel其中工程

import java.awt.EventQueue; 

public class Dataentrylog extends JPanel { 

public Dataentrylog() { 
setBounds(100, 100, 618, 373); 
setBackground(new Color(255, 250, 250)); 
setBorder(new LineBorder(Color.DARK_GRAY, 1, true)); 
setLayout(null); 

DEadmin DEA = new Deadmin(); 

    final JButton btnSignIn = new JButton("Sign in"); 
    btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14)); 
    btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground")); 
    btnSignIn.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 


    } 


    }); 
    btnSignIn.setBounds(226, 282, 153, 52); 
    add(btnSignIn); 
} 


} 

雖然這個作品,如果我嘗試從一個的DataEntry調用另一個JPanel的日誌JFrame中是空白。我能做些什麼來調用另一個JPanel?任何幫助都會很棒。另外,我知道佈局管理員是常態,但對於我必須做的事情,我無法找到任何有效的工作,所以儘管我做出了最佳判斷,但仍選擇使用null。謝謝!

回答

2

Dataentrylog的Intialization應該是這樣的

final Dataentrylog DEL = new Dataentrylog(this); 

我們必須通過父的JFrame。

public class Dataentrylog extends JPanel { 


    public Dataentrylog(final JFrame parent) { 
     setBounds(100, 100, 618, 373); 
     setBackground(new Color(255, 250, 250)); 
     setBorder(new LineBorder(Color.DARK_GRAY, 1, true)); 
     final JButton btnSignIn = new JButton("Sign in"); 
     btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14)); 
     btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground")); 
     btnSignIn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       setVisible(false); 
       JPanel panel = new JPanel(); 
       panel.add(new JButton("Solved")); 
       parent.setContentPane(panel); 
      } 
     }); 
     btnSignIn.setBounds(226, 282, 153, 52); 
     add(btnSignIn); 
    } 
} 
+0

感謝您的幫助,工作。 –