2012-03-06 31 views
1

我想下一張卡片上顯示的用戶名用戶登錄後,並沒有運氣的文本字段值。如何顯示一個卡,其他卡上CardLayout交換

我使用CardLayout並定義了兩張卡片 - 一張卡片供用戶輸入名稱&密碼,第二張卡片顯示帶有登錄的用戶名稱的歡迎信息。我正在學習Java &擺動我自己而不是專家。任何幫助,包括修復這些代碼或參考資料供我閱讀,將不勝感激。

這裏是我當前的代碼(還需要添加代碼來更新歡迎屏幕上的文本字段):

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

public class CardTest 
{ 
    private JFrame frame; 
    public static final String CARD_LOGIN = "Card Login"; 
    public static final String CARD_DEPARTMENT = "Card Department"; 
    public static final String CARD_TEAM = "Card Team"; 
    public static JPanel cards; 
    public Employee employee = null; 
    public CardLogin cardLogin = null; 
    public CardDepartment cardDepartment = null; 
    public CardTeam cardTeam = null; 

    public CardTest() 
    { 
     Employee employee = new Employee(); 

     frame = new JFrame("Card Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationByPlatform(true); 

     cards = new JPanel(); 
     cards.setLayout(new CardLayout(20, 20)); 

     cardLogin = new CardLogin(this, employee); 
     cardDepartment = new CardDepartment(this, employee); 
     cardTeam = new CardTeam(this, employee); 

     cards.add(cardLogin, CARD_LOGIN);  
     cards.add(cardDepartment, CARD_DEPARTMENT); 
     cards.add(cardTeam, CARD_TEAM); 

     frame.getContentPane().add(cards); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void swapView(String key) 
    { 
     CardLayout cardLayout = (CardLayout) cards.getLayout(); 
     cardLayout.show(cards, key); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new CardTest(); 
      } 
     }); 
    } 
} 

class CardLogin extends JPanel 
{ 
    private ActionListener action; 
    private JTextField tfUsername= null; 
    Employee employee; 
    CardTest cardTest; 

    public CardLogin(CardTest cardTest, Employee employee) 
    { 
     this.cardTest = cardTest; 
     this.employee = employee; 
     init(); 
    } 

    private void init() 
    { 
     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 

     gc.fill = GridBagConstraints.HORIZONTAL; 

     JLabel lbCardName = new JLabel("Login Card "); 
     gc.gridx = 1; 
     gc.gridy = 0; 
     gc.gridwidth = 2; 
     panel.add(lbCardName, gc); 

     JLabel lbUsername = new JLabel("Username: "); 
     gc.gridx = 0; 
     gc.gridy = 2; 
     gc.gridwidth = 1; 
     panel.add(lbUsername, gc); 

     tfUsername = new JTextField(20); 
     gc.gridx = 1; 
     gc.gridy = 2; 
     gc.gridwidth = 2; 
     panel.add(tfUsername, gc); 

     JLabel lbPassword = new JLabel("Password: "); 
     gc.gridx = 0; 
     gc.gridy = 3; 
     gc.gridwidth = 1; 
     panel.add(lbPassword, gc); 

     JPasswordField pfPassword = new JPasswordField(20); 
     gc.gridx = 1; 
     gc.gridy = 3; 
     gc.gridwidth = 2; 
     panel.add(pfPassword, gc); 

     final JButton loginButton = new JButton("Login"); 

     action = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       if (tfUsername.getDocument().getLength() > 0) 
       { 
        employee.setUserName(tfUsername.getText()); 
        cardTest.swapView(cardTest.CARD_DEPARTMENT); 
       } 
      } 
     }; 

     loginButton.addActionListener(action); 

     JPanel bp = new JPanel(); 
     bp.add(loginButton); 

     setSize(640, 480); 

     add(panel, BorderLayout.CENTER); 
     add(bp, BorderLayout.PAGE_END); 
    } 
} 

class CardDepartment extends JPanel 
{ 
    private ActionListener actionNext; 
    private ActionListener actionLogout; 
    private JTextField tfDepartment= null; 
    private String department= null; 
    Employee employee; 
    CardTest cardTest; 
    CardLogin cardLogin; 

    public CardDepartment(CardTest cardTest, Employee employee) 
    { 
     this.employee = employee; 
     this.cardTest = cardTest; 
     init(); 
    } 

    private void init() 
    { 
     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 
     gc.fill = GridBagConstraints.HORIZONTAL; 

     JLabel lbCardName = new JLabel("Department Card "); 
     gc.gridx = 1; 
     gc.gridy = 0; 
     gc.gridwidth = 2; 
     panel.add(lbCardName, gc); 

     JLabel lbWelcome = new JLabel("Welcome "); 
     gc.gridx = 0; 
     gc.gridy = 2; 
     gc.gridwidth = 1; 
     panel.add(lbWelcome, gc); 

     gc.gridx = 1; 
     gc.gridy = 2; 
     gc.gridwidth = 2; 
     panel.add(new JLabel(employee.getUserName()), gc); 

     JLabel lbDepartment = new JLabel("Enter Department: "); 
     gc.gridx = 0; 
     gc.gridy = 3; 
     gc.gridwidth = 1; 
     panel.add(lbDepartment, gc); 

     tfDepartment = new JTextField(20); 
     gc.gridx = 1; 
     gc.gridy = 3; 
     gc.gridwidth = 2; 
     panel.add(tfDepartment, gc); 

     final JButton nextButton = new JButton("Next"); 
     actionNext = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       if (tfDepartment.getDocument().getLength() > 0) 
       { 
        department = tfDepartment.getText(); 
        cardTest.swapView(cardTest.CARD_TEAM); 
       } 
      } 
     }; 

     final JButton logoutButton = new JButton("Logout"); 
     actionLogout = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
        cardTest.swapView(cardTest.CARD_LOGIN); 
      } 
     }; 

     nextButton.addActionListener(actionNext); 
     logoutButton.addActionListener(actionLogout); 

     JPanel bp = new JPanel(); 
     bp.add(panel); 
     bp.add(logoutButton); 
     bp.add(nextButton); 

     add(panel); 
     add(bp); 
    } 
} 

class CardTeam extends JPanel 
{ 
    private ActionListener actionPrev; 
    private ActionListener actionLogout; 
    private JTextField tfTeam= null; 
    Employee employee; 
    CardTest cardTest; 

    public CardTeam(CardTest cardTest, Employee employee) 
    { 
     this.cardTest = cardTest; 
     this.employee = employee; 
     init(); 
    } 

    private void init() 
    { 
     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 
     gc.fill = GridBagConstraints.HORIZONTAL; 

     JLabel lbCardName = new JLabel("Team Card "); 
     gc.gridx = 1; 
     gc.gridy = 0; 
     gc.gridwidth = 2; 
     panel.add(lbCardName, gc); 

     JLabel lbWelcome = new JLabel("Welcome "); 
     gc.gridx = 0; 
     gc.gridy = 2; 
     gc.gridwidth = 1; 
     panel.add(lbWelcome, gc); 

     gc.gridx = 1; 
     gc.gridy = 2; 
     gc.gridwidth = 2; 
     panel.add(new JLabel(employee.getUserName()), gc); 

     JLabel lbDepartment = new JLabel("Department: "); 
     gc.gridx = 0; 
     gc.gridy = 3; 
     gc.gridwidth = 1; 
     panel.add(lbDepartment, gc); 

     gc.gridx = 1; 
     gc.gridy = 3; 
     gc.gridwidth = 2; 
     panel.add(new JLabel(employee.getDepartment()), gc); 

     JLabel lbTeam = new JLabel("Enter Team: "); 
     gc.gridx = 0; 
     gc.gridy = 4; 
     gc.gridwidth = 1; 
     panel.add(lbTeam, gc); 

     tfTeam = new JTextField(20); 
     gc.gridx = 1; 
     gc.gridy = 4; 
     gc.gridwidth = 2; 
     panel.add(tfTeam, gc); 

     final JButton prevButton = new JButton("Prev"); 
     actionPrev = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
        cardTest.swapView(cardTest.CARD_DEPARTMENT); 
      } 
     }; 

     final JButton logoutButton = new JButton("Logout"); 
     actionLogout = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
        cardTest.swapView(cardTest.CARD_LOGIN); 
      } 
     }; 

     prevButton.addActionListener(actionPrev); 
     logoutButton.addActionListener(actionLogout); 

     JPanel bp = new JPanel(); 
     bp.add(logoutButton); 
     bp.add(prevButton); 

     add(panel); 
     add(bp); 
    } 
} 

class Employee 
{ 
    private String userName = null; 
    private String department = null; 
    private String team = null; 

    public Employee() { 
     super(); 
    } 

    public String getUserName() { 
     return userName; 
    } 
    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
    public String getDepartment() { 
     return department; 
    } 
    public void setDepartment(String department) { 
     this.department = department; 
    } 
    public String getTeam() { 
     return team; 
    } 
    public void setTeam(String team) { 
     this.team = team; 
    } 
} 
+0

@AtifHameed:請不要檢查此[CardLayoutCode](http://pastebin.com/bc2WHRig),我已修改的位,這是在稍後階段更新事物的一種方式。請給我提供代碼或項目文件夾,以便我可以看看整個項目。 – 2012-03-09 06:02:12

回答

4

您聲明並作爲一個實例變量和CardLayoutLoginTest類的構造函數內第二次在你的代碼初始化JPanel cards兩次,一次。 和你是一步步啄,所以最好你把你的JPanel被一個到CardLayout作爲Cards,一個。因爲如果LoginFails不需要它們,只要登錄有效時,就可以添加任何你想添加到CardLayout的東西。

不要使用setLocationByPlatform(true);代替setLocationRelativeTo(null);。前者更好,因爲通過@Andrew湯普森在他的職位之一,How to best position Swing GUI's 我修改了你的代碼解釋,確實有一看,讓我知道,如果它是短暫的東西。

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

public class CardTest 
{ 
    private JFrame frame; 
    public static final String CARD_LOGIN = "Card Login"; 
    public static final String CARD_WELCOME = "Card Welcome"; 
    public static JPanel cards; 
    public CardLogin cardLogin = null; 

    public CardTest() 
    { 
     frame = new JFrame("Card LOGIN"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationByPlatform(true); 

     cards = new JPanel(); 
     cards.setLayout(new CardLayout(20, 20)); 

     cardLogin = new CardLogin(this); 
     cards.add(cardLogin, CARD_LOGIN);  

     frame.getContentPane().add(cards); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void swapView(String key) 
    { 
     CardLayout cardLayout = (CardLayout) cards.getLayout(); 
     cardLayout.show(cards, key); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new CardTest(); 
      } 
     }); 
    } 
} 

class CardLogin extends JPanel 
{ 
    private ActionListener action; 
    CardTest cardLayoutLoginTest; 
    /* 
    * Made JTextField an instance variable so that 
    * ActionListener can access it or you can make 
    * it final. 
    */ 
    private JTextField tfUsername= null; 
    private String username = null; 

    public CardLogin(CardTest cardLayoutLoginTest) 
    { 
     this.cardLayoutLoginTest = cardLayoutLoginTest; 
     init(); 
    } 

    private void init() 
    { 

     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 

     gc.fill = GridBagConstraints.HORIZONTAL; 

     JLabel lbUsername = new JLabel("Username: "); 
     gc.gridx = 0; 
     gc.gridy = 0; 
     gc.gridwidth = 1; 
     panel.add(lbUsername, gc); 

     tfUsername = new JTextField(20); 
     gc.gridx = 1; 
     gc.gridy = 0; 
     gc.gridwidth = 2; 
     panel.add(tfUsername, gc); 

     JLabel lbPassword = new JLabel("Password: "); 
     gc.gridx = 0; 
     gc.gridy = 1; 
     gc.gridwidth = 1; 
     panel.add(lbPassword, gc); 

     JPasswordField pfPassword = new JPasswordField(20); 
     gc.gridx = 1; 
     gc.gridy = 1; 
     gc.gridwidth = 2; 
     panel.add(pfPassword, gc); 

     final JButton loginButton = new JButton("Login"); 

     action = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 

        // Here need code to update text filed of welcome card 
       /* 
       * Here we are first checking if there is any text inside 
       * the JTextField for USERNAME, if found we will send it to the 
       * next JPanel which will be serving as a new Card. 
       */ 
       if (tfUsername.getDocument().getLength() > 0) 
       { 
        username = tfUsername.getText(); 
        CardWelcome cardWelcome = new CardWelcome(cardLayoutLoginTest.cardLogin); 
        CardTest.cards.add(cardWelcome, cardLayoutLoginTest.CARD_WELCOME); 
        cardLayoutLoginTest.swapView(cardLayoutLoginTest.CARD_WELCOME); 
       } 
      } 
     }; 

     loginButton.addActionListener(action); 

     JPanel bp = new JPanel(); 
     bp.add(loginButton); 

     /*set size of the frame*/ 
     setSize(640, 480); 

     add(panel, BorderLayout.CENTER); 
     add(bp, BorderLayout.PAGE_END); 

    } 

    public String getUserName() 
    { 
     return username; 
    } 
} 

class CardWelcome extends JPanel 
{ 
    private JTextField textField; 
    private CardLogin cardLogin; 

    public CardWelcome(CardLogin cl) 
    { 
     cardLogin = cl; 
     init(); 
    } 

    private void init() 
    { 
     setLayout(new GridLayout(1, 1)); 
     JLabel userLabel = new JLabel("Welcome "); 
     textField = new JTextField(); 
     textField.setText(cardLogin.getUserName()); 
     System.out.println("UserName : " + cardLogin.getUserName()); 

     add(userLabel); 
     add(textField); 
    } 
} 

少量樣品程序在運行時更新JLabel

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

public class UpdateLabel extends JFrame 
{ 
    private int count = 0; 
    public UpdateLabel() 
    { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationByPlatform(true); 

     final JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout()); 

