【发布时间】:2015-02-24 03:07:48
【问题描述】:
我知道有人问过这个问题,但我没有找到解决办法。
我为登录创建了一个JFrame,我想在按下“Cont Nou”按钮后用新帐户的jpanel打开一个新窗口,但不知道如何使初始框架消失并出现jpanel 的框架。你有什么想法吗?谢谢!
这是我到目前为止所做的:
这是JFrame的登录名:
public class LogIn extends JFrame implements ActionListener{
private JLabel labelEmail;
private JLabel labelParola;
private JTextField textFieldEmail;
private JPasswordField textFieldParola;
private JButton buttonLogin;
private JButton buttonContNou;
public LogIn (){
super();
this.setSize(400,200);
this.setTitle("Login");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setResizable(false);
this.setupComponents();
}
private void setupComponents(){
labelEmail = new JLabel("Email: ");
labelParola = new JLabel("Parola: ");
textFieldEmail = new JTextField();
textFieldParola = new JPasswordField();
buttonContNou = new JButton("Cont Nou");
buttonLogin = new JButton("Login");
labelEmail.setBounds(30,30,50,20);
labelParola.setBounds(30,70,50,20);
textFieldEmail.setBounds(100,30,185,20);
textFieldParola.setBounds(100,70,185,20);
buttonContNou.setBounds(185,110,100,20);
buttonLogin.setBounds(100,110,75,20);
buttonLogin.addActionListener(this);
buttonContNou.addActionListener(this);
this.add(labelEmail);
this.add(labelParola);
this.add(textFieldEmail);
this.add(textFieldParola);
this.add(buttonLogin);
this.add(buttonContNou);
}
public static void main(String[] args){
LogIn login= new LogIn();
login.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(buttonLogin)){
boolean toateDateleOk =true;
textFieldEmail.setBackground(Color.WHITE);
textFieldParola.setBackground(Color.WHITE);
if(textFieldEmail.getText().length()==0){
textFieldEmail.setBackground(Color.RED);
toateDateleOk =false;
}
if(textFieldParola.getPassword().length==0){
textFieldParola.setBackground(Color.RED);
toateDateleOk =false;
}
if(!toateDateleOk)
return ;
else
System.out.println("Incepe Procesul de logare");
if(e.getSource().equals(buttonContNou)){
//this.dispose();
//dispose();
//new NewAccountPanel().setVisible(true);
//new secondTab().show();
}
}
}
}
【问题讨论】:
-
我忘记输入 Jpanel 的代码
-
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
谢谢,我不知道该选择什么布局,所以我使用了null。我现在试着改一下:)