当actionPerformed启动时,java JFrame将无法工作

问题描述 投票:0回答:1

我一直在谷歌上寻找解决这个问题的方法,但是找不到任何答案如何解决这个问题。我的问题是,当我从一个动作事件开始一个新的jframe时,按下一个按钮,JFrame打开但是程序开始冻结,弹出窗口保持空白。

如果瑞典有一些糟糕的节目或一些话,她就是鳕鱼我道歉:

启动upp类:

import java.util.ArrayList;

public class maineClassen {

    ArrayList<infoClass> Infon = new ArrayList<>();

    public static void main (String [] args)
    {
        referenser referens = new referenser();
        Startskärmen ss = new Startskärmen(referens);
    }

}

“startskärm”是com的第一个屏幕:

public class Startskärmen extends JFrame implements ActionListener {

referenser referens;
ArrayList<infoClass> Infon;

JButton öppna = new JButton("open");
JButton ny = new JButton("create new");
JButton radera = new JButton("erase");

JScrollPane pane = new JScrollPane();

DefaultListModel mod = new DefaultListModel();
JList list = new JList(mod);
JScrollPane  sp = new JScrollPane(list);

JLabel texten = new JLabel("pre-alpha 0.1");

public Startskärmen(referenser re)
{
    //references should be sent by itself or received
    referens = re;
    Infon = referens.getInfoReferens();

    //build up the window
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.LINE_AXIS));
    labelPanel.setBorder(BorderFactory.createEmptyBorder(10,10,0,10));
    labelPanel.add(texten);

    JPanel scrollPanel = new JPanel();
    scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.LINE_AXIS));
    scrollPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    scrollPanel.add(sp);// man kan ocksä sätta in --> pane  <--

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    buttonPanel.add(Box.createHorizontalGlue());
    buttonPanel.add(öppna);
    buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPanel.add(ny);
    buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPanel.add(radera);

    Container contentPane = getContentPane();
    contentPane.add(labelPanel,BorderLayout.NORTH);
    contentPane.add(scrollPanel, BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.PAGE_END);

    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setSize(500, 500);

    //adda action listener
    ny.addActionListener(this);
    öppna.addActionListener(this);
    radera.addActionListener(this);
    //skapaNyIC();

}

infoClass hh;

public void skapaNyIC()
{
    Infon.add(new infoClass());
    Infon.get(Infon.size() -1).referenser(Infon); //Infon.get(Infon.size() -1)
    mod.addElement(Infon.get(Infon.size() -1).getName());
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == ny)
    {
        skapaNyIC();
    }
    else if(e.getSource() == öppna)
    {
        JOptionPane.showMessageDialog(texten,"This function doesn't exist yet");
    }
    else if(e.getSource() == radera)
    {
        JOptionPane.showMessageDialog(texten, "This function doesn't exist yet");
    }       
}
}

将存储信息的类,并创建将显示信息的窗口(类):

public class infoClass {

ArrayList<infoClass> Infon;
private infoClass ic;
private  String namn = "inget namn existerar";
private String infoOmInfo = null;

private int X_Rutor = 3;
private int Y_Rutor = 3;

private String[] information = new String[X_Rutor + Y_Rutor];

//info om dessa värden
public infoClass()
{

}
public void referenser(infoClass Tic)
{
    ic = Tic;
    infonGrafiskt ig = new infonGrafiskt(ic);
}
public void referenser(ArrayList<infoClass> Tic)
{
    ic = Tic.get((Tic.size() - 1 ));
    System.out.println("inna");
    infonGrafiskt ig = new infonGrafiskt(ic);
    ig.setVisible(true);
    System.out.println("efter");
}
public String namnPåInfon()
{
    return namn; 
}

//namnen
public String getName()
{
    return namn;
}
public void setNamn(String n)
{
    namn = n;
}

//xkordinaterna
public int getX_Rutor()
{
    return X_Rutor;
}
public void setX_Rutor(int n)
{
    X_Rutor = n;
}
//y kordinaterna
public int getY_Rutor()
{
    return Y_Rutor;
}
public void setY_Rutor(int n)
{
    Y_Rutor = n;
}
//informationen
public String[] getInformationen()
{
    return information;
}
public void setInformationen(String[] n)
{
    information = n;
}
//infoOmInfo
public String getinfoOmInfo()
{
    return infoOmInfo;
}
public void setinfoOmInfo(String n)
{
    infoOmInfo = n;
}
}

将显示窗口创建的信息的类:

public class infonGrafiskt extends JFrame implements ActionListener{

    infoClass ic;
    infonGrafiskt ig;

    //tillrutnätet
    JPanel panel = new JPanel(new SpringLayout());

    boolean pausa = true;

    //sakerna till desigen grund inställningar GI = grund inställningar
    JButton GIklarKnapp = new JButton("Spara och gå vidare");
    JTextField GInamn = new JTextField();
    JLabel GINamnText = new JLabel("Namn:");
    JTextField GIxRutor = new JTextField();
    JLabel GIxRutorText = new JLabel("Antal rutor i X-led:");

    JTextField GIyRutor = new JTextField();

    JLabel GIyRutorText = new JLabel("Antal rutor i Y-led:");
    JLabel GIInfo = new JLabel("Grund Inställningar");

    // de olika framm:arna
    JFrame GIframe = new JFrame("SpringGrid");
    JFrame frame = new JFrame("SpringGrid");

    //info om denna infon som finns här
    JTextArea textArea = new JTextArea();
    JScrollPane infoOmClasen = new JScrollPane(textArea); //hadde text area förut
    JLabel infoRutan = new JLabel("Informatin om denna resuldatdatabank:");

    //namnet på informationsdatabanken
    JLabel namnetPåInfot = new JLabel("Namnet på denna resuldatdatabas.");
    JButton ändraNamn = new JButton("Ändra namn");

    JButton sparaAllt = new JButton("Spara allt");

    public infonGrafiskt(infoClass Tic)
    {
        //få startinfo
        namnOchRutor();
        ic = Tic;

        //skapar om rutan
        JPanel p1 = new JPanel();
        p1.setLayout(new BoxLayout(p1, BoxLayout.PAGE_AXIS));
        p1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        namnetPåInfot.setFont(new Font("Dialog",1,22));
        p1.add(namnetPåInfot);

        //pausa programet tills grundinställningarna är instälda
        int m =1;
        try {
            while(m ==1)
            {
            Thread.sleep(100);
            if(pausa == false)
                m =2;
            }
            } catch(InterruptedException e) {
            } 


        //Create the panel and populate it. skapar den så alla kommer åt den
        //JPanel panel = new JPanel(new SpringLayout());
        for (int i = 0; i < ic.getX_Rutor()*ic.getY_Rutor(); i++) {
            JTextField textField = new JTextField(Integer.toString(i));

            panel.add(textField);
        }

        //Lay out the panel.
        SpringUtilities.makeGrid(panel,
                                 ic.getY_Rutor(), ic.getX_Rutor(), //rows, cols
                                 5, 5, //initialX, initialY
                                 5, 5);//xPad, yPad

        //set up the window.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //p1.add(ändraNamn);

        JPanel p2 = new JPanel();
        p2.setLayout(new BoxLayout(p2, BoxLayout.PAGE_AXIS));
        p2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        p2.add(infoRutan);
        infoOmClasen.setPreferredSize(new Dimension(0,100));
        p2.add(infoOmClasen);

        JPanel p3 = new JPanel();
        p3.setLayout(new FlowLayout());
        p3.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
        p3.setLayout(new BoxLayout(p3, BoxLayout.LINE_AXIS));
        p3.add(ändraNamn);
        p3.add(sparaAllt);


        //Set up the content pane.
        panel.setOpaque(true); //content panes must be opaque
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
        frame.add(p1);
        frame.add(p2);
        frame.add(panel);//frame.setContentPane(panel);
        frame.add(p3);

        //Display the window.
        frame.pack();
        sparaAllt.addActionListener(this);
        ändraNamn.addActionListener(this);          
    }

