从另一个Jframe打开后,JPanel内容变黑

时间:2012-02-25 00:01:53

标签: java swing concurrency jframe jpanel

所以我有一个带有JPanel的Jframe,并且那里有一些按钮。 按下按钮时,它会打开另一个Jframe,其中包含另一个Jpanel,但此Jframe的内容全部为黑色。 应该有4个按钮,当我单独打开第二个Jframe时,它显示并按照预期工作。 这可能与我在第二次使用thread.wait一段时间(frame.visible)的事实有关吗? 任何帮助将不胜感激。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: ThePetr
 * Date: 17/02/12
 * Time: 19:28
 * To change this template use File | Settings | File Templates.
 */
public class SelectKleurUI {
    private  JFrame frm = new JFrame("Kies kleur");
    private int gekozenKleur;

    public SelectKleurUI() {
        frm = new JFrame();
        Toolkit kit = frm.getToolkit();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
        Dimension d = kit.getScreenSize();
        int max_width = (d.width - in.left - in.right);
        int max_height = (d.height - in.top - in.bottom);
        frm.setSize(Math.min(max_width, 400), Math.min(max_height, 64));//whatever size you want but smaller the insets
        frm.setLocation((max_width - frm.getWidth()) / 2, (max_height - frm.getHeight() ) / 2);
        //frm.setUndecorated(true);
        frm.setResizable(false);
        frm.setVisible(true);

        frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        maakKnoppen();
    }

    public  int selectKleur(){

        while(frm.isVisible()){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.dispose();
        return gekozenKleur;
    }

    public void setKleur(int kleur){
        gekozenKleur=kleur;
        frm.dispose();
    }
    private  void maakKnoppen(){
        JPanel knoppenFrame = new JPanel(new GridLayout(1,0));
        JButton geel = new JButton("Geel");
        geel.setBackground(Color.yellow);
        geel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(0);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        JButton groen = new JButton("Groen");
        groen.setBackground(Color.green);
        groen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(1);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        JButton blauw = new JButton("Blauw");
        blauw.setBackground(Color.blue);
        blauw.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(2);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        JButton rood = new JButton("Rood");
        rood.setBackground(Color.red);
        rood.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(3);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        knoppenFrame.add(geel);
        knoppenFrame.add(groen);
        knoppenFrame.add(blauw);
        knoppenFrame.add(rood);
        frm.add(knoppenFrame,BorderLayout.NORTH);

    }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: ThePetr
 * Date: 17/02/12
 * Time: 14:55
 * To change this template use File | Settings | File Templates.
 */
public class Uno {
    private JFrame Hoofdvenster;
    private JPanel pnlOnder=new JPanel(new GridLayout(1,0));
    private JButton[] kaarten=new JButton[50];
    Uno(){
        Hoofdvenster = new JFrame();
        Hoofdvenster.setName("Uno");
        Toolkit kit = Hoofdvenster.getToolkit();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
        Dimension d = kit.getScreenSize();
        int max_width = (d.width - in.left - in.right);
        int max_height = (d.height - in.top - in.bottom);
        Hoofdvenster.setSize(Math.min(max_width, 800), Math.min(max_height, 600));//whatever size you want but smaller the insets
        Hoofdvenster.setLocation((max_width - Hoofdvenster.getWidth()) / 2, (max_height - Hoofdvenster.getHeight() ) / 2);
        maakComponenten();
        Hoofdvenster.setVisible(true);
        Hoofdvenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void maakComponenten(){
        for(int i=0;i<7;i++){
            //kaarten[i]=new JButton("Kaart "+i);
            ImageIcon btnIcon = createImageIcon("Images/Naamloos.gif");//new ImageIcon("./Images/Naamloos.gif");

            kaarten[i]=new JButton("Kaart "+(i+1),btnIcon);
            kaarten[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //JOptionPane.showMessageDialog(null, "Ok");
                    Hoofdvenster.setVisible(false);
                    SelectKleurUI selectKleurUI = new SelectKleurUI();
                    JOptionPane.showMessageDialog(null, ""+ selectKleurUI.selectKleur());
                    Hoofdvenster.setVisible(true);

                }
            });
            pnlOnder.add(kaarten[i]);


        }
        Hoofdvenster.add(pnlOnder,BorderLayout.SOUTH);
    }
    private static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Uno.class.getResource(path);

    return new ImageIcon(imgURL);
}
}

1 个答案:

答案 0 :(得分:3)

对Swing组件的所有调用都必须在事件派发线程上。 Swing组件不是线程安全的。

您的申请应该只有一个JFrame。您可以在一个JPanels中使用JFrame个{{1}}。