2015-10-06 64 views
1

我使用Eclipse在Java swing中制作了一个简单的数字名片,我对Java和编程有所了解,因此请耐心等待。基本上我想要做的是添加一个按钮,例如“我们的工作”或“投资组合”,点击后会打开一个新窗口,然后我可以添加图片或链接或虚假评论等。在Java swing中制作第二帧

我知道这可能很简单,但是当它基于其他人的代码时,我很难理解很多教程。

package dbuscard; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import java.awt.Color; 
import javax.swing.JLabel; 
import javax.swing.ImageIcon; 
import java.awt.Font; 
import javax.swing.JSeparator; 

public class card { 

    private JFrame frame; 

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

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

    /** 
    * Initialise the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.getContentPane().setBackground(new Color(153, 204, 255)); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JLabel lblLogo = new JLabel(""); 
     lblLogo.setBounds(127, 11, 219, 104); 
     frame.getContentPane().add(lblLogo); 
     lblLogo.setIcon(new ImageIcon("Images\\zlogoimg.png")); 

     JLabel lblNewLabel = new JLabel("t: 01254 777494"); 
     lblNewLabel.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11)); 
     lblNewLabel.setBounds(20, 187, 126, 14); 
     frame.getContentPane().add(lblNewLabel); 

     JLabel lblEApexuxdesigngmailcom = new JLabel("e: [email protected]"); 
     lblEApexuxdesigngmailcom.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11)); 
     lblEApexuxdesigngmailcom.setBounds(20, 204, 200, 14); 
     frame.getContentPane().add(lblEApexuxdesigngmailcom); 

     JLabel lblVisitWebsite = new JLabel("visit website"); 
     lblVisitWebsite.setFont(new Font("Source Code Pro Light", Font.BOLD, 11)); 
     lblVisitWebsite.setBounds(10, 237, 117, 14); 
     frame.getContentPane().add(lblVisitWebsite); 

     JLabel facebook = new JLabel(""); 
     facebook.setBounds(282, 204, 64, 47); 
     frame.getContentPane().add(facebook); 
     facebook.setIcon(new ImageIcon("Images\\facebook.png")); 

     JLabel twitter = new JLabel(""); 
     twitter.setBounds(320, 204, 72, 47); 
     frame.getContentPane().add(twitter); 
     twitter.setIcon(new ImageIcon("Images\\twitter.png")); 

     JLabel youtube = new JLabel(""); 
     youtube.setBounds(356, 204, 68, 47); 
     frame.getContentPane().add(youtube); 
     youtube.setIcon(new ImageIcon("Images\\youtube.png")); 

     JLabel lblSeanHutchinson = new JLabel("Sean Hutchinson"); 
     lblSeanHutchinson.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11)); 
     lblSeanHutchinson.setBounds(20, 128, 126, 14); 
     frame.getContentPane().add(lblSeanHutchinson); 

     JLabel lblUxDesigner = new JLabel("UX Designer"); 
     lblUxDesigner.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11)); 
     lblUxDesigner.setBounds(20, 145, 107, 14); 
     frame.getContentPane().add(lblUxDesigner); 

    JLabel lblNewLabel_1 = new JLabel("CEO - Apex UX Design"); 
     lblNewLabel_1.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11)); 
     lblNewLabel_1.setBounds(20, 162, 158, 14); 
     frame.getContentPane().add(lblNewLabel_1); 


    } 
} 
+0

我还没有试过这样做,但我相信它会像拥有两个JFrame一样简单。 'JFrame mainWindow,portfolioWindow'和初始化信息。 – CoderMusgrove

+2

看看[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556),这是一个坏主意,[如何制作对话框](http ://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)更合理的解决方案 – MadProgrammer

+1

实际上,这几乎是Swing 101,请参阅[使用JFC/Swing创建GUI](http: //docs.oracle.com/javase/tutorial/uiswing/)了解更多详情。避免使用'空'布局,像素完美的布局是现代UI设计中的幻想。影响组件的个体大小的因素太多,其中没有一个可以控制。 Swing旨在与布局经理一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正 – MadProgrammer

回答

1

简单。添加一个在单独的JFrame上使用方法setVisible(true);的动作侦听器。示例代码:

package com.nonsense; 

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

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

public class del { 

private JFrame frame, frame2; 


/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    del window = new del(); 
    window.frame.setVisible(true); 
} 

/** 
* Create the application. 
*/ 
public del() { 
    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); 

    frame2 = new JFrame(); 
    frame2.setBounds(100, 100, 450, 300); 
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    frame2.getContentPane().setLayout(null); 

    JButton btnOpenWindow = new JButton("Open Window"); 
    btnOpenWindow.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      frame2.setVisible(true); 
     } 
    }); 
    btnOpenWindow.setBounds(167, 118, 120, 23); 
    frame.getContentPane().add(btnOpenWindow); 

} 
} 

我使用“DISPOSE_ON_CLOSE”,使被按压的X按钮当第二窗口不终止程序。

+0

'package com.nonsense;'......嗯,具有讽刺意味的是合适的。 1)请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556)2)Java GUI必须适用于不同的操作系统,屏幕大小,屏幕分辨率等。在不同的地区使用不同的PLAF。因此,它们不利于像素的完美布局。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。 3)Swing/AWT GUI应该在EDT上启动.. –

+0

@Andrew Thompson很多好点。但是我不能在不同的操作系统上测试。对于任何想知道的人来说,这个程序都适用于Win8。 – RubyJunk

+0

*“我无法在不同的操作系统上进行测试。”*我也不能。这就是为什么将它放在布局上很合理的原因之一! –