简体   繁体   English

为什么内容不显示? 但会在调整框架时出现

[英]Why does the content not appear ? but it will appear when the frame is adjusted

I'm now creating a simple program with Frame. 我现在正在使用Frame创建一个简单的程序。 I, however, face one unknown error that any contents do not appear ,except Panel sized 90 * 90 on the frame. 但是,我遇到一个未知错误,即任何内容都不会出现,除了面板上尺寸为90 * 90的面板。 when I adjust windows size by dragging , then the contents appear as I expected. 当我通过拖动调整窗口大小时,内容将按预期显示。 I've tired few methods by googling.. all the same.. not working . 我通过谷歌搜索累了几种方法..都一样..不起作用。

For making this clear, I will attach few screenshots. 为了清楚起见,我将附上一些屏幕截图。

  • Before mouse drag 拖动鼠标之前 在此处输入图片说明

  • After mouse drag 鼠标拖动后 在此处输入图片说明

Java code : Java代码:

    import java.awt.Color;
import java.awt.Container;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

class menuListener implements MenuListener {

    @Override
    public void menuSelected(MenuEvent e) {
        // TODO Auto-generated method stub
        System.out.println("menu clicked !");
        System.exit(0);
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void menuCanceled(MenuEvent e) {
        // TODO Auto-generated method stub

    }

}

public class pcMain extends JFrame implements Runnable {
    userSeat us;
    int user;
    JPanel[] userSeat;
    Container con;
    JPanel leftPane, rightPane;
    JMenuBar jm;
    JMenu exit;

    public pcMain(int user) {

        super("Pc Management Application");

        con = getContentPane();
        con.setBackground(new Color(0, 128, 128));

        setVisible(true);
        setSize(1100, 500);
        // setLayout(null);
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Menu bar
        jm = new JMenuBar();

        // Menu items
        exit = new JMenu("EXIT");
        exit.addMenuListener(new menuListener());

        // letPanel
        leftPane = new JPanel();
        leftPane.setLayout(null);
        // leftPane.setSize(800, 470);
        leftPane.setBorder(BorderFactory.createLineBorder(Color.black));
        leftPane.setOpaque(false);
        leftPane.setBounds(0, 0, 800, 500);

        // rightPanel
        rightPane = new JPanel();
        // rightPane.setSize(300, 470);
        rightPane.setBorder(BorderFactory.createLineBorder(Color.red));
        rightPane.setOpaque(false);
        rightPane.setBounds(800, 0, 300, 500);

        // adding all components
        this.user = user;
        jm.add(exit);
        us = new userSeat(user);
        userSeat = us.getSeat();
        setJMenuBar(jm);
        // add(leftPane);
        // add(rightPane);

    }

    @Override
    public void run() {

        int seatX = 10;
        int seatY = 10;

        // Add user seats into leftPane
        for (int i = 0; i < user; i++) {

            if (i != 0 && i % 5 == 0) {
                // Increase y value
                System.out.println("if : " + i);
                seatX = 10;
                seatY += 100;
            }

            // Increase x values
            userSeat[i].setLocation(seatX, seatY);
            leftPane.add(userSeat[i]);
            // add(leftPane);
            System.out.println("else :" + i);
            seatX += 95;

        }

        add(leftPane);

    }

    public static void main(String[] args) {
        new Thread(new pcMain(10)).start();

    }

}

As @Kiheru said I moved the method,setVisibe() to the last and to make it work, I added two methods repaint() & revalidate() after adding the leftPane. 正如@Kiheru所说,我将setVisibe()方法移到了最后一个方法并使其起作用,我在添加了leftPane之后添加了两个方法repaint()和revalidate()

see as below: 如下所示:

在此处输入图片说明

Thank you, all :) 谢谢你们 :)

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

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