[英]How can I change cardLayout card within child card?
我有一個父類是CardLayout。 我也有兩個具有文本字段和按鈕的子類(JPanel)。 我想要的是,當用戶單擊第一張卡片上的按鈕時,屏幕將切換到第二張屏幕。 這是代碼
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());
}
}
}
一般來說,您不應該這樣做。 當前的“卡片”應該不知道導航的順序,相反,它應該能夠提供某種事件通知(例如通過ActionListener
),或者告訴其他管理者下一次需要切換它/上一個/目標“卡片” ...
話雖如此,您“可以”從“卡的”父級中提取布局管理器
LayoutManager layout = getParent().getLayout();
if (layout instanceof CardLayout) {
CardLayout cardLayout = (CardLayout)layout;
// switch panels...
}
但是,當導航順序更改時,這將不會給您帶來任何麻煩,最好使用某種“導航”管理器,該管理器可以從中央控制器為您解決。
正如@nachokk指出的那樣,此(控制器)將代表中介者模式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.