我有一个父类,即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());
}
}
}
答案 0 :(得分:2)
一般来说,你不应该。当前的“卡”应该不知道导航的顺序,相反,它应该能够提供某种事件通知(例如通过ActionListener
)或告诉其他经理它需要是切换到下一个/上一个/目标“卡”......
话虽如此,您“可以”从“卡片”父母那里提取布局管理器
LayoutManager layout = getParent().getLayout();
if (layout instanceof CardLayout) {
CardLayout cardLayout = (CardLayout)layout;
// switch panels...
}
但是当导航顺序发生变化时,这将导致您无法解决问题,最好使用某种“导航”管理器,它可以从中央控制器处理它。
正如@nachokk指出的那样,这个(控制器)代表Mediator Pattern