JButton文本和图像图标用省略号切断

时间:2012-08-10 16:22:31

标签: java swing jscrollpane jbutton layout-manager

我是一名初学程序员,正在构建一个带按钮和滚动条的简单窗口。当我编译我的代码时,我的按钮上的文字被删除了一个省略号,图像图标没有显示。我试过在eclipse和NetBeans中编译它。为了解决我试过的问题

.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();
    }
}

2 个答案:

答案 0 :(得分: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中它们可以位于主项目目录中)吗? 如果它们位于jar中,则需要使用:getResourceAsStream使用正确的包路径来解决它们,其中包含相对于当前包的图像。

我建议总是用你的罐子包装你的图像,减少问题(在netbeans上这样做只需将图像拖放到类的当前包中即可访问它们然后简单地使用确切的名称(区分大小写)来使用getResourceAsStream

检索它

答案 1 :(得分: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();
            }
        });
    }
}