从另一个JPanel调用JPanel

时间:2013-09-26 16:27:20

标签: java eclipse swing jpanel windowbuilder

我知道这是不明智但我使用绝对布局并试图从一个面板到另一个面板并且很好奇如何做到这一点。

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。谢谢!

1 个答案:

答案 0 :(得分:2)

数据输入日志的初始化应该是这样的

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);
    }
}