我有三个不同的框架:
如果我点击登录按钮登录页面必须打开(即LoginForm.java),如果我点击注册按钮注册页面必须打开
//welcome.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class welcome extends JFrame
{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
welcome window = new welcome();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public welcome() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnLogIn = new JButton("LOG IN");
btnLogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnLogIn.setBounds(200, 69, 117, 25);
frame.getContentPane().add(btnLogIn);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnRegister.setBounds(200, 138, 117, 25);
frame.getContentPane().add(btnRegister);
JLabel lblNewToSvk = new JLabel("New to SVK Polytechnic ?");
lblNewToSvk.setBounds(12, 143, 191, 15);
frame.getContentPane().add(lblNewToSvk);
}
}
`//LoginForm.java
import java.awt.EventQueue;
import javax.swing.JScrollPane;
public class LoginForm extends JPanel implements ItemListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JTextField textField;
private JPasswordField pwdJjh;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginForm window = new LoginForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblStudentLogin = new JLabel("Login ");
lblStudentLogin.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 30));
lblStudentLogin.setHorizontalAlignment(SwingConstants.CENTER);
lblStudentLogin.setBounds(94, 12, 265, 36);
frame.getContentPane().add(lblStudentLogin);
JLabel lblUserName = new JLabel("USER NAME");
lblUserName.setFont(new Font("Dialog", Font.BOLD, 15));
lblUserName.setBounds(12, 74, 111, 15);
frame.getContentPane().add(lblUserName);
textField = new JTextField();
textField.setFont(new Font("Dialog", Font.PLAIN, 15));
textField.setBounds(141, 70, 160, 22);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblPassword = new JLabel("PASSWORD");
lblPassword.setFont(new Font("Dialog", Font.BOLD, 15));
lblPassword.setBounds(12, 116, 111, 15);
frame.getContentPane().add(lblPassword);
pwdJjh = new JPasswordField();
pwdJjh.setFont(new Font("Dialog", Font.PLAIN, 15));
pwdJjh.setBounds(141, 114, 160, 22);
frame.getContentPane().add(pwdJjh);
JLabel lblUserType = new JLabel("USER TYPE");
lblUserType.setFont(new Font("Dialog", Font.BOLD, 15));
lblUserType.setBounds(12, 154, 111, 15);
frame.getContentPane().add(lblUserType);
JComboBox<String> cm = new JComboBox<String>();
cm.setBounds(141, 148, 160, 24);
cm.addItem("User");
cm.addItem("Admin");
cm.addItemListener(this);
setLayout(null);
frame.getContentPane().add(cm);
JButton btnLogin = new JButton("Login ");
btnLogin.setBounds(41, 199, 117, 25);
frame.getContentPane().add(btnLogin);
JButton btnCancle = new JButton("Cancel");
btnCancle.setBounds(209, 199, 117, 25);
frame.getContentPane().add(btnCancle);
}
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
}
//Register.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class Register {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register window = new Register();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Register() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblRegisterForm = new JLabel("Register Form");
lblRegisterForm.setForeground(Color.BLUE);
lblRegisterForm.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 25));
lblRegisterForm.setBounds(119, 12, 209, 25);
frame.getContentPane().add(lblRegisterForm);
JLabel lblSelectTheUser = new JLabel("Select the user type");
lblSelectTheUser.setFont(new Font("Dialog", Font.BOLD, 15));
lblSelectTheUser.setBounds(22, 83, 192, 25);
frame.getContentPane().add(lblSelectTheUser);
JComboBox cb1 = new JComboBox();
cb1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Lecturer", "Office staff", "HOD"}));
cb1.setBounds(213, 83, 151, 24);
frame.getContentPane().add(cb1);
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
public void setTitle(String string) {
// TODO Auto-generated method stub
}
public void setSize(int i, int j) {
// TODO Auto-generated method stub
}
public void setLocationRelativeTo(Object object) {
// TODO Auto-generated method stub
}
public void setDefaultCloseOperation(int exitOnClose) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
您可以使用
frame.setVisible(true);
使JFrame(表单)可见。