繁体   English   中英

除非调整窗口大小,否则组件不会显示-Java

[英]Component won't display unless window is resized - Java

单击按钮后,图像可见性出现问题。 我有带框架的主要班级:

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

}

然后,我进入了Panel的所有组件类:

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)有关; 但我不确定在哪里使用它。

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

阅读更多要点...

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

请看一下如何使用各种布局管理器 ,这些管理器完全负责组件的定位和大小调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM