Jframe 中没有内容 java

no content in Jframe java

我的应用程序中有两个 jframes,Addnmember.java 和 Frame1.java Frame1为主Jframe,

我希望当有人按下添加按钮时,将显示添加成员,我已经用这段代码完成了这个,一切都很好:

    AddB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            AddMember addmem = new AddMember();
            addmem.setVisible(true);
        }
    });

但是在新的 Jframe 中,我想在选择完成 Bt 时再次回来,我已经完成了这个代码:

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //dispose();
            Frame1 addmem = new Frame1();
            addmem.setVisible(true);
        }
    });

但这行不通!表示 Jframe 加载但其中没有内容!看:

为什么会这样?这是我的代码:

AddMember.java:

public class AddMember extends JFrame {
private JFrame frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton btnNewButton;

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

/**
 * Create the frame.
 */
public AddMember() {
    setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 331);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel label = new JLabel("\u0646\u0627\u0645:");
    label.setFont(new Font("Tahoma", Font.BOLD, 11));
    label.setBounds(332, 60, 46, 14);
    contentPane.add(label);

    textField = new JTextField();
    textField.setToolTipText("\u0646\u0627\u0645");
    textField.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            textField.setText("");
        }
    });
    textField.setHorizontalAlignment(SwingConstants.RIGHT);
    textField.setText("\u0646\u0627\u0645");
    textField.setBounds(236, 57, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    JLabel label_1 = new JLabel("\u0634.\u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC:");
    label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_1.setBounds(328, 103, 85, 14);
    contentPane.add(label_1);

    textField_1 = new JTextField();
    textField_1.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
    textField_1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_1.setText("");
        }
    });
    textField_1.setText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
    textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_1.setColumns(10);
    textField_1.setBounds(236, 100, 86, 20);
    contentPane.add(textField_1);

    JLabel label_2 = new JLabel("\u0631\u0634\u062A\u0647:");
    label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_2.setBounds(332, 145, 46, 14);
    contentPane.add(label_2);

    textField_2 = new JTextField();
    textField_2.setToolTipText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
    textField_2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField_2.setText("");
        }
    });
    textField_2.setText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
    textField_2.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_2.setColumns(10);
    textField_2.setBounds(236, 142, 86, 20);
    contentPane.add(textField_2);

    JLabel label_3 = new JLabel("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC:");
    label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_3.setBounds(144, 61, 82, 14);
    contentPane.add(label_3);

    textField_3 = new JTextField();
    textField_3.setToolTipText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
    textField_3.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_3.setText("");
        }
    });
    textField_3.setText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
    textField_3.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_3.setColumns(10);
    textField_3.setBounds(48, 58, 86, 20);
    contentPane.add(textField_3);

    JLabel label_4 = new JLabel("\u0634. \u062A\u0645\u0627\u0633:");
    label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_4.setBounds(144, 104, 69, 14);
    contentPane.add(label_4);

    textField_4 = new JTextField();
    textField_4.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
    textField_4.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_4.setText("");
        }
    });
    textField_4.setText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
    textField_4.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_4.setColumns(10);
    textField_4.setBounds(48, 101, 86, 20);
    contentPane.add(textField_4);

    textField_5 = new JTextField();
    textField_5.setToolTipText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
    textField_5.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField_5.setText("");
        }
    });
    textField_5.setText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
    textField_5.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_5.setColumns(10);
    textField_5.setBounds(48, 140, 86, 20);
    contentPane.add(textField_5);

    JLabel label_5 = new JLabel("\u0648\u0631\u0648\u062F\u06CC:");
    label_5.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_5.setBounds(144, 143, 46, 14);
    contentPane.add(label_5);

    textField_6 = new JTextField();
    textField_6.setToolTipText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
    textField_6.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField_6.setText("");
        }
    });
    textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
    textField_6.setText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
    textField_6.setColumns(10);
    textField_6.setBounds(131, 187, 86, 20);
    contentPane.add(textField_6);

    JLabel label_6 = new JLabel("\u0627\u06CC\u0645\u06CC\u0644:");
    label_6.setFont(new Font("Tahoma", Font.BOLD, 11));
    label_6.setBounds(227, 190, 46, 14);
    contentPane.add(label_6);

    btnNewButton = new JButton("Done");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //dispose();
            Frame1 addmem = new Frame1();
            addmem.setVisible(true);
        }
    });
    btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton.setBounds(171, 227, 89, 23);
    contentPane.add(btnNewButton);
}
private static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}
}

和Frame1.java:

public class Frame1 extends JFrame {
Connection connection = null;
private JFrame frame;

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

/**
 * Create the application.
 */

private JTable table;
public Frame1() {
    JOptionPane.showMessageDialog(null, "sazande :|");
    this.initialize();
    connection = DataBConect.dbConnect();
    try {
        String query = "select * from Users";
        PreparedStatement pst = connection.prepareStatement(query);
        ResultSet rs = pst.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    JOptionPane.showMessageDialog(null, "intialize :|");
    frame.setTitle("App");
    frame.setBounds(100, 100, 725, 465);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton AddB = new JButton("Add");
    AddB.setBackground(Color.WHITE);
    AddB.setFont(new Font("Tahoma", Font.BOLD, 11));
    AddB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            AddMember addmem = new AddMember();
            addmem.setVisible(true);
        }
    });
    AddB.setBounds(302, 392, 135, 23);
    frame.getContentPane().add(AddB);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(22, 26, 658, 359);
    frame.getContentPane().add(scrollPane);

    table = new JTable();
    scrollPane.setViewportView(table);



}

}

谢谢。

至于为什么你的第二个Frame1没有显示数据,我不是100%确定,但可能与你没有关闭数据库连接有关。无论如何,在已经获得一次数据库数据的情况下想要收集两次数据库数据,并且在已经创建一次 Frame1 GUI 的情况下再创建两次,这似乎是一种浪费,甚至有潜在的危险。

您应该重新设计您的 GUI。主 JFrame GUI 应该保持可见,并且 Frame1 window 应该是模式 JDialog。作为模式对话框,它将阻止用户与父 JFrame 交互,直到对话框不再可见。

请注意,只需进行几处更改即可开始:

在Frame1.java

  AddB.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
        // frame.dispose();
        AddMember addmem = new AddMember(Frame1.this); // !!
        addmem.setVisible(true);
     }
  });


在AddMember.java

class AddMember extends JDialog {

public AddMember(JFrame frame) {
   super(frame, "", ModalityType.APPLICATION_MODAL);
   setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
   // !! setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  btnNewButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
        // dispose();
        dispose(); // !!
        // !! Frame1 addmem = new Frame1();
        // addmem.setVisible(true);
     }
  });

此外,您需要避免 null 布局和 setBounds(...),因为这会导致程序非常死板,难以调试和改进。