2014-02-09 80 views
-1

目前我的JFrame沒有顯示任何按鈕,文本等。我​​查看了其他主題,導致錯誤的原因之一是因爲setVisable方法在所有內容之前被調用被創建。正如你所看到的,這不是問題,因爲它是在創建完成之後調用的。我的目標是底部面板有一個啓動按鈕,點擊時JLabel取代它。 (這是我的第一個swing應用程序..)JFrame沒有顯示按鈕或文本

我的問題是,啓動JButton和bottomPanel沒有顯示。

這是發生問題,客戶端類

private void createPanel() { 
    bottomPanel = new JPanel(new BorderLayout()); 
    Launch = new JButton(Settings.launch); 
    Loading = new JLabel(Settings.loaderIcon); 
    Launch.addActionListener(this); 
} 

private void addPanel() { 
    setTitle("RuneRebellionV2 Launcher"); 
    setContentPane(new JLabel(Settings.background)); 
    getContentPane().add(bottomPanel, BorderLayout.SOUTH); 
    bottomPanel.add(new JLabel("Launcher, release " + Settings.version), BorderLayout.WEST); 
    bottomPanel.setBackground(Color.black); 
    bottomPanel.add(Launch, BorderLayout.EAST); 
    Launch.setPreferredSize(new Dimension(120, 40)); 
    setSize(Settings.width, Settings.height); 
    add(bottomPanel); 
} 

這是系統的其餘部分它的工作原理是這樣..你啓動開始。

package com.RBV2; 

import java.io.IOException; 

import com.RBV2.engine.Client; 
/* 
* Author David 
* 
*/ 
public class Initiate { 
    public static void main(String[] args) { 
     try { 
      Settings.init(); 
      Client.main(args); 
      System.out.println("Client started. -Alpha"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

運行Settings.java

package com.RBV2; 

import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 

/* 
* Author David 
* 
*/ 

public class Settings { 
    public static String version = "1.0.0"; 
    public static String saveDirectory = System.getProperty("user.home")+"/"; 

    public static ImageIcon launch; 
    public static ImageIcon icon1; 
    public static ImageIcon icon2; 
    public static Icon loaderIcon; 
    public static ImageIcon slideshow; 
    public static ImageIcon background; 

    public static int width = 800, height = 480; 

    public static String downloadUrl = "http://www.runerebellion.com/RuneRebellion.jar"; 
    public static String fileName = "RuneRebellion.jar"; 
    public static String serverName = "RuneRebellion"; 

    public static void init() {//Have to host files online 
     try { 
      URL background = new URL("http://www.runerebellion.com/clientImages/background.png"); 
      Settings.background = new ImageIcon(background); 
      URL slideshow = new URL("http://www.runerebellion.com/clientImages/launch.png");//temp 
      Settings.slideshow = new ImageIcon(slideshow); 
      URL loaderIcon = new URL("http://www.runerebellion.com/clientImages/loader.gif"); 
      Settings.loaderIcon = new ImageIcon(loaderIcon); 
      URL icon2 = new URL("http://www.runerebellion.com/clientImages/testcommunity.png"); 
      Settings.icon2 = new ImageIcon(icon2); 
      URL icon1 = new URL("http://www.runerebellion.com/clientImages/community.png"); 
      Settings.icon1 = new ImageIcon(icon1); 
      URL launch = new URL("http://www.runerebellion.com/clientImages/launch.png"); 
      Settings.launch = new ImageIcon(launch); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

然後這個類使用設置,使JFrame中。

package com.RBV2.engine; 

import javax.swing.*; 
import javax.swing.text.html.HTMLEditorKit; 

import com.RBV2.Settings; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 
import java.net.URL; 
import java.net.URLConnection; 

import java.io.*; 
/* 
* Author Jon & David 
* 
*/ 
@SuppressWarnings("serial") 
public class Client extends JFrame implements ActionListener { 
    private JPanel bottomPanel; 
    private JButton Launch; 
    private JLabel Loading; 
    //private JLabel Loading = new JLabel(Settings.loaderIcon); 

    protected boolean updated = false; 

    public void refresh() { 
     setSize(Settings.width - 1, Settings.height - 1); 
     setSize(Settings.width, Settings.height); 
    } 

    private void createLayout() { 
     createPanel(); 
     addPanel(); 
     setVisible(true); 
    } 

    private void createPanel() { 
     bottomPanel = new JPanel(new BorderLayout()); 
     Launch = new JButton(Settings.launch); 
     Loading = new JLabel(Settings.loaderIcon); 
     Launch.addActionListener(this); 
    } 

    private void addPanel() { 
     setTitle("RuneRebellionV2 Launcher"); 
     setContentPane(new JLabel(Settings.background)); 
     getContentPane().add(bottomPanel, BorderLayout.SOUTH); 
     bottomPanel.add(new JLabel("Launcher, release " + Settings.version), BorderLayout.WEST); 
     bottomPanel.setBackground(Color.black); 
     bottomPanel.add(Launch, BorderLayout.EAST); 
     Launch.setPreferredSize(new Dimension(120, 40)); 
     setSize(Settings.width, Settings.height); 
     add(bottomPanel); 
    } 

    private void checkUpdates() { 
     try { 
     final URL url = new URL(Settings.downloadUrl); 
     URLConnection connection = url.openConnection(); 
     connection.connect(); 
     downloadUpdates(url); 
     } catch(IOException e) {//failed to connect therefore lets just run the client if it exists all will run smooth 
      try { 
       startApplication(); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 

    private void downloadUpdates(final URL url) {//Downloads the client off the website & removes older client 
     try { 
      final File file = new File(Settings.saveDirectory + url.getFile()); 
      if (file.exists()) 
       file.delete(); 

      Thread t = new Thread() { 
       public void run() { 
        try { 
         OutputStream dest = new BufferedOutputStream(
           new FileOutputStream(file)); 
         URLConnection download = url.openConnection(); 
         InputStream readFileToDownload = download 
           .getInputStream(); 
         byte[] data = new byte[1024]; 
         int numRead; 
         download.getContentLength(); 
         while ((numRead = readFileToDownload.read(data)) != -1) { 
          dest.write(data, 0, numRead); 
         } 
         if (readFileToDownload != null) 
          readFileToDownload.close(); 
         if (dest != null) 
          dest.close(); 
         Thread.sleep(1000L); 
         startApplication(); 
        } catch (IOException | InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 
      t.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public Client() throws IOException { 
     createLayout(); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == Launch) { 
      checkUpdates(); 
     } 
    } 

    public static void startApplication() throws InterruptedException { 
     try { 
      Runtime.getRuntime().exec(
        "java -jar " + (Settings.saveDirectory + Settings.fileName) 
          + ""); 
      Thread.sleep(1000L); 
      System.exit(0); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String args[]) throws IOException { 
     final Client l = new Client(); 
     l.setVisible(true); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       l.setVisible(true); 
      } 
     }); 

    } 

} 

編輯:

setContentPane(new JLabel(Settings.background)); 

替換

JLabel label = new JLabel(Settings.background); 
setContentPane(label); 
label.setLayout(new BorderLayout()); 

修正了問題,由於被放置在一切的JLabel。

+0

你的代碼有點複雜,或者我認爲編程術語是它有很多「[複雜性](http://en.wikipedia.org/wiki/Cyclomatic_complexity)」。這會讓你和其他人難以調試它。考慮重構,簡化和避免許多副作用的方法。 –

+0

@HovercraftFullOfEels真的只有一點需要看,我把這段代碼分開了..我真的不明白你的意思,但我可以完美地閱讀代碼。 – RuneRebellion

+0

你可以閱讀代碼,但你找不到錯誤,對不對?否則你不會在這裏。但我的意思是說,你的許多方法改變了類字段的狀態並且什麼都不返回,這會增加複雜性,因爲它不太明顯,它們如何改變狀態,從而可能導致問題。 –

回答

4

這裏的問題

setContentPane(new JLabel(Settings.background)); 

你設置與標籤內容窗格中。如果你擺脫這一點,它的作品。

一個選項,如果你想有一個背景圖片,是把它漆成上JPanel像這樣

public class ButtonPanel extends JPanel { 

    Image img; 

    public ButtonPanel() { 
     img = (Settings.background).getImage(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(Settings.width, Settings.height); 

    } 
} 

ButtonPanel將是buttonPanel您當前使用

另一種解決方案,爲@MadProgrammer建議只是將背景標籤的佈局管理器設置爲BorderLayout,像這樣

private void addPanel() { 
    setTitle("RuneRebellionV2 Launcher"); 
    JLabel label = new JLabel(Settings.background); 
    setContentPane(label); 
    label.setLayout(new BorderLayout()); 
+0

或設置標籤的佈局管理器.... – MadProgrammer

+3

對不起,意思是給你+1,但我2歲的女兒決定她需要做一些「computr」工作...:P – MadProgrammer

+0

@MadPogrammer aww有多可愛;)..我會添加你的建議,雖然+1回atcha! –