2013-05-05 116 views
1

因此,對於我正在採取的類,我需要創建一個ATM系統。所以,我決定有2個面板。一個主面板,所有進程都在其中進行,一個選項面板顯示用戶列出的選項。如上所述,問題是我似乎無法將主面板移除,所以我實際上可以將其替換爲面板,例如創建帳戶屏幕。事實上,所發生的只是終端窗口顯示。完全空白。據我檢查,按鈕事件觸發,它甚至超過了刪除功能。似乎無法得到.remove工作

在任何情況下,我似乎無法弄清楚問題所在,也許是因爲按下的按鈕也被移除了面板。這是相關的代碼。

public class AccountSystem extends JFrame implements ActionListener 
{ 
    public static Account currentuser = new Account(); //This is so that the methods know which account is currently logged in so they can perform operations on it. 
    public static int count=0; 
    public static Account acc[] = new Account[1000]; 
    public static String parts[] = new String[3]; 
    private JButton login, logout, createacc, deposit1, deposit2, withdraw1, withdraw2, transfer1, transfer2, nevermind; 
    private JPanel options, mainarea, titlecard; 
    private JTextField username, password, transfer, depositarea, withdrawarea, retypearea; 
    private JLabel userprompt, depositprompt, withdrawpromt, balancedisp, passwordprompt, mainmessage, title; 
    private String newuser, newpass, newpassconfirm; 
    BorderLayout borderlayout; 
    GridLayout gridlayout; 
    public AccountSystem() 
    { 
     borderlayout = new BorderLayout(); 
     borderlayout.setHgap(5); 
     borderlayout.setVgap(5); 
     //Establishing our buttons here. 
     JButton login = new JButton("Login"); 
     login.addActionListener(this); 
     JButton createacc = new JButton("New Account"); 
     createacc.addActionListener(this); 

     //Establishing our panels here. 
     JPanel options = new JPanel(); 
     JPanel mainarea = new JPanel(); 
     JPanel titlecard = new JPanel(); 
     //Establishing our JLabel here. 
     JLabel userprompt = new JLabel("Username: "); 
     JLabel passwordprompt = new JLabel("Password: "); 
     JLabel title = new JLabel(" LOGIN "); 
     //Establishing our textfields here. 
     JTextField username = new JTextField(20); 
     JTextField password = new JTextField(20); 
     JTextField transfer = new JTextField(20); 
     JTextField withdrawarea = new JTextField(20); 
     //Building the GUI here. 
     titlecard.setSize(500,50); 
     titlecard.setLocation (0,0); 
     mainarea.setSize(300,450); 
     mainarea.setLocation(0,50); 
     options.setSize(150,450); 
     options.setLocation(300,50); 
     titlecard.add(title); 
     mainarea.add(userprompt); 
     mainarea.add(username); 
     mainarea.add(passwordprompt); 
     mainarea.add(password); 
     mainarea.add(login); 
     mainarea.add(createacc); 
     getContentPane().setLayout(null); 
     getContentPane().add(titlecard); 
     getContentPane().add(mainarea); 
     getContentPane().add(options); 
    } 
} 

public void actionPerformed (ActionEvent e) 
{ 
    if ((e.getActionCommand()).equals("Login")) 
    { 
     login(); 
    } 
    else if ((e.getActionCommand()).equals("New Account")) 
    { 
     accountmaker(); 
    } 
    else if ((e.getActionCommand()).equals("Deposit Funds")) 
    { 
     deposit(); 
    } 
    else if ((e.getActionCommand()).equals("Withdraw Funds")) 
    { 
     withdraw(); 
    } 
    else if ((e.getActionCommand()).equals("Withdraw")) 
    { 
     withdrawprocedure(); 
    } 
    else if ((e.getActionCommand()).equals("Create Account")) 
    { 
     accountprocedure(); 
    } 
} 

