2012-08-10 70 views
5

我是一個開始的程序員,並且正在構建一個帶有按鈕和滾動條的簡單窗口。當我編譯我的代碼時,我的按鈕上的文本被elipsis截斷,圖像圖標不顯示。我試圖在eclipse和NetBeans中編譯它。爲了解決我試過的問題JButton文本和圖像圖標被省略號

.setMargin(new Insets(0, 0, 0, 0)); 

.setPreferedSize 

adding padding (I forgot the code for this) 

.setBounds 

和我在互聯網上偶然發現的其他一切。這些都沒有解決我的問題,我無法查看按鈕中的文字和圖像。

我的代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class FeedBar2 extends JFrame { 

    public FeedBar2() { 
     super("FeedBar 2"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // create icons 
     ImageIcon loadIcon = new ImageIcon("load.gif"); 
     ImageIcon saveIcon = new ImageIcon("save.gif"); 
     ImageIcon subscribeIcon = new ImageIcon("subscribe.gif"); 
     ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif"); 
     // create buttons 
     JButton load = new JButton("Load", loadIcon); 
     JButton save = new JButton("Save", saveIcon); 
     JButton subscribe = new JButton("Subscribe", subscribeIcon); 
     JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon); 
     // add buttons to toolbar 
     JToolBar bar = new JToolBar(); 
     bar.add(load); 
     bar.add(save); 
     bar.add(subscribe); 
     bar.add(unsubscribe); 
     // create menu 
     JMenuItem j1 = new JMenuItem("Load"); 
     JMenuItem j2 = new JMenuItem("Save"); 
     JMenuItem j3 = new JMenuItem("Subscribe"); 
     JMenuItem j4 = new JMenuItem("Unsubscribe"); 
     JMenuBar menubar = new JMenuBar(); 
     JMenu menu = new JMenu("Feeds"); 
     menu.add(j1); 
     menu.add(j2); 
     menu.addSeparator(); 
     menu.add(j3); 
     menu.add(j4); 
     menubar.add(menu); 
     // prepare user interface 
     JTextArea edit = new JTextArea(8, 40); 
     JScrollPane scroll = new JScrollPane(edit); 
     BorderLayout bord = new BorderLayout(); 
     setLayout(bord); 
     add("North", bar); 
     add("Center", scroll); 
     setJMenuBar(menubar); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] arguments) { 
     FeedBar2 frame = new FeedBar2(); 
    } 
} 
+0

+1個不錯的問題,歡迎來到SO論壇 – mKorbel 2012-08-10 16:25:54

+0

希望我的這個[答](http://stackoverflow.com/a/9866659/1057230)能夠爲你解決圖片問題。這裏有一個[示例](http://stackoverflow.com/a/11428289/1057230)供您快速參考:-)如果您不使用IDE的話,那麼這個[回答](http:// stackoverflow。 com/a/11372350/1057230)可能會告訴您,您的圖像相對於您的類文件的放置位置。 – 2012-08-10 16:28:08

回答

4

必須有你的圖像的位置的問題,因爲這工作得很好(用URL圖像):

import java.awt.BorderLayout; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JToolBar; 

public class FeedBar2 extends JFrame { 

    public FeedBar2() { 
     super("FeedBar 2"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // create icons 
     ImageIcon loadIcon = null; 
     try { 
      loadIcon = new ImageIcon(new URL("http://t0.gstatic.com/images?q=tbn:ANd9GcRQgmCgdCMtXO6db7pX4UwzdvJY9-r8kI2zwE5A6c3VqB9eOR2Pe8gpqQBdeg")); 
     } catch (MalformedURLException ex) { 
      Logger.getLogger(FeedBar.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     // create buttons 
     JButton load = new JButton("load", loadIcon); 
     // add buttons to toolbar 
     JToolBar bar = new JToolBar(); 
     bar.add(load); 
     BorderLayout bord = new BorderLayout(); 
     setLayout(bord); 
     add("North", bar); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] arguments) { 
     FeedBar2 frame = new FeedBar2(); 
    } 
} 

所以是位於圖片與jar相同的目錄(在NetBeans中,它們可以位於主項目目錄中)? 如果它們位於罐子中,則需要使用以下方法對它們進行分割:getResourceAsStream,其中包含相對於當前軟件包的圖像的正確路徑。

我會建議你總是用你的罐子打包你的圖片,少些問題(在NetBeans上只需將圖像拖放到當前的類包中,然後訪問它們,然後簡單地使用確切的名稱(區分大小寫)通過檢索它的getResourceAsStream

+1

JAR部分的+1 :-),從來沒有使用過'getResourceAsStream()',但今天有一些關於你的提示:-) – 2012-08-10 17:02:59

4
  • 你的意思是

enter image description here

從代碼

import java.awt.*; 
import javax.swing.*; 

public class FeedBar2 extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon"); 
    private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon"); 
    private Icon subscribeIcon = UIManager.getIcon("OptionPane.warningIcon"); 
    private Icon unsubscribeIcon = UIManager.getIcon("OptionPane.questionIcon"); 

    public FeedBar2() { 
     super("FeedBar 2"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // create icons 
     /*ImageIcon loadIcon = new ImageIcon("load.gif"); 
     ImageIcon saveIcon = new ImageIcon("save.gif"); 
     ImageIcon subscribeIcon = new ImageIcon("subscribe.gif"); 
     ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");*/ 
     // create buttons 
     JButton load = new JButton("Load", loadIcon); 
     JButton save = new JButton("Save", saveIcon); 
     JButton subscribe = new JButton("Subscribe", subscribeIcon); 
     JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon); 
     // add buttons to toolbar 
     JToolBar bar = new JToolBar(); 
     bar.add(load); 
     bar.add(save); 
     bar.add(subscribe); 
     bar.add(unsubscribe); 
     // create menu 
     JMenuItem j1 = new JMenuItem("Load"); 
     JMenuItem j2 = new JMenuItem("Save"); 
     JMenuItem j3 = new JMenuItem("Subscribe"); 
     JMenuItem j4 = new JMenuItem("Unsubscribe"); 
     JMenuBar menubar = new JMenuBar(); 
     JMenu menu = new JMenu("Feeds"); 
     menu.add(j1); 
     menu.add(j2); 
     menu.addSeparator(); 
     menu.add(j3); 
     menu.add(j4); 
     menubar.add(menu); 
     // prepare user interface 
     JTextArea edit = new JTextArea(8, 40); 
     JScrollPane scroll = new JScrollPane(edit); 
     BorderLayout bord = new BorderLayout(); 
     setLayout(bord); 
     add("North", bar); 
     add("Center", scroll); 
     setJMenuBar(menubar); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] arguments) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       FeedBar2 frame = new FeedBar2(); 
      } 
     }); 
    } 
} 
+2

+1對於你很久以前教給我的東西,***使用INBUILD圖標* ** :-) – 2012-08-10 17:02:06

+2

高興地看到你在這裏,你失去了你的動物戰爭名稱:-) ??? .... – mKorbel 2012-08-10 17:08:40

+0

+1是的,我試圖記住內置的圖標,所以我只是用一個網址:) – 2012-08-10 17:09:51