简体   繁体   English

如何从JFrame中删除一个JPanel并将其替换为另一个

[英]How to remove a JPanel from JFrame and substitute it with another one

I have a JFrame with two JPanel. 我有一个带有两个JPanel的JFrame。 Two buttons. 两个按钮。 The app starts with no panels, just buttons. 该应用程序开始时没有面板,只有按钮。 Upon pressing a button I want I display one panel, and then upon pressing another button I substitute one panel with another one and vice versa. 按下按钮时,我希望显示一个面板,然后按下另一个按钮时,我将一个面板替换为另一个面板,反之亦然。 I have this code, but it doesn't really work. 我有这段代码,但实际上并没有用。 The panels (when one button is clicked and then another) appear on top of each other. 面板(单击一个按钮然后单击另一个按钮时)彼此重叠。 Any help would be appreciated! 任何帮助,将不胜感激! Thank you in advance! 先感谢您!

Main.java Main.java

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

public class Main {

    static JButton enterDataButton = new JButton("Enter Data");
    static JButton viewDataButton = new JButton("View Data");


    static int yPosition = 10;
    static int xLabelPosition = 20;
    static int xFieldPosition = 20;
    static int labelWidth = 200;
    static int fieldWidth = 200;

    public static void main (String[] args) {

        final JFrame frame = new JFrame("SimpleTrans Main Window");
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setBounds(100, 100, 1200, 800);
        frame.getContentPane().setLayout(null);

        JPanel mainPanel = new JPanel();

        enterDataButton.setBounds(xFieldPosition, yPosition, fieldWidth, 30);
        enterDataButton.setForeground(Color.DARK_GRAY);

        final InputDataArea inputDataArea = new InputDataArea();
        final ViewDataArea viewDataArea = new ViewDataArea();

        enterDataButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("enterData Button Pressed!");
                frame.remove(viewDataArea);
                inputDataArea.InputDataArea(frame.getContentPane());
                frame.add(inputDataArea);
                frame.getContentPane().invalidate();
                frame.getContentPane().validate();
                frame.getContentPane().repaint();
            }
        });

        viewDataButton.setBounds(xFieldPosition + fieldWidth, yPosition, fieldWidth, 30);
        viewDataButton.setForeground(Color.DARK_GRAY);

        viewDataButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("viewData Button Pressed!");
                frame.remove(inputDataArea);
                viewDataArea.ViewDataArea(frame.getContentPane());
                frame.add(viewDataArea);
                frame.getContentPane().revalidate();
                frame.getContentPane().validate();
                frame.getContentPane().repaint();
            }
        });

        frame.getContentPane().add(enterDataButton);
        frame.getContentPane().add(viewDataButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

InputDataArea.java InputDataArea.java

import javax.swing.*;
import java.awt.*;

public class InputDataArea extends JPanel {

    private DataTextField textFieldTripNumber = new DataTextField();
    private DataTextField textFieldExportCountry = new DataTextField();
    private DataTextField textFieldDestinationCountry = new DataTextField();
    private DataTextField textFieldPriceFixed = new DataTextField();


    JLabel textFieldLabelTripNumber = new JLabel("Trip Number:");
    JLabel textFieldLabelExportCountry = new JLabel("Export Country:");
    JLabel textFieldLabelDestinationCountry = new JLabel("Destination Country:");
    JLabel textFieldLabelPriceFixed = new JLabel("Price Fixed:");

    JButton saveButton = new JButton("Save");
    JButton emptyButton = new JButton("Empty");


    int yPosition = 60;
    int xLabelPosition = 20;
    int xFieldPosition = 220;
    int labelWidth = 200;
    int fieldWidth = 200;

    public void InputDataArea(Container pane) {
        textFieldLabelTripNumber.setBounds(xLabelPosition, yPosition, labelWidth, 20);
        pane.add(textFieldLabelTripNumber);

        textFieldTripNumber.setColumns(20);
        textFieldTripNumber.setBounds(xFieldPosition, yPosition, fieldWidth, 20);
        pane.add(textFieldTripNumber);


        textFieldLabelExportCountry.setBounds(xLabelPosition, yPosition + 30, labelWidth, 20);
        pane.add(textFieldLabelExportCountry);

        textFieldExportCountry.setColumns(20);
        textFieldExportCountry.setBounds(xFieldPosition, yPosition + 30, fieldWidth, 20);
        pane.add(textFieldExportCountry);


        textFieldLabelDestinationCountry.setBounds(xLabelPosition, yPosition + 60, labelWidth, 20);
        pane.add(textFieldLabelDestinationCountry);

        textFieldDestinationCountry.setColumns(20);
        textFieldDestinationCountry.setBounds(xFieldPosition, yPosition + 60, fieldWidth, 20);
        pane.add(textFieldDestinationCountry);


        textFieldLabelPriceFixed.setBounds(xLabelPosition, yPosition + 90, labelWidth, 20);
        pane.add(textFieldLabelPriceFixed);

        textFieldPriceFixed.setColumns(20);
        textFieldPriceFixed.setBounds(xFieldPosition, yPosition + 90, fieldWidth, 20);
        pane.add(textFieldPriceFixed);


        saveButton.setBounds(xFieldPosition, yPosition + 130, fieldWidth, 50);
        saveButton.setForeground(Color.GREEN);
        pane.add(saveButton);

        emptyButton.setBounds(xFieldPosition + fieldWidth, yPosition + 130, fieldWidth, 50);
        emptyButton.setForeground(Color.RED);
        pane.add(emptyButton);
    }
}

ViewDataArea.java ViewDataArea.java

import javax.swing.*;
import java.awt.*;


public class ViewDataArea extends JPanel {

    private DataTextField textFieldTripNumber = new DataTextField();
    private DataTextField textFieldExportCountry = new DataTextField();
    private DataTextField textFieldDestinationCountry = new DataTextField();
    private DataTextField textFieldPriceFixed = new DataTextField();


    JLabel textFieldLabelViewData = new JLabel("ViewData");

    JButton saveButton = new JButton("Save");
    JButton emptyButton = new JButton("Empty");


    int yPosition = 60;
    int xLabelPosition = 20;
    int xFieldPosition = 220;
    int labelWidth = 200;
    int fieldWidth = 200;

    public void ViewDataArea(Container pane) {
        textFieldLabelViewData.setBounds(xLabelPosition, yPosition, labelWidth, 20);
        pane.add(textFieldLabelViewData);

    }
}

DataTextField.java DataTextField.java

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class DataTextField extends JTextField {
    private List<JTextField> textFields = new ArrayList<JTextField>();
    public void addTextField(JTextField textField) {
        textFields.add(textField);
    }
}

YOu should be using a Card Layout . 您应该使用Card Layout A Card Layout is specifically designed to display multiple components in the same place. Card Layout是专门设计用于在同一位置显示多个组件的。 The Card Layout will reserve space for the largest component and then you just swap components as required. Card Layout局将为最大的组件保留空间,然后您仅需根据需要交换组件。

Read the section from the Swing tutorial on How to Use Card Layout for more information at a working example. 阅读Swing教程中有关如何使用卡布局的部分 ,以获取一个工作示例的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM