2014-02-26 37 views
1

我有一個父類是CardLayout。我也有兩個具有文本字段和按鈕的子類(JPanel)。我想要的是,當用戶點擊第一張卡上的按鈕時,屏幕將變爲第二個屏幕。下面是代碼如何更換兒童卡內的cardLayout卡?

public class GUI extends JFrame { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private JPanel contentPane; 
private static LoginScreen login; 
private static DatabaseSelection selector; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       GUI frame = new GUI(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public GUI() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    setLocationRelativeTo(null); 
    contentPane = new JPanel(); 
    CardLayout card = new CardLayout(); 
    contentPane.setLayout(card); 
    login = new LoginScreen(); 
    selector = new DatabaseSelection(); 
    contentPane.add(login, "1"); 
    contentPane.add(selector, "2"); 
    setContentPane(contentPane); 
    card.show(contentPane, "1"); 
} 

}

public class LoginScreen extends JPanel implements ActionListener { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private JTextField userName; 
private JPasswordField passWord; 
private JTextField port; 
private JTextField host; 
private JLabel hostLabel; 
private JLabel portLabel; 
private Connection con; 

/** 
* Create the panel. 
*/ 
public LoginScreen() { 
    GridBagLayout gridBagLayout = new GridBagLayout(); 
    setLayout(gridBagLayout); 

    JLabel userNameLabel = new JLabel("Username: "); 
    GridBagConstraints gbc_userNameLabel = new GridBagConstraints(); 
    gbc_userNameLabel.anchor = GridBagConstraints.EAST; 
    gbc_userNameLabel.insets = new Insets(0, 0, 5, 5); 
    gbc_userNameLabel.gridx = 1; 
    gbc_userNameLabel.gridy = 1; 
    add(userNameLabel, gbc_userNameLabel); 

    userName = new JTextField(); 
    GridBagConstraints gbc_userName = new GridBagConstraints(); 
    gbc_userName.anchor = GridBagConstraints.EAST; 
    gbc_userName.insets = new Insets(0, 0, 5, 5); 
    gbc_userName.gridx = 2; 
    gbc_userName.gridy = 1; 
    add(userName, gbc_userName); 
    userName.setColumns(10); 

    JLabel passWordLabel = new JLabel("Password: "); 
    GridBagConstraints gbc_passWordLabel = new GridBagConstraints(); 
    gbc_passWordLabel.anchor = GridBagConstraints.EAST; 
    gbc_passWordLabel.insets = new Insets(0, 0, 5, 5); 
    gbc_passWordLabel.gridx = 1; 
    gbc_passWordLabel.gridy = 2; 
    add(passWordLabel, gbc_passWordLabel); 

    passWord = new JPasswordField(); 
    GridBagConstraints gbc_passWord = new GridBagConstraints(); 
    gbc_passWord.fill = GridBagConstraints.HORIZONTAL; 
    gbc_passWord.insets = new Insets(0, 0, 5, 5); 
    gbc_passWord.gridx = 2; 
    gbc_passWord.gridy = 2; 
    add(passWord, gbc_passWord); 

    JButton loginButon = new JButton("Login"); 
    loginButon.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    GridBagConstraints gbc_loginButon = new GridBagConstraints(); 
    gbc_loginButon.anchor = GridBagConstraints.CENTER; 
    gbc_loginButon.fill = GridBagConstraints.VERTICAL; 
    gbc_loginButon.gridheight = 4; 
    gbc_loginButon.gridx = 3; 
    gbc_loginButon.gridy = 1; 
    add(loginButon, gbc_loginButon); 
    loginButon.addActionListener(this); 

    hostLabel = new JLabel("Host: "); 
    GridBagConstraints gbc_hostLabel = new GridBagConstraints(); 
    gbc_hostLabel.anchor = GridBagConstraints.EAST; 
    gbc_hostLabel.insets = new Insets(0, 0, 5, 5); 
    gbc_hostLabel.gridx = 1; 
    gbc_hostLabel.gridy = 3; 
    add(hostLabel, gbc_hostLabel); 

    host = new JTextField(); 
    host.setText("localhost"); 
    GridBagConstraints gbc_host = new GridBagConstraints(); 
    gbc_host.insets = new Insets(0, 0, 5, 5); 
    gbc_host.fill = GridBagConstraints.HORIZONTAL; 
    gbc_host.gridx = 2; 
    gbc_host.gridy = 3; 
    add(host, gbc_host); 
    host.setColumns(10); 

    portLabel = new JLabel("Port:"); 
    GridBagConstraints gbc_portLabel = new GridBagConstraints(); 
    gbc_portLabel.anchor = GridBagConstraints.EAST; 
    gbc_portLabel.insets = new Insets(0, 0, 0, 5); 
    gbc_portLabel.gridx = 1; 
    gbc_portLabel.gridy = 4; 
    add(portLabel, gbc_portLabel); 

    port = new JTextField(); 
    port.setText("3306"); 
    GridBagConstraints gbc_port = new GridBagConstraints(); 
    gbc_port.insets = new Insets(0, 0, 0, 5); 
    gbc_port.fill = GridBagConstraints.HORIZONTAL; 
    gbc_port.gridx = 2; 
    gbc_port.gridy = 4; 
    add(port, gbc_port); 
    port.setColumns(10); 
    setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{userName, passWord, host, port, loginButon})); 
} 

public void actionPerformed(ActionEvent e) { 
    try { 
     ConnectionManager conMgr = new ConnectionManager(host.getText(), port.getText()); 
     char[] pass = passWord.getPassword(); 
     con = conMgr.getConnection(userName.getText(), new String(pass)); 
    } catch (ClassNotFoundException e1) { 
     System.out.println("Driver Error ..."); 
     //e1.printStackTrace(); 
    } catch (SQLException e1){ 
     System.out.println(e1.getMessage()); 
    } 
} 

}

+0

你必須使用'card.show(..)'在行動 – nachokk

+0

@nachokk但是'LoginScreen'沒有直接訪問'CardLayout' ...;) – MadProgrammer

回答

2

一般來說,你不應該。當前的「卡片」應該不知道導航的順序,相反,它應該能夠提供某種事件通知(例如通過ActionListener)或告訴某個其他經理需要切換爲下一個/一/目標「一卡通」 ......

說了這麼多,你「可能」,從「卡上的」父

LayoutManager layout = getParent().getLayout(); 
if (layout instanceof CardLayout) { 
    CardLayout cardLayout = (CardLayout)layout; 
    // switch panels... 
} 

提取的佈局管理器,但這樣會給您造成任何的問題時結束導航更改的順序,更好地使用某種「導航」管理器,可以從中央控制器爲您提供照顧。

由於@nachokk指出,這(控制器)將代表Mediator Pattern

+0

+1,但是當你的意思是「中央控制器」時,你的意思就像一箇中介,其中佈局中的不同組件是同事? – nachokk

+0

@nachokk噢,如果你想得到技術...是的... P – MadProgrammer

+0

我只是提出一個問題來理解你的答案,不知道你的意思 – nachokk