我有一个带有两个JPanel的JFrame。两个按钮。该应用程序首先没有面板,只有按钮。按下按钮后,我希望显示一个面板,然后按下另一个按钮,我将一个面板替换为另一个面板,反之亦然。我有这个代码,但它并没有真正起作用。面板(单击一个按钮,然后单击另一个按钮)显示在彼此之上。任何帮助,将不胜感激! 提前谢谢!
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
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
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
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);
}
}
答案 0 :(得分:2)
你应该使用Card Layout
。 Card Layout
专门用于在同一位置显示多个组件。 Card Layout
将为最大的组件保留空间,然后根据需要交换组件。
阅读How to Use Card Layout上Swing教程中的部分,以获取更多信息。