Java JFrame 意外怪异地删除组件

huangapple go评论95阅读模式
英文:

Java JFrame Removing Components Weirdly by Accident

问题

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);
    }
}
英文:

My Java JFrame project has been buggy with the components, removing everything JComboBox has used in space.

The video example of the bug in my program can be viewed visually here.

Main code is as below. You can replicate it by replacing the 3 Panels with random JCombobox(es) and using the main class to call it.

Any help would be appreciated.

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

答案1

得分: 0

不必费心制作自己的GUI界面,始终浏览Java文档,以检查是否有与您想使用的内容类似的部分。
不需要创建自己的"MenuSection",已经有了JTabbedPane,您可以更轻松地使用它们(无需出现闪烁和其他图形问题)。
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");
    }
英文:

Instead of bothering making your own GUI, always look through the java documentation in order to check if there is anything similar to what you want to use.
Instead of making your own "MenuSection", there already are JTabbedPanes, which you can use much more easily (without having flickering and other graphical issues).
https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html

Alternatively, you could use CardLayout.
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Example for 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");
    }

huangapple
  • 本文由 发表于 2020年4月11日 05:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定