【问题标题】:Component won't display unless window is resized - Java除非调整窗口大小,否则组件不会显示 - Java
【发布时间】:2014-12-03 13:31:15
【问题描述】:

单击按钮后,我遇到了图像可见性问题。我有带框架的主类:

package superworld;

import java.awt.*;
import javax.swing.*;
public class SuperWorld {


    public static void main(String[] args) {

        JFrame frame= new JFrame();
       frame.setSize(1050,650);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
       frame.add(new SuperPanel());
       frame.setVisible(true);
    //   frame.setResizable(false);
    }

}

然后我有所有组件的面板类:

package superworld;

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

public class SuperPanel extends JPanel implements ActionListener{

        Timer mainTimer;
    public static final int HEIGHT = 550;
    public static final int WIDTH = 1050;
        int i;
        int w=-100;
        int h=-50;
        ArrayList<SuperMiasto> miasta = new ArrayList<SuperMiasto>();

   private JButton heroButton;
   private JButton cywilButton;


    public SuperPanel() {
        mainTimer = new Timer(10,this);
               heroButton = new HeroButton(this);
               cywilButton = new CywilButton(this);
        setLayout(null);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.GREEN);                           
                for(i=0;i<10;i++)
                {
                    miasta.add( new SuperMiasto() );
                    miasta.get(i).x=w;
                    miasta.get(i).y=h;
                    miasta.get(i).imagelabel = new JLabel(miasta.get(i).image);
                    miasta.get(i).imagelabel.setBounds(miasta.get(i).x,miasta.get(i).y,miasta.get(i).image.getIconWidth(),miasta.get(i).image.getIconHeight());
                    add(miasta.get(i).imagelabel);
                    w=w+200;
                    if (w > WIDTH-200)
                    {
                        h=h+200;
                        w=-100;
                    }
                }

    }
      public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
           add(heroButton); 
           add(cywilButton);               
    }   
      public void actionPerformed(ActionEvent e) {
            repaint();
    }
}

和带有按钮的类,添加带有图像的新对象:

package superworld;

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

class HeroButton extends JButton implements ActionListener {

    private JPanel buttonPanel;

    HeroButton(JPanel buttonPanel) {
        super("Dodaj hero");
        this.buttonPanel = buttonPanel;
                setBounds(0,500,150,50);
        addActionListener(this);              
    }

    @Override
    public void actionPerformed(ActionEvent e) {
            SuperLudzie batman = new SuperLudzie();
            batman.imagelabel = new JLabel(batman.image);
            batman.imagelabel.setBounds(50,50,batman.image.getIconWidth(),batman.image.getIconHeight());
        buttonPanel.add(batman.imagelabel);                
    }
}

还有这个 SuperLudzie 的类:

package superworld;

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

public class SuperLudzie {
    private String imie;
    private int zycie;
    private int inteligencja;
    private int wytrzymalosc;
    private int sila; 
    private int umiejetnosci_walki;
    private int x,y;
    ImageIcon image = new ImageIcon("C:/Users/Zuzanna Sawala/Moje dokumenty/NetBeansProjects/SuperWorld/mysz.jpg");
    JLabel imagelabel;
}

一切都很好。我只对按钮创建的这个对象和图像有问题,它在单击后不可见,但在我调整窗口大小后。我知道它与 setVisibility(true);但我不确定在哪里使用它。

【问题讨论】:

  • 使用布局管理器
  • 是的,使用布局管理器,在你的 frame.setVisible() 之前,试试 frame.pack()。
  • 但是将来我希望我的对象在随机位置移动和显示,所以我想使用 setLayout(null);。我应该使用哪种布局?
  • “我希望我的对象在随机位置移动和显示” 使用自定义绘画。

标签: java image swing resize components


【解决方案1】:

使用SwingUtilities.invokeLater()EventQueue.invokeLater() 确保EDT 已正确初始化。

使用重写的paintComponent() 方法而不是paint()

class SuperPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ...
    }

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

Read more points...

尽量避免null布局,并根据我们的需要使用适合的布局。

请查看How to Use Various Layout Managers,该How to Use Various Layout Managers 仅负责组件的定位和调整大小。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多