[英]How should I drag a JLabel from one JPanel in a JFrame onto a JTextField in another JPanel in the same JFrame?
[英]How to remove a JPanel from JFrame and substitute it with another one
我有一個帶有兩個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);
}
}
您應該使用Card Layout
。 Card Layout
是專門設計用於在同一位置顯示多個組件的。 Card Layout
局將為最大的組件保留空間,然后您僅需根據需要交換組件。
閱讀Swing教程中有關如何使用卡布局的部分 ,以獲取一個工作示例的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.