0

尝试更改 jpanel 的 BG 图像,但我无法在任何普通方法上调用 poaint,当我构建构造函数但我不想重建构造函数时它工作得很好。

……

通过在我的中心框架中放置一个标签并调用 setIcon 找到了一个解决方案,但我需要能够提取相关信息,所以我需要找到一种方法将值存储到我的 Jtoggle 按钮(Race 或类的 id所以我可以获取它的图片并更改图标)

想法?一切都在 iff 语句之外编译,这是我的症结所在

RaceButtons_lft[i] =  new JToggleButton();
RaceButtons_lft[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JToggleButton cb = (JToggleButton)ae.getSource();
for (int j=0; j<MyRaceArray.size(); j++)
{
if (MyRaceArray.get(j).getraceID() == combo_contents.getIndex()){//here is my sticking point, i need to find a way to match MyRaceArray's getRaceID to some value saved withthe Toggle button
final ImageIcon BGCSMs = ScaledImageIcon("Fantasy_Landscape_01.jpg", "Profile Pic", (468-(60*2)), 285);
picLabel.setIcon(BGCSMs);
}//if
}//for
}//action performed;
});//button add action listener
4

4 回答 4

1

打电话

super.paintComponent(..)

可能 - 取决于超类 - 用背景颜色填充组件。

public void paintComponent(Graphics g) {
  // Let UI Delegate paint first, which 
  // includes background filling since 
  // this component is opaque.

  super.paintComponent(g);       
  g.drawString("This is my custom Panel!",10,20);
  redSquare.paintSquare(g);
}

(请参阅仔细查看绘制机制)。在这种情况下,您不需要 repaint(..) 。

于 2013-06-07T08:23:37.860 回答
1

You could be suffering from a number of problems, which we can't see because we don't have enough context...

  • You could have a reference issue, instead of trying to repaint the component on the screen, you've inadvertently gotten the wrong reference...
  • You could be shadowing your variables...
  • You could be painting to a opaque component...

Assuming that the code you have posted is linear (ie, it appears in you code in this exact order or close enough to it), I can see one possible problem...

ImageIcon RCicon = createImageIcon(temp_race.getActiveHeadshot(), temp_race.getRaceNameString(race.getraceID()));
Image RCimg = RCicon.getImage();
RCimg = RCimg.getScaledInstance((468-(60*2)), 285, java.awt.Image.SCALE_SMOOTH);

portraitCenterOptions.setBackground(Color.White){
    protected void paintComponent(Graphics h)
    {
        //...//
        // There is no way that this reference can be valid...
        // The image created above will only have a local reference unto itself
        // suggestion that you're shadowing your variables...
        final ImageIcon bodypicSM = new ImageIcon(RCimg);
        //...//
    }
};

But without a working example, it's impossible to know...

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ChangeBackground {

    public static void main(String[] args) {
        new ChangeBackground();
    }

    public ChangeBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final PaintPane pane = new PaintPane();
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(pane);

                JButton change = new JButton("Change");
                change.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pane.changeBackground();
                        pane.repaint();
                    }
                });

                frame.add(change, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class PaintPane extends JPanel {

        private BufferedImage bg;
        private int changes = 0;

        public PaintPane() {
            changeBackground();
        }

        public void changeBackground() {

            bg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bg.createGraphics();
            FontMetrics fm = g.getFontMetrics();
            g.setColor(getForeground());
            String[] text = {
                "I've been changed " + changes + " times", 
                "Last changed at " + DateFormat.getDateTimeInstance().format(new Date())};
            int y = (200 - (fm.getHeight() * 2)) / 2;
            for (String value : text) {
                int x = (200 - fm.stringWidth(value)) / 2;
                g.drawString(value, x, y + fm.getAscent());
                y += fm.getHeight();
            }
            g.dispose();
            changes++;

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            int x = (getWidth() - bg.getWidth()) / 2;
            int y = (getHeight() - bg.getHeight()) / 2;
            g.drawImage(bg, x, y, this);
        }

    }

}
于 2013-06-07T10:22:24.973 回答
0

repaint()完成对后台的更改后调用组件

于 2013-06-07T08:21:28.203 回答
0

所以最终尝试了一些事情并且变得懒惰,在中心添加了一个标签并称为“SetIcon,做我需要它做的事情,不过感谢你的想法。

于 2013-06-17T01:28:29.573 回答