我的Java JFrame项目在组件方面存在漏洞,删除了JComboBox在空间中使用的所有内容。

可以在视觉上here观看程序中错误的视频示例。

主要代码如下。您可以通过用random JCombobox(es)替换3个面板并使用主类来调用它来复制它。

任何帮助,将不胜感激。

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DisplayPage extends JFrame implements ActionListener {

    EnterDetailsPage newRecord;
    ViewMore viewMoreRecord;
    ViewPage viewRecord;
    JPanel contentPanel = new JPanel(new FlowLayout());

    public DisplayPage(){
        setLayout(new BorderLayout());
        setSize(900, 600);
        setTitle("Add Flight Details");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setPage(1);
        MenuSection menuSection = new MenuSection();

        //Add action listener to buttons
        menuSection.addNewButton.addActionListener(this::addActionPerformed);
        menuSection.updateButton.addActionListener(this::updateActionPerformed);
        menuSection.viewButton.addActionListener(this::viewActionPerformed);

        add("West", menuSection.getMainMenu());
        add("Center", contentPanel);
        setVisible(true);
    }

    public void setPage(int selection){
        contentPanel.removeAll();
        contentPanel.revalidate();
        //contentPanel.repaint();
        switch (selection) {
            case 1:
                newRecord = new EnterDetailsPage();
                contentPanel = newRecord.getTotalPanel();
                break;
            case 2:
                viewMoreRecord = new ViewMore();
                contentPanel = viewMoreRecord.getViewMorePanel();
                break;
            case 3:
                viewRecord = new ViewPage();
                contentPanel = viewRecord.getUpdatePagePanel();
                break;
        }
        add("Center", contentPanel);
    }

    public void actionPerformed(ActionEvent e){

    }

    public void addActionPerformed(ActionEvent e){
        setPage(1);
    }

    public void updateActionPerformed(ActionEvent e){
        setPage(2);
    }

    public void viewActionPerformed(ActionEvent e){
        setPage(3);
    }
}

最佳答案

不必费心制作自己的GUI,而是始终仔细阅读java文档,以检查是否有与您要使用的内容相似的内容。
现在已经有JTabbedPanes可以代替使用自己的“ MenuSection”了,您可以更加轻松地使用它(不会出现闪烁和其他图形问题)。
https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html

或者,您可以使用CardLayout。
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

CardLayout示例:

public class DisplayPage extends JFrame implements ActionListener {
 ...

    CardLayout cl;
 ...

    public DisplayPage(){
     ...

        cl = new CardLayout();
        contentPanel.setLayout(cl);
     ...

        contentPanel.add(newRecord,"newRecord");
        contentPanel.add(viewMoreRecord,"viewMoreRecord");
        contentPanel.add(viewRecord,"viewRecord");
     ...
        cl.show(contentPanel,"newRecord");
     }


    public void addActionPerformed(ActionEvent e){
        cl.show(contentPanel,"newRecord");
    }

    public void updateActionPerformed(ActionEvent e){
        cl.show(contentPanel,"viewMoreRecord");
    }

    public void viewActionPerformed(ActionEvent e){
        cl.show(contentPanel,"viewRecord");
    }

07-24 16:02