为什么Windows不像Ubuntu那样在JButton上显示与ImageIcon相同的输出?

时间:2014-09-09 09:48:51

标签: java swing

我正在尝试创建一个图像库,就像在下图中一样,但Windows显示图像的不必要的背景,而在Ubuntu中它显示正确。

enter image description here

我使用以下代码......

public class CommonGalleryPanel extends JPanel implements ActionListener {

    public static boolean isGallaryAllowed = false;
    private int counter = 0;
    private InputStream binaryStream;
    private BufferedImage img;
    private ImageIcon iconImg;
    private JButton[] btns;
    private JCheckBox[] checkBox;
    private JButton nextButton;
    private JButton prevButton;
    private ResultSet rs;
    private String imageID;
    private long lastRecordWas;
    private long selectedOrnamentType;
    private JPanel[] panels;
    private JButton okButton;
    private JPanel centerPanel;
    private JPanel eastPanel;
    private JPanel northPanel;
    private JPanel southPanel;
    private JComboBox<String> itemsCombo;
    private String[] itemsList;
    private JButton load;
    private JPanel westPanel;
    private int rsLen = 0;
    private int noOfRows;
    private BufferedImage nextButtonImage;
    private BufferedImage prevButtonImage;
    private static List<Long> listOfSelectedOrnaments;
    private Administrator admin;

    public static List<Long> getListOfSelectedOrnaments() {
        return listOfSelectedOrnaments;
    }

    public static void setListOfSelectedOrnaments(
            List<Long> listOfSelectedOrnaments) {
        CommonGalleryPanel.listOfSelectedOrnaments = listOfSelectedOrnaments;
    }

    public CommonGalleryPanel(Administrator admin) {
        this.admin = admin;
        try {
            setLayout(new BorderLayout());
            centerPanel = new JPanel(new FlowLayout());
            eastPanel = new JPanel(new GridLayout());
            westPanel = new JPanel(new GridLayout());
            northPanel = new JPanel(new GridBagLayout());
            southPanel = new JPanel(new GridBagLayout());

            listOfSelectedOrnaments = new ArrayList<Long>();
            itemsList = DatabaseHandler.getOrnamentTypesInString();
            itemsCombo = new JComboBox<String>(itemsList);
            load = new JButton("Load");
            load.addActionListener(this);
            load.setActionCommand("loadButton");

            northPanel.setBackground(Color.LIGHT_GRAY);
            eastPanel.setBackground(Color.LIGHT_GRAY);
            westPanel.setBackground(Color.LIGHT_GRAY);
            southPanel.setBackground(Color.LIGHT_GRAY);

            centerPanel.setBackground(Color.GRAY);

            northPanel.add(itemsCombo);
            northPanel.add(new JLabel("   "));
            northPanel.add(load);
            northPanel.add(Box.createRigidArea(new Dimension(0, 50)));
            add(northPanel, BorderLayout.NORTH);

            try {
                nextButtonImage = Utility
                        .getMyResource("/buttons/next-btn.png");
                prevButtonImage = Utility.getMyResource("/buttons/pre-btn.png");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            prevButton = new JButton(new ImageIcon(
                    prevButtonImage.getScaledInstance(40, 100,
                            BufferedImage.SCALE_AREA_AVERAGING)));
            prevButton.setSize(getMaximumSize());
            westPanel.add(prevButton);

            add(eastPanel, BorderLayout.EAST);

            nextButton = new JButton(new ImageIcon(
                    nextButtonImage.getScaledInstance(40, 100,
                            BufferedImage.SCALE_AREA_AVERAGING)));
            eastPanel.add(nextButton);

            add(westPanel, BorderLayout.WEST);

            okButton = new JButton("Demonstrate");
            southPanel.add(Box.createRigidArea(new Dimension(0, 50)));
            southPanel.add(okButton);

            add(southPanel, BorderLayout.SOUTH);

            add(centerPanel, BorderLayout.CENTER);

            repaint();
            revalidate();
            setVisible(true);

            nextButton.addActionListener(this);
            okButton.addActionListener(this);
            prevButton.addActionListener(this);
            nextButton.setActionCommand("nextButton");
            okButton.setActionCommand("okButton");
            prevButton.setActionCommand("prevButton");

            revalidate();
            repaint();
            setVisible(true);
        } catch (Exception backException) {
            backException.printStackTrace();
        }
    }

    public void getImages(ResultSet rs) {
        try {
            rsLen = 0;
            while (rs.next()) {
                counter++;
                rsLen++;
            }
            if (itemsCombo.getSelectedItem().toString().equals("EARING")) {
                noOfRows = DatabaseHandler
                        .getRowCountOfGivenSubOrnamentType(selectedOrnamentType);
            } else {
                noOfRows = DatabaseHandler
                        .getRowCountOfGivenOrnamentType(selectedOrnamentType);
            }
            checkBox = new JCheckBox[noOfRows + 1];
            List<String> listOfImageIds = new ArrayList<String>(counter);
            rs.beforeFirst();
            while (rs.next()) {
                listOfImageIds.add(rs.getString(1));
            }

            rs.beforeFirst();
            int i = 0;
            centerPanel.removeAll();
            btns = new JButton[10];

            panels = new JPanel[10];
            while (rs.next()) {
                if (itemsCombo.getSelectedItem().toString().equals("EARING")) {
                    binaryStream = rs.getBinaryStream(2);
                } else {
                    binaryStream = rs.getBinaryStream(2);
                }

                img = ImageIO.read(binaryStream);
                iconImg = new ImageIcon(img.getScaledInstance(290, 180,
                        BufferedImage.TYPE_INT_ARGB));
                btns[i] = new JButton(iconImg);
                checkBox[i] = new JCheckBox();
                checkBox[i].setActionCommand(String.valueOf(rs.getLong(1)));
                panels[i] = new JPanel();
                btns[i].setBorderPainted(false);
                btns[i].setActionCommand(String.valueOf(rs.getLong(1)));
                btns[i].setBackground(new Color(0, 0, 0, 0));
                btns[i].addActionListener(this);
                //btns[i].setOpaque(true);
                panels[i].add(btns[i]);
                panels[i].add(checkBox[i]);
                panels[i].setBackground(Color.LIGHT_GRAY);
                centerPanel.add(panels[i]);
                lastRecordWas = rs.getLong(1);
                System.out.println("lastRecordWas = " + lastRecordWas);
                i++;
            }

            for (int j = 0; j < i; j++) {
                final int k = j;
                checkBox[j].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (checkBox[k].isSelected()) {
                            System.out.println("Selected image..."
                                    + checkBox[k].getActionCommand());
                            listOfSelectedOrnaments.add(Long
                                    .parseLong(checkBox[k].getActionCommand()));
                        } else {
                            System.out.println("Deselected image..."
                                    + checkBox[k].getActionCommand());
                            listOfSelectedOrnaments.remove(Long
                                    .parseLong(checkBox[k].getActionCommand()));
                        }
                        System.out.println("Selected items list = "
                                + listOfSelectedOrnaments);
                    }
                });
            }

