qq_40230610 2018-05-07 07:46 采纳率: 40%
浏览 1001
已采纳

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条回答 默认 最新

  • 笨笨的程序员啊 2018-05-07 08:07
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题