2015-05-23 50 views
0

我有一個帶有兩個JPanel的JFrame。兩個按鈕。該應用程序從沒有面板開始,只有按鈕。按下按鈕後,我想顯示一個面板,然後按另一個按鈕時,我用另一個面板替換一個面板,反之亦然。我有這個代碼,但它並沒有真正的工作。面板(當一個按鈕被點擊,然後是另一個按鈕時)出現在彼此的頂部。任何幫助,將不勝感激! 提前謝謝!如何從JFrame中刪除一個JPanel並用另一個替換它

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個面板和2個按鈕創建一個框架就足以證明您的問題。 – user1803551

+0

好的。剛想到我會更精確。 –

+0

[This](http://stackoverflow.com/help/mcve)會幫助你。 – user1803551

回答

2

你應該使用一個Card Layout。 A Card Layout專門設計用於在同一地點顯示多個組件。 Card Layout將爲最大的組件保留空間,然後根據需要交換組件。

閱讀Swing教程中有關How to Use Card Layout的部分以獲取更多信息。

+0

謝謝!我會嘗試的!非常感謝! –

相關問題