    private void namnOchRutor()
    {
        System.out.println("inna 2");

        //sättigång action listner
        GIklarKnapp.addActionListener(this);
        frame.setVisible(false);
        //frameStart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GIframe.setLayout(new BoxLayout(GIframe.getContentPane(), BoxLayout.PAGE_AXIS));

        JPanel GIP0 = new JPanel();
        GIP0.setLayout(new BoxLayout(GIP0,BoxLayout.LINE_AXIS));
        GIP0.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        GIP0.add(GIInfo);
        GIInfo.setFont(new Font("Dialog",1,22));

        JPanel GIP1 = new JPanel();
        GIP1.setLayout(new BoxLayout(GIP1,BoxLayout.LINE_AXIS));
        GIP1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        GIP1.add(GINamnText);
        GIP1.add(Box.createRigidArea(new Dimension(10, 0)));
        GIP1.add(GInamn);

        JPanel GIP2 = new JPanel();
        GIP2.setLayout(new BoxLayout(GIP2,BoxLayout.LINE_AXIS));
        GIP2.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
        GIP2.add(GIxRutorText);
        GIP2.add(Box.createRigidArea(new Dimension(10, 0)));
        GIP2.add(GIxRutor);

        JPanel GIP3 = new JPanel();
        GIP3.setLayout(new BoxLayout(GIP3,BoxLayout.LINE_AXIS));
        GIP3.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
        GIP3.add(GIyRutorText);
        GIP3.add(Box.createRigidArea(new Dimension(10, 0)));
        GIP3.add(GIyRutor);

        JPanel GIP4 = new JPanel();
        GIP4.setLayout(new BoxLayout(GIP4,BoxLayout.LINE_AXIS));
        GIP4.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
        GIP4.add(GIklarKnapp);
        System.out.println("inna 3");
        //lägga till sakerna gurund instllnings framen
        GIframe.add(GIP0);
        GIframe.add(GIP1);
        GIframe.add(GIP2);
        GIframe.add(GIP3);
        GIframe.add(GIP4);
        //desigen
        System.out.println("inna 4");
        GIframe.pack();
        GIframe.setVisible(true);
        System.out.println("inna5");

    }



    /*public static void main (String [] args)
    {
        infoClass i = new infoClass();
        infonGrafiskt ig = new infonGrafiskt(i);
    }*/

    public void referenserna( infonGrafiskt Tig)
    {
        ig = Tig;
    }

    private void skrivTillbaka()
    {
        String[] tillfäligString = ic.getInformationen();

        Component[] children = panel.getComponents();
        for (int i=0;i<children.length;i++){
            if (children[i] instanceof JTextField){
                ((JTextField)children[i]).setText(tillfäligString[i]);
                System.out.println(tillfäligString[i]);
            }
        }

        namnetPåInfot.setText(ic.getName());
        textArea.setText(ic.getinfoOmInfo());
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == GIklarKnapp)
        {   
            //skicka x och y antal ett och så
            ic.setNamn(GInamn.getText());
            ic.setX_Rutor(Integer.parseInt(GIxRutor.getText()));
            ic.setY_Rutor(Integer.parseInt( GIyRutor.getText()));

            namnetPåInfot.setText(ic.getName());

            pausa = false;
            GIframe.setVisible(false);
            frame.setVisible(true);
        }

        if(e.getSource() == sparaAllt)
        {
            String[] tillfäligString = ic.getInformationen();
            Component[] children = panel.getComponents();
            for (int i=0;i<children.length;i++){
                if (children[i] instanceof JTextField){
                    tillfäligString[i] = ((JTextField)children[i]).getText();
                    System.out.println(tillfäligString[i]);
                }
            }

            ic.setInformationen(tillfäligString);

            ic.setNamn(namnetPåInfot.getText());
            ic.setinfoOmInfo(textArea.getText());
        }
        if(e.getSource() == ändraNamn)
        {
            skrivTillbaka();
        }

    }


    }

所以我现在的问题是我无法创建一个新的“infoclass”,它将在“infoGrafikst”类中​​显示信息。用代码:

        Infon.add(new infoClass());
    Infon.get(Infon.size() -1).referenser(Infon); //Infon.get(Infon.size() -1)
    mod.addElement(Infon.get(Infon.size() -1).getName());

这是由按钮点击触发的。

对不起所有的鳕鱼,但不知道如何以另一种方式显示我的问题。

非常感谢。我确实发现这是代码

        int m =1;
        try {
              while(m ==1) {
                 Thread.sleep(100);
                 if(pausa == false)
                   m =2;
              }
            } catch(InterruptedException e) {
            }

这使它不起作用......

java swing jframe jbutton
1个回答
1
投票

好吧,我没有通过你的代码可能是因为我懒得阅读代码页。我认为你正在创建的新窗口必须干扰qazxsw poi。

好吧,我做了一个简短的例子,可以帮助你,它顺利:

EDT
© www.soinside.com 2019 - 2024. All rights reserved.