有两个Jframe,第一个Jframe上点击进入第二个可以,第二个Jframe上的返回第一个不行,谢谢大神帮我看看!
还有一个问题就是我在frame上面设置背景图片用的是Jlable的方式,但这样,我用其他的布局方式的时候,上面加的所有组件都不会显示除了背景,但是用这种直接定死位置的方法却可以显示,是为什么呢?对于用Jlable的方式显示背景图片,怎么修改才可以用其他布局显示加的其他组件呢?
//第一个界面
public class Start {
JLabel jlpic = new JLabel();
JFrame startJf = new JFrame("my Diary");
public Start() {
initStart();
}
public void show(){
startJf.setSize(800, 600);
startJf.setResizable(false);
startJf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
startJf.setVisible(true);
}
public void initStart() {
ImageIcon icon = new ImageIcon("ico\\delu.jpg");
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(), Image.SCALE_DEFAULT));
// System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setBounds(0, 0, 800, 600);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
Container c = new Container();
JButton bt = new JButton("进入");
bt.setBounds(130,450,500,50); //如果不用这种方式,按钮显示不出来
bt.setFocusable(false);
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Delu().show();
startJf.setVisible(false);
}
});
c.add(bt);
c.add(jlpic);
startJf.add(c);
}
public static void main(String args[]) {
new Start().show();
}
}
//第二个界面
public class Delu implements ActionListener {
private JLabel jlpic = new JLabel();
private JFrame Delujf = new JFrame("my Diary");
private JButton btPw;
private JButton btRe;
private JButton btDelu;
private JButton back;
private JLabel user;
private JLabel password;
private JTextField txf_userName;
private JPasswordField pwd_password;
public Delu() {
initDelu();
}
public void show() {
Delujf.setSize(800, 600);
Delujf.setResizable(false);
Delujf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Delujf.setVisible(true);
}
public void initDelu() {
ImageIcon icon = new ImageIcon("ico\\delu.jpg");
icon.setImage(
icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT));
// System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setBounds(0, 0, 800, 600);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
Container c = new Container();
back = new JButton("返回");
btPw = new JButton("找回密码");
btRe = new JButton("注册");
btDelu = new JButton("登录");
user = new JLabel("用户:");
password = new JLabel("密码:");
txf_userName = new JTextField();
pwd_password = new JPasswordField() {
public void paste() {
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
};
user.setBounds(230, 125, 50, 25);
password.setBounds(230, 160, 50, 25);
txf_userName.setBounds(330, 125, 200, 25);
pwd_password.setBounds(330, 160, 200, 25);
back.setBounds(650,50,100,35);
back.setFocusable(false);
btDelu.setBounds(370, 300, 120, 50);
btDelu.setIcon(new ImageIcon("ico\\李白.gif"));
btPw.setFocusable(false);
btPw.setBounds(300, 400, 120, 25);
btPw.setFocusable(false);
btRe.setBounds(440, 400, 120, 25);
btRe.setFocusable(false);
btPw.addActionListener(this);
btRe.addActionListener(this);
c.add(back);
c.add(user);
c.add(txf_userName);
c.add(password);
c.add(pwd_password);
c.add(btDelu);
c.add(btPw);
c.add(btRe);
c.add(jlpic);
Delujf.add(c);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//返回--------------------------->回不去
if(e.getSource() == back){
new Start().show();
Delujf.setVisible(false);
}
//登录
if(e.getSource() == btDelu){
}
//找回密码
if(e.getSource() == btPw){
}
//注册
if(e.getSource() == btRe){
}
}
}