     final JLabel label = new JLabel("JLabel " + count); 
     JButton button = new JButton("UPDATE JLABEL"); 
     button.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       count++; 
       label.setText("JLabel " + count); 
       contentPane.revalidate(); // sometimes you require to do this and the below line. 
       contentPane.repaint(); 
      } 
     }); 

     contentPane.add(label, BorderLayout.CENTER); 
     contentPane.add(button, BorderLayout.PAGE_END); 

     setContentPane(contentPane); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     Runnable runnable = new Runnable() 
     { 
      public void run() 
      { 
       new UpdateLabel(); 
      } 
     }; 
     SwingUtilities.invokeLater(runnable); 
    } 
} 
+0

@Arif哈米德這裏的知識是回答你的問題,請不要忘記接受這個答案 – mKorbel 2012-03-06 18:21:50

+0

@Gagandeep巴厘島,感謝您的輸入反應迅速和重複上的評論宣言。恐怕這個解決方案並不適合真正的項目,因爲我有7張卡GUI創建和用戶的時間加入到甲板可以切換到任何卡和一些數據將被傳遞形式的有效卡交換卡,甚至註銷從任何卡都將顯示登錄卡。這是我創建的一個小程序,用於測試將值從一張卡片傳遞到另一張卡片的功能。如何處理此示例中的CardLayout多實例創建情況,如@mKorbel所述? – 2012-03-07 12:59:19

+0

@ArifHameed:請參閱,而不是將值傳遞給下一張卡片,相反,這是您必須執行的操作。無論你在輸入什麼內容,這些輸入可以基本上與某些事情相關(對於信息:學生,員工,工作人員或某事),所以你可以做的是爲這個實體創建一個單獨的類。因此,當您從登錄到歡迎窗口時,將特定實體的用戶名和密碼保存在相應的類變量中。 – 2012-03-07 13:11:17

3

你re_create CardLayout例如每次發射時JButton's Action,創建只有一次時間,例如

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

public class OnTheFlyImageTest extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JPanel cardPanel; 
    private CardLayout cardLayout; 

    public OnTheFlyImageTest() { 
     JPanel cp = new JPanel(new BorderLayout()); 
     cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     cardLayout = new CardLayout(5, 5); 
     cardPanel = new JPanel(cardLayout); 
     cp.add(cardPanel); 
     for (int i = 0; i < 5; i++) {// Create random panels for testing. 
      String name = "ImagePanel" + (i + 1); 
      String image = (i & 1) == 0 ? "foo.gif" : "bar.gif"; 
      Color clr = (i & 1) == 0 ? Color.red : Color.blue; 
      ImagePanel imgPanel = new ImagePanel(name, image, clr); 
      cardPanel.add(imgPanel, name); 
      cardLayout.addLayoutComponent(imgPanel, name); 
     } 
     JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5)); 
     JButton prevButton = new JButton("< Previous"); 
     prevButton.setActionCommand("Previous"); 
     prevButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       cardLayout.previous(cardPanel); 
      } 
     }); 
     buttonPanel.add(prevButton); 
     JButton nextButton = new JButton("Next >"); 
     nextButton.setActionCommand("Next"); 
     nextButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       cardLayout.next(cardPanel); 
      } 
     }); 
     buttonPanel.add(nextButton); 
     JPanel temp = new JPanel(new BorderLayout()); 
     temp.add(buttonPanel, BorderLayout.LINE_END); 
     cp.add(temp, BorderLayout.SOUTH); 
     setContentPane(cp); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setTitle("Test"); 
     pack(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new OnTheFlyImageTest().setVisible(true); 
      } 
     }); 
    } 
} 

class ImagePanel extends JPanel { 

    private static final long serialVersionUID = 1L; 
    private String imgString; 
    private JLabel imgLabel; 

    public ImagePanel(String name, String imgString, Color backGround) { 
     setName(name); 
     this.imgString = imgString; 
     setLayout(new BorderLayout()); 
     setBackground((backGround)); 
     // Ensure size is correct even before any image is loaded. 
     setPreferredSize(new Dimension(400, 300)); 
    } 

    @Override 
    public void setVisible(boolean visible) { 
     if (visible) { 
      System.err.println(getName() + ": Loading and adding image"); 
      ImageIcon icon = new ImageIcon(imgString); 
      imgLabel = new JLabel(icon); 
      add(imgLabel); 
     } 
     super.setVisible(visible); 
     if (!visible) { // Do after super.setVisible() so image doesn't "disappear". 
      System.err.println(getName() + ": Removing image"); 
      if (imgLabel != null) { // Before display, this will be null 
       remove(imgLabel); 
       imgLabel = null; // Hint to GC that component/image can be collected. 
      } 
     } 
    } 
} 
+0

+1,你的代碼總是添加的東西對我:-) – 2012-03-06 18:12:22

相關問題