Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPanel内容不显示_Java_Swing_Jframe_Jpanel - Fatal编程技术网

Java JPanel内容不显示

Java JPanel内容不显示,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我的申请有问题。其概念是我想创建一个显示java命令进度的窗口。以下是该系统的代码: import java.awt.*; import javax.swing.*; class DosCommandsWindow extends JFrame{ JLabel firstMsg, progressMsg, enFolderMsg, dbMsg, shortcutMsg, finishedMsg;//koina JLabel.Etiketes host, port, dbName,

我的申请有问题。其概念是我想创建一个显示java命令进度的窗口。以下是该系统的代码:

import java.awt.*;

import javax.swing.*;

class DosCommandsWindow extends JFrame{

    JLabel firstMsg, progressMsg, enFolderMsg, dbMsg, shortcutMsg, finishedMsg;//koina JLabel.Etiketes host, port, dbName, user kai password

    public DosCommandsWindow()//arxikh dhmiourgia tou frame
    {
        super("Endocrino Installation");
        this.setSize(400,200);
        this.setMinimumSize(new Dimension(400,200));
        this.setMaximumSize(new Dimension(400,200));
        this.setResizable(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }//constractor

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout
    {
        JPanel panel=new JPanel();
        GridLayout g=new GridLayout(rows,columns,vgap,hgap);
        panel.setLayout(g);
        this.setContentPane(panel);

        this.addElements();
    }//createForm

    public void addProgressMsg()//prosthetei to minima "Plese wait.."
    {
        Container c=this.getContentPane();
        this.progressMsg=new JLabel("Progress");
        c.add(this.progressMsg);
    }


    public void addFirstMsg()//prosthetei to minima "Plese wait.."
    {
        Container c=this.getContentPane();
        this.firstMsg=new JLabel("Please wait...");
        c.add(this.firstMsg);
    }//addFirstMsg

    public void addEnFolderMsg()//prosthetei to label gia tin dimiourgia tou fakelou C:/Endocrino
    {
        Container c=this.getContentPane();
        this.enFolderMsg=new JLabel("jju");
        c.add(this.enFolderMsg);
    }//addEnFolderMsg

    public void addDBMsg()//prosthetei to label gia tin dimiourgia tis basis
    {
        Container c=this.getContentPane();
        this.dbMsg=new JLabel("jbj");
        c.add(this.dbMsg);
    }//addDBMsg

    public void addShortcutMsg()//prosthetei to label gia tin dimiourgia tou shortcut sto Desktop
    {
        Container c=this.getContentPane();
        this.shortcutMsg=new JLabel("jbgj");
        c.add(this.shortcutMsg);
    }//addShortcutMsg

    public void addFinishedMsg()//prosthetei to label gia tin dimiourgia tou minimatos "finished"
    {
        Container c=this.getContentPane();
        this.finishedMsg=new JLabel("jg");
        c.add(this.finishedMsg);
    }//addFinishedMsg

    public void showForm()//emfanizei to frame
    {
        this.setVisible(true);  
    }//showFrame

    public void addElements(){
        this.addProgressMsg();
        this.addFirstMsg();
        this.addEnFolderMsg();
        this.addDBMsg();
        this.addShortcutMsg();
        this.addFinishedMsg();
    }//addElements

    public void close(){
        this.dispose();
        System.exit(0);
    }

    public void dosCommands(){
        this.enFolderMsg.setText("Creating Endocrino Folder...");
        java.lang.Thread.sleep(1000);
        revalidate();repaint();
        this.dbMsg.setText("Creating Data Base...");
        java.lang.Thread.sleep(1000);
        revalidate();repaint();
        this.shortcutMsg.setText("Creating shortcut...");
        java.lang.Thread.sleep(1000);
        revalidate();repaint();
        this.finishedMsg.setText("Finished");
        JOptionPane.showMessageDialog(null,"Now you can delete the Endocrino folder from your Desktop");
        close();
    }//dosCommands
}//DosCOmmandsWindow
问题是,当我运行这个类时,它运行得很好,但是当从另一个jframe调用它时,它就不行了。这是调用DosCommandsWindow的框架

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;

class SettingsForm extends JFrame{

    JButton submit,defaultOption;//koumpia kataxwrhshs-akurwshs
    JLabel host;//koina JLabel.Etiketes host, port, dbName, user kai password
    JTextField hostT;//koina JTextField.perioxh egrafhs twn host, port, dbName, user kai password

    public SettingsForm()//arxikh dhmiourgia tou frame
    {
        super("Data Base Information");
        this.setSize(300,300);
        this.setMinimumSize(new Dimension(300,300));
        this.setMaximumSize(new Dimension(300,300));
        this.setResizable(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }//constractor

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout
    {
        JPanel panel=new JPanel();
        GridLayout g=new GridLayout(rows,columns,vgap,hgap);
        panel.setLayout(g);
        this.setContentPane(panel);

        this.addElements();

        submit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (hostT.getText().length()!=0)
                {
                    try {
                    CreateSettings s = new CreateSettings("Endocrino\\settings.txt");
                    s.write(hostT.getText());
                    close();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null,"All items must be filled","Warning",JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        defaultOption.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   hostT.setText("localhost");
            }
        });
    }//createForm


    public void addButtons()//prosthetei to koumpi kataxwrhshs sto panel
    {
        Container c=this.getContentPane();
        this.submit=new JButton("OK");
        this.defaultOption=new JButton("Set Default");

        c.add(submit);
        c.add(defaultOption);
    }//addButtons

    public void addHost()//prosthetei to label kai text field gia egrafh tou host apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.host=new JLabel("Host");
        this.hostT=new JTextField();
        c.add(host);
        c.add(hostT);
    }//addHost

    public void showForm()//emfanizei to frame
    {
        this.setVisible(true);
    }//showFrame

    public void addElements(){
        this.addHost();
        this.addButtons();
    }//addElements

    public void close(){
        this.dispose();
        DosCommandsWindow d=new DosCommandsWindow();
        d.createForm(6,1,3,3);
        d.showForm();
        d.dosCommands();
    }//close
}//SettingsForm

Swing的线程规则是,GUI的所有更新都在一个名为EDT的线程(也称为事件调度程序线程)上运行。因此,如果让线程进入睡眠状态,它会停止所有GUI更新过程并冻结,或者产生不可预测的结果

建议使用辅助线程,

或者Braj在评论中提到的摆动计时器

我粗略地猜测,应用程序无法按预期工作的原因与线程有关

我还建议编写具有MVC结构的swing应用程序, 因此,您可以配置一个帧,并使用控制器显示另一个帧

这里有一个很好的基础教程:


还有一个由装满鳗鱼的气垫船写的很好的例子,给了我很大的启发:

永远不要使用
Thread.sleep(1000)。尽量避免使用
setMinimumSize()
setMaximumSize()
setSize()
,并让布局管理器根据组件的首选尺寸设置尺寸和位置。在@Braj的建议中使用
JFrame#pack()
而不是
setSize()
1+。您的代码忽略了Java Swing线程规则。另外,您知道Java是区分大小写和拼写的,dosComandsWindow与dosComandsWindow不同。您的SettingsForm窗口声明了一个DosCommandsWindow,上面发布的类称为dosComandsWindow,它的拼写和大写字母不同。您的代码似乎还有很多不必要的圈复杂度。好的,您修复了一个大写问题,但仍然存在拼写问题。科曼兹!=命令。但更重要的是,Braj是对的,不要在Swing事件线程中调用
Thread.sleep(…)
,除非您想让整个GUI进入睡眠状态。谷歌搜索并阅读Swing中的
并发性
,了解这一点的重要性。在@Braj给出的建议中再添加一条建议是非常必要的。与使用多个JFrames不同,我建议使用和其他+方法,正如Braj所说,使用而不是
Thread.sleep()
方法。