2015-10-06 63 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

*「我無法在不同的操作系統上進行測試。」*我也不能。這就是爲什麼將它放在佈局上很合理的原因之一! –