0

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

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?任何帮助都会很棒。另外,我知道 layoutmanagers 是常态,但是对于我必须做的事情,我找不到任何有用的东西,所以尽管我做出了最好的判断,我还是选择使用 null。谢谢!

4

1 回答 1

3

Dataentrylog的初始化应该是这样的

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);
    }
}
于 2013-09-26T16:55:16.240 回答