JPanel内容未显示

时间:2014-07-14 23:01:00

标签: 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, 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

1 个答案:

答案 0 :(得分:2)

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

建议是使用工作线程,
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

或作为Braj的摇摆计时器在他的评论中提到 http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

我粗略猜测你的应用程序没有按预期工作的原因与线程有关。

另外我建议使用MVC结构编写swing应用程序, 所以你可以配置一个框架并用控制器显示另一个框架。

这是一个很好的基础教程:
http://www.newthinktank.com/2013/02/mvc-java-tutorial/

也是 Hovercraft Full Of Eels 写的一个很好的例子,它给了我很大的帮助:
https://stackoverflow.com/a/15729267/3610291