2013-06-12 109 views
0

好吧,這可能是一個愚蠢的問題,但我是一般的GUI和Java的新手。在我的GUI中,我創建了我的框架,因爲我沒有在哪裏看到JFrame。或者我必須創建一個JFrame,並將所有的東西放在這裏。我需要一個JFrame來做一些事情,比如最小化屏幕,改變圖標等。感謝您的幫助!添加一個框架到GUI Java

private ImageIcon bgi; 
private JLabel bgl; 


private JButton startButton; 
private JButton helpButton; 
private JButton backButton; 
private final Action action = new SwingAction(); 


public static void main(String[] args) throws MalformedURLException, IOException { 
    TwitterUnfollower gui = new TwitterUnfollower(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program 
    gui.setSize(902, 305); 

    gui.setVisible(true); 
    gui.setTitle("Solid Cloud Inc - Twitter Unfolower"); 
} 

public TwitterUnfollower() throws MalformedURLException, IOException{ 

    bgi = new ImageIcon(getClass().getResource("tu.png")); 
    getContentPane().setLayout(null); 
    BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png")); 
    //ImageIcon start = new ImageIcon(getClass().getResource("start.png")); 
    startButton = new JButton(""); 
    startButton.setIcon(new ImageIcon(img)); 
    startButton.setBounds(22, 186, 114, 50); 


    getContentPane().add(startButton); 

    BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png")); 
    helpButton = new JButton(""); 
    helpButton.setIcon(new ImageIcon(img2)); 
    helpButton.setBounds(192, 186, 114, 50); 

    getContentPane().add(helpButton); 

    BufferedImage img3 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/back_zps9d62b65b.png")); 
    backButton = new JButton(""); 
    backButton.setIcon(new ImageIcon(img3)); 
    backButton.setBounds(105, 205, 82, 44); 
    backButton.setBorder(BorderFactory.createEmptyBorder()); 
    backButton.setContentAreaFilled(false); 
    backButton.setVisible(false); 

    getContentPane().add(backButton); 

    bgl = new JLabel (bgi); 
    bgl.setBounds(0, 0, 886, 272); 
    getContentPane().add(bgl); 

    Events e = new Events(); 
    startButton.addActionListener(e); 
    helpButton.addActionListener(e); 
    backButton.addActionListener(e); 
} 

} 

我確實有一個動作偵聽器,我從代碼中刪除了它以縮短它。而且我知道我應該避免使用空佈局,但我使用的是WindowBuilder,而且這可能會改變。再次感謝!

+0

TwitterUnfollower似乎是你的框架 – nachokk

+0

'TwitterUnfollower gui = new TwitterUnfollower(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);'我的第一個猜測是'TwitterUnfollower'實際上**'擴展了JFrame' **。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

此代碼不能編譯。 TwitterUnfollower應該可能是一個擴展JFrame的類。此外,你應該閱讀LayoutManagers,並擺脫setBounds()調用(它不會飛)。 – haraldK

回答

1

你的意思是這樣的:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class TwitterUnfollower extends JFrame { 
    private ImageIcon bgi; 
    private JLabel bgl; 

    private JButton startButton; 
    private JButton helpButton; 
    private JButton backButton; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        TwitterUnfollower gui = new TwitterUnfollower(); 
        gui.setTitle("Solid Cloud Inc - Twitter Unfolower"); 
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program 
        gui.pack(); 
        gui.setVisible(true); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public TwitterUnfollower() throws MalformedURLException, IOException { 

     bgi = new ImageIcon(ImageIO.read(new URL(
       "http://content.mcfc.co.uk//~/media/Images/Home/News/Club%20news/2012/twitter%20background%20new.ashx"))); 
     bgl = new JLabel(bgi); 
     bgl.setLayout(new BorderLayout()); 
     getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 
     getContentPane().add(bgl); 

     BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png")); 
     // ImageIcon start = new ImageIcon(getClass().getResource("start.png")); 
     startButton = getButton(img); 

     BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png")); 
     helpButton = getButton(img2); 

     BufferedImage img3 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/back_zps9d62b65b.png")); 
     backButton = getButton(img3); 

     JPanel alignLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); 
     alignLeftPanel.setOpaque(false); 
     JPanel buttonPanel = new JPanel(new GridLayout(1, 0)); 
     buttonPanel.setOpaque(false); 
     buttonPanel.add(startButton); 
     buttonPanel.add(helpButton); 
     buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
     alignLeftPanel.add(buttonPanel); 
     bgl.add(alignLeftPanel, BorderLayout.SOUTH); 
    } 

    private JButton getButton(BufferedImage img) { 
     JButton button = new JButton(new ImageIcon(img)); 
     button.setContentAreaFilled(false); 
     button.setBorderPainted(false); 
     button.setFocusPainted(false); 
     return button; 
    } 

} 

重要:寧可使用一個LayoutManager比使用null佈局,只會給你帶來麻煩。

警告:photobucket返回狀態503,因此我不能再測試此代碼。