public void accountmaker() //This is the screen where the user creates the account they want to. Of course, something needed to be done to differentiate this screen and the login screen. 
{ 
    currentuser = null; //This is so that the program doesn't get somehow confused when dealing with multiple uses of the program. 
    getContentPane().remove(mainarea); 
    title.setText("Create New Account"); 
    mainarea = new JPanel(); 
    JLabel userprompt = new JLabel ("Username: "); 
    JLabel passwordprompt = new JLabel("Password: "); 
    JLabel retype = new JLabel ("Retype: "); //This is what makes it different from the login screen 
    createacc = new JButton ("Create Account"); 
    createacc.addActionListener(this); 
    JButton nevermind = new JButton("Cancel"); 
    nevermind.addActionListener(this); 
    JTextField username = new JTextField(20); 
    JTextField password = new JTextField(20); 
    retypearea = new JTextField(20); 
    mainarea.setSize(300,500); 
    mainarea.setLocation(0,0); 
    mainarea.add(userprompt); 
    mainarea.add(username); 
    mainarea.add(passwordprompt); 
    mainarea.add(password); 
    mainarea.add(retype); 
    mainarea.add(retypearea); 
    getContentPane().add(mainarea); 
    getContentPane().invalidate(); 
    getContentPane().validate(); 
    getContentPane().repaint(); 
} 

這是程序使用的帳戶類。

public class Account 
{ 
    private String username; 
    private String password; 
    private double balance=0; 
    public void deposit (double deposit) 
    { 
     balance += deposit; 
    } 
    public void withdraw (double withdraw) 
    { 
     balance -= withdraw; 
    } 
    public void setBalance (double newbalance) 
    { 
     balance = newbalance; 
    } 
    public void setUsername (String newusername) 
    { 
     username = newusername; 
    } 
    public void setPassword (String newpassword) 
    { 
     password = newpassword; 
    } 
    public String getUsername() 
    { 
     return username; 
    } 
    public double getbalance() 
    { 
     return balance; 
    } 
    public String getpassword() 
    { 
     return password; 
    } 
} 
+0

爲什麼不執行選項窗口的[的JDialog(http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html)? – nullptr 2013-05-05 03:58:59

+0

這聽起來像你可能想看看'CardLayout'。這是讓你的框架在不同時間顯示不同組件的方法。閱讀教程http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html – ApproachingDarknessFish 2013-05-05 04:23:58

回答

2

如果我理解正確..你基本上需要用某個事件的帳戶面板來替換主面板!爲了保持所有組件 主面板:

我寫的示例代碼

在此代碼..我創建了4個板即 組件面板(請根據您的項目修改)中包含的文本標籤「主面板」和它的邊框塗成紅色 帳戶面板:包含文字「帳戶面板」的標籤,其邊框被塗成綠色 選項面板:其邊界空面板漆成藍色

兩個按鈕: 賬戶按鈕:哪個應該將主面板替換爲帳戶面板 主要按鈕:應該用主面板替換帳戶面板

使用GridBagLayout,您只需將主面板和帳戶面板置於相同的位置,即gridx = 0和gridy = 0。首先顯示主面板。在事件「帳戶按鈕」中,將主面板的可見性設置爲false並且帳戶面板爲true。對於事件「主要按鈕」,請使用vica。 GridBagLayout是一個夢幻般的動態佈局,它通過它的空閒空間來管理空間而沒有任何失真。

public class SwingSolution extends JFrame implements ActionListener 
{ 
    private JPanel componentPanel = null; 
    private JPanel mainPanel = null;  
    private JLabel mainLabel = null;  
    private JPanel optionPanel = null; 
    private JPanel accountPanel = null; 
    private JLabel accountLabel = null; 
    private JButton replaceToAccountPanel = null; 
    private JButton replaceToMainPanel = null; 

    private final static String MAIN_TO_ACCOUNT = "MainToAccount"; 
    private final static String ACCOUNT_TO_MAIN = "AccountToMain"; 

    public JPanel getComponentPanel() 
    { 
     if(null == componentPanel) 
     { 
      componentPanel = new JPanel(); 
      GridBagLayout gridBagLayout = new GridBagLayout(); 
      componentPanel.setLayout(gridBagLayout); 

      GridBagConstraints constraint = new GridBagConstraints(); 
      constraint.insets = new Insets(10, 10, 10, 10); 

      mainPanel = new JPanel();   
      constraint.gridx = 0; 
      constraint.gridy = 0; 
      mainPanel.setMinimumSize(new Dimension(100, 50)); 
      mainPanel.setPreferredSize(new Dimension(100, 50)); 
      mainPanel.setMaximumSize(new Dimension(100, 50)); 
      mainPanel.setBorder(
        BorderFactory.createLineBorder(Color.RED)); 

      mainLabel = new JLabel("Main Panel"); 
      mainPanel.add(mainLabel); 
      componentPanel.add(mainPanel, constraint); 

      accountPanel = new JPanel();   
      constraint.gridx = 0; 
      constraint.gridy = 0; 
      accountPanel.setMinimumSize(new Dimension(100, 50)); 
      accountPanel.setPreferredSize(new Dimension(100, 50)); 
      accountPanel.setMaximumSize(new Dimension(100, 50)); 
      accountPanel.setBorder(
        BorderFactory.createLineBorder(Color.GREEN)); 

      accountLabel = new JLabel("Account Panel"); 
      accountPanel.add(accountLabel); 

      componentPanel.add(accountPanel, constraint); 

      optionPanel = new JPanel();   
      constraint.gridx = 0; 
      constraint.gridy = 1; 
      optionPanel.setMinimumSize(new Dimension(100, 50)); 
      optionPanel.setPreferredSize(new Dimension(100, 50)); 
      optionPanel.setMaximumSize(new Dimension(100, 50)); 
      optionPanel.setBorder(
        BorderFactory.createLineBorder(Color.BLUE)); 
      componentPanel.add(optionPanel, constraint); 

      replaceToAccountPanel = new JButton("Account Button"); 
      replaceToAccountPanel.setName(MAIN_TO_ACCOUNT); 
      constraint.gridx = 0; 
      constraint.gridy = 2; 
      replaceToAccountPanel.setSize(new Dimension(800, 30)); 
      replaceToAccountPanel.addActionListener(this); 
      componentPanel.add(replaceToAccountPanel, constraint); 

      replaceToMainPanel = new JButton("Main Button"); 
      replaceToMainPanel.setName(ACCOUNT_TO_MAIN); 
      constraint.gridx = 1; 
      constraint.gridy = 2; 
      replaceToMainPanel.setMinimumSize(new Dimension(800, 30)); 
      replaceToMainPanel.addActionListener(this); 
      componentPanel.add(replaceToMainPanel, constraint); 
     }  
     return componentPanel; 
    } 

    public void actionPerformed (ActionEvent evt) 
    { 
     JButton buttonClicked = (JButton) evt.getSource(); 
     if(buttonClicked != null) 
     { 
      if(buttonClicked.getName().equals(MAIN_TO_ACCOUNT)) 
      { 
       mainPanel.setVisible(false); 
       accountPanel.setVisible(true); 
      } 
      else if(buttonClicked.getName().equals(ACCOUNT_TO_MAIN)) 
      { 
       mainPanel.setVisible(true); 
       accountPanel.setVisible(false); 
      } 
     }  
    } 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame(); 
     SwingSolution main = new SwingSolution(); 

     frame.setTitle("Simple example"); 
     frame.setSize(400, 300); 
     frame.setLocationRelativeTo(null); 

     frame.setContentPane(main.getComponentPanel()); 

     frame.setVisible(true); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 
} 
+0

我可以使用普通面板和沒有網格佈局來做到這一點嗎?還是需要它? – 2013-05-05 07:02:06

+0

當然你可以用其他佈局來做。只要記住,當你將面板的可見性設置爲false時,它只是隱藏在屏幕上。我所做的是提供了邏輯,即隱藏一個面板並在其頂部顯示另一個面板。 – Mady 2013-05-05 07:07:40

+0

只需記住當您從屏幕上隱藏一個面板時,一些佈局管理器會嘗試利用空白空間,這可能會扭曲UI。 – Mady 2013-05-05 07:16:49

相關問題