            System.out.println("counter = " + counter);
            if (rsLen == 0) {
                centerPanel.add(new JLabel("No more images available...."),
                        BorderLayout.CENTER);
                repaint();
                revalidate();
            }
        } catch (SQLException | IOException ex) {
            ex.printStackTrace();
        }

        add(centerPanel, BorderLayout.CENTER);
        repaint();
        revalidate();
        setVisible(true);
    }

    public String getImageID() {
        return imageID;
    }

    public void setImageID(String imageID) {
        this.imageID = imageID;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("nextButton")) {
            System.out.println("next button clicked...");

            rs = DatabaseHandler.getNext9ItemsByType(lastRecordWas,
                    selectedOrnamentType);
            getImages(rs);
            revalidate();
            repaint();

        } else if (e.getActionCommand().equals("prevButton")) {
            System.out.println("prev button clicked...");

            rs = DatabaseHandler.getPrev9ItemsByType(lastRecordWas,
                    selectedOrnamentType);
            getImages(rs);
            revalidate();
            repaint();
        } else if (e.getActionCommand().equals("okButton")) {
            try {
                if (listOfSelectedOrnaments.isEmpty()) {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Please Select at least any one ornament to demonstrate...");
                } else {
                    this.admin.setListOfSelectedOrnaments(
                            listOfSelectedOrnaments, selectedOrnamentType);
                    System.out.println("list is setteled with Selected images "
                            + listOfSelectedOrnaments);
                    this.setVisible(false);
                }
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        } else if (e.getActionCommand().equals("loadButton")) {
            try {
                this.selectedOrnamentType = DatabaseHandler
                        .getOrnamentIdFromOrnamentName(itemsCombo
                                .getSelectedItem().toString());
                if (itemsCombo.getSelectedItem().toString().equals("EARING")) {
                    rs = DatabaseHandler.getNext9EaringsByType(0l,
                            this.selectedOrnamentType);
                } else {
                    rs = DatabaseHandler.getNext9ItemsByType(0l,
                            this.selectedOrnamentType);
                }
                getImages(rs);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            try {
                System.out.println("Selected image = " + e.getActionCommand());

                ImageViewer.showImage(Long.parseLong(e.getActionCommand()),itemsCombo.getSelectedItem().toString());

                setImageID(e.getActionCommand());
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
    }
}

基本上它适用于Ubuntu然后为什么不在Windows上?

1 个答案:

答案 0 :(得分:7)

只是建议

在主要方法

中加上第一个
public static void main(String[] args)
{
   String propertyName = "sun.java2d.noddraw";
   System.setProperty(propertyName, "true");

   propertyName = "sun.java2d.d3d";
   System.setProperty(propertyName, "false");

   //YOUR CODE HERE...
}

通过这样做,您将关闭direct3D和directDraw的使用。

您也可以尝试使用不同的LookAndFeel :(此处为示例MetalLookAndFeel)

public static void main(String[] args)
{
   String propertyName = "sun.java2d.noddraw";
   System.setProperty(propertyName, "true");

   propertyName = "sun.java2d.d3d";
   System.setProperty(propertyName, "false");

   try 
   {
       javax.swing.UIManager.setLookAndFeel(MetalLookAndFeel.class.getName());
   }
   catch (Exception ex) 
   {
       ex.printStackTrace();
   }

   //YOUR CODE HERE...

}

默认情况下,SystemLookAndFeel将返回“WindowsLookAndFeel”(使用Windows时) 但在Linux上,它将返回MetalLookAndFeel(或GTKLookAndFeel)。

强制 Windows使用MetalLookAndFeel(而不是SystemLookAndFeel(WindowsLookAndFeel)),您必须使用上面的代码!