java 用new jfram打开另一个class的窗体,窗体是空的?

打开的新窗体如图左上角图片说明
想从一个窗体打开另一个class的窗体
是我写得不对吗?
图片说明

登录窗体代码

package zjx_qizhong;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class login extends JFrame{

private JFrame frame;
private JTextField textField;
private JTextField textField_1;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                login window = new login();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public login() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {





    frame = new JFrame();
    frame.setTitle("登录");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("login");
    btnNewButton.setBounds(149, 169, 113, 27);
    frame.getContentPane().add(btnNewButton);







    textField = new JTextField();
    textField.setBounds(159, 55, 86, 24);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(159, 101, 86, 24);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

    JLabel lblUser = new JLabel("User");
    lblUser.setBounds(86, 58, 49, 18);
    frame.getContentPane().add(lblUser);

    JLabel lblPassword = new JLabel("password");
    lblPassword.setBounds(79, 104, 72, 18);
    frame.getContentPane().add(lblPassword);




    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


      qizhong newWindow=new qizhong();
      newWindow.setVisible(true);

}


        }


    });






}

}

登录后打开的窗体代码

package zjx_qizhong;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.awt.event.ActionEvent;

public class qizhong extends JFrame {

private JFrame frame1;
private JTextField txt;
private JPanel imgPanel;
JLabel lblNewLabel ;





/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                qizhong window = new qizhong();
                window.frame1.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });







}







/**
 * Create the application.
 */
public qizhong() {
    initialize();


}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {





    File file = new File("E:/360Downloads/1.wav");  
    java.net.URL url = null;  
    try {  
        URI uri = file.toURI();  
        url = uri.toURL();  
    }  
    catch (MalformedURLException ex) {  
    }  
    AudioClip audio = Applet.newAudioClip(url);  







    File file1 = new File("E:/360Downloads/2.wav");  
    java.net.URL url1 = null;  
    try {  
        URI uri = file1.toURI();  
        url1 = uri.toURL();  
    }  
    catch (MalformedURLException ex) {  
    }  
    AudioClip audio1 = Applet.newAudioClip(url1);  





    File file2 = new File("E:/360Downloads/3.wav");  
    java.net.URL url2 = null;  
    try {  
        URI uri = file2.toURI();  
        url2 = uri.toURL();  
    }  
    catch (MalformedURLException ex) {  
    }  
    AudioClip audio2 = Applet.newAudioClip(url2);






     String img1="E:/360Downloads/1.jpg";
     String img2="E:/360Downloads/2.jpg";
     String img3="E:/360Downloads/3.jpg";
     ImageIcon bg = new ImageIcon(img1);
     ImageIcon bg1 = new ImageIcon(img2);
     ImageIcon bg2 = new ImageIcon(img3);








    frame1 = new JFrame("显示图片");
    frame1.setBounds(100, 100, 1280, 720);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.getContentPane().setLayout(null);








    txt = new JTextField();
    txt.setEnabled(false);
    txt.setHorizontalAlignment(SwingConstants.CENTER);
    txt.setEditable(false);
    txt.setFont(new Font("Open Sans", Font.PLAIN, 40));
    txt.setBounds(524, 540, 226, 67);
    frame1.getContentPane().add(txt);
    txt.setColumns(10);
    txt.setOpaque(false);

    JButton btnNewButton = new JButton("BMW");
    btnNewButton.setBounds(356, 634, 113, 27);
    frame1.getContentPane().add(btnNewButton);
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            txt.setText("BMW");
            audio1.stop();
            audio2.stop();
                audio.play();



            if(lblNewLabel==null){
                lblNewLabel = new JLabel(bg); 
                frame1.getContentPane().add(lblNewLabel);
                lblNewLabel.setBounds(0, 0, frame1.getWidth(), frame1.getHeight());
            }
            else {
            lblNewLabel.setIcon(bg);

            }

        }
    });


    JButton btnNewButton_1 = new JButton("Tesla");
    btnNewButton_1.setBounds(583, 634, 113, 27);
    frame1.getContentPane().add(btnNewButton_1);
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            txt.setText("Tesla");
            audio.stop();
            audio2.stop();
            audio1.play();
            if(lblNewLabel==null){
                lblNewLabel = new JLabel(bg1); 
                frame1.getContentPane().add(lblNewLabel);
                lblNewLabel.setBounds(0, 0, frame1.getWidth(), frame1.getHeight());
            }
            else {
            lblNewLabel.setIcon(bg1);

            }
        }
    });

    JButton btnNewButton_2 = new JButton("Mustang 5.0");
    btnNewButton_2.setBounds(805, 634, 113, 27);
    frame1.getContentPane().add(btnNewButton_2);
    btnNewButton_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            txt.setText("Mustang 5.0");
            audio1.stop();
            audio.stop();
            audio2.play();
            if(lblNewLabel==null){
                lblNewLabel = new JLabel(bg2); 
                frame1.getContentPane().add(lblNewLabel);
                lblNewLabel.setBounds(0, 0, frame1.getWidth(), frame1.getHeight());
            }
            else {
            lblNewLabel.setIcon(bg2);


            }   


        }
    });














}

}

1个回答

用setSize和setLocation设置一下新窗体大小和位置

qq_40527059
qq_40527059 实例化,没具体内容。
一年多之前 回复
congyeyan9940
笨笨的程序员啊 回复qq_40230610:加油
一年多之前 回复
congyeyan9940
笨笨的程序员啊 回复qq_40230610:额 不行你就在外面实例化一个对象呗,,,,
一年多之前 回复
qq_40230610
qq_40230610 回复congyeyan9940: 已经解决 感谢
一年多之前 回复
qq_40230610
qq_40230610 回复congyeyan9940: 监听里面的改成this之后出现了The method getContentPane() is undefined for the type new ActionListener(){},请问这怎么改?
一年多之前 回复
congyeyan9940
笨笨的程序员啊 回复qq_40230610: 把frame1属性删掉 都用this表示 例如 this.setBounds(100, 100, 1280, 720); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setLayout(null);
一年多之前 回复
qq_40230610
qq_40230610 回复congyeyan9940: 请问具体怎么写?不太会
一年多之前 回复
congyeyan9940
笨笨的程序员啊 回复qq_40230610: 看了一下 你把加入的元素都放到frame1里面了 你的qizhong类(类名最好用大写)已经继承了JFrame 直接操作这个类本身就行了
一年多之前 回复
weixin_42156099
weixin_42156099 路径的问题吧
一年多之前 回复
qq_40230610
qq_40230610 回复congyeyan9940: 不行呢,大小是有了但是还是空白
一年多之前 回复
qq_36579777
就是喜欢看不惯你又干不掉你的样子 这个不是吧
一年多之前 回复
congyeyan9940
笨笨的程序员啊 newWindow.setSize(width, height); newWindow.setLocation(x, y); 试试
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!