JFrame没有正确加载

时间:2015-06-07 19:43:43

标签: java swing jframe jbutton

我创建了一个包含多个类的Java项目,其中一个是Shell.java。这将启动一个GUI(使用swing),其中按钮和图像可以使用className.main(args);

启动其他类

除了一个按钮之外,这个工作正常,但我已经像其他所有人一样对该部分进行了编码......(下面的代码):

File morseFile = new File("c:\\temp\\morse.png");
Icon morseIcon = new ImageIcon(morseFile.getAbsolutePath());
JButton morseButton = new JButton(morseIcon);
morseButton.setBorder(emptyBorder);
morseButton.setContentAreaFilled(false);    
morseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        Morsecodes.main(args);
    }
});

如果我在Morsecodes.jar中启动main方法,一切正常。但是,如果我从shell类启动它,那么会发生这种情况:

Screenshot http://fs1.directupload.net/images/150607/f24jcfq3.png

如您所见,它首先打印背景,但永远不会改变。如果按下X,它也不会关闭,我必须强制它通过任务管理器关闭。

我已经尝试过改变Morsecodes.jar所以我可以做(也没有工作):

morseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        new Morsecodes();
    }
});

运行另一个main方法的所有其他按钮工作正常。

@Programmer Shell类:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.*;
import javax.swing.border.Border;

public class Shell {

    public static void main(String[] args) throws IOException {

        JFrame frame = new JFrame("S H E L L");
        frame.setSize(600,600);
        frame.setLayout(new GridLayout(2,2));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel_1 = new JPanel();
        JPanel panel_2 = new JPanel();
        JPanel panel_3 = new JPanel();
        JPanel panel_4 = new JPanel();

        File calcFile = new File("c:\\temp\\calculator.png");
        Icon calcIcon = new ImageIcon(calcFile.getAbsolutePath());
        JButton calcButton = new JButton(calcIcon);
        Border emptyBorder = BorderFactory.createEmptyBorder();
        calcButton.setBorder(emptyBorder);
        calcButton.setContentAreaFilled(false);

        calcButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Calculator.main(args);
            }
        });

        File swFile = new File("c:\\temp\\stopwatch.png");
        Icon swIcon = new ImageIcon(swFile.getAbsolutePath());
        JButton stopwatchButton = new JButton(swIcon);
        stopwatchButton.setBorder(emptyBorder);
        stopwatchButton.setContentAreaFilled(false);
        stopwatchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Stopwatch.main(args);
            }
        });

        File morseFile = new File("c:\\temp\\morse.png");
        Icon morseIcon = new ImageIcon(morseFile.getAbsolutePath());
        JButton morseButton = new JButton(morseIcon);
        morseButton.setBorder(emptyBorder);
        morseButton.setContentAreaFilled(false);    
        morseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Morsecodes.main(args);
            }
        });

        File foodFile = new File("c:\\temp\\food.png");
        Icon foodIcon = new ImageIcon(foodFile.getAbsolutePath());
        JButton foodButton = new JButton(foodIcon);
        foodButton.setBorder(emptyBorder);
        foodButton.setContentAreaFilled(false);
        foodButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Food_Combining.main(args);
            }
        });



        panel_1.add(calcButton);
        panel_2.add(stopwatchButton);
        panel_3.add(morseButton);
        panel_4.add(foodButton);

        frame.add(panel_1);
        frame.add(panel_2);
        frame.add(panel_3);
        frame.add(panel_4);`
        frame.setVisible(true);

    }

}

Morsecodes.java的主类:

public static void main(String[] args) {

    JFrame pathsetter = new JFrame("P A T H");
    pathsetter.setSize(350,100);
    JPanel pathpanel = new JPanel(new FlowLayout());
    JComboBox path = new JComboBox();
    path.addItem("c:\\MorseCodes\\Audio");
    path.setEditable(true);
    path.setSize(15,15);

    JButton button = new JButton("Set Path");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            A = new File(path.getSelectedItem()+"\\A.wav");
            B = new File(path.getSelectedItem()+"\\B.wav");
            C = new File(path.getSelectedItem()+"\\C.wav");
            D = new File(path.getSelectedItem()+"\\D.wav");
            E = new File(path.getSelectedItem()+"\\E.wav");
            F = new File(path.getSelectedItem()+"\\F.wav");
            G = new File(path.getSelectedItem()+"\\G.wav");
            H = new File(path.getSelectedItem()+"\\H.wav");
            I = new File(path.getSelectedItem()+"\\I.wav");
            J = new File(path.getSelectedItem()+"\\J.wav");
            K = new File(path.getSelectedItem()+"\\K.wav");
            L = new File(path.getSelectedItem()+"\\L.wav");
            M = new File(path.getSelectedItem()+"\\M.wav");
            N = new File(path.getSelectedItem()+"\\N.wav");
            O = new File(path.getSelectedItem()+"\\O.wav");
            P = new File(path.getSelectedItem()+"\\P.wav");
            Q = new File(path.getSelectedItem()+"\\Q.wav");
            R = new File(path.getSelectedItem()+"\\R.wav");
            S = new File(path.getSelectedItem()+"\\S.wav");
            T = new File(path.getSelectedItem()+"\\T.wav");
            U = new File(path.getSelectedItem()+"\\U.wav");
            V = new File(path.getSelectedItem()+"\\V.wav");
            W = new File(path.getSelectedItem()+"\\W.wav");
            X = new File(path.getSelectedItem()+"\\X.wav");
            Y = new File(path.getSelectedItem()+"\\Y.wav");
            Z = new File(path.getSelectedItem()+"\\Z.wav");
            One = new File(path.getSelectedItem()+"\\1.wav");
            Two = new File(path.getSelectedItem()+"\\2.wav");
            Three = new File(path.getSelectedItem()+"\\3.wav");
            Four = new File(path.getSelectedItem()+"\\4.wav");
            Five = new File(path.getSelectedItem()+"\\5.wav");
            Six = new File(path.getSelectedItem()+"\\6.wav");
            Seven = new File(path.getSelectedItem()+"\\7.wav");
            Eight = new File(path.getSelectedItem()+"\\8.wav");
            Nine = new File(path.getSelectedItem()+"\\9.wav");
            Zero = new File(path.getSelectedItem()+"\\0.wav");
            frame.setVisible(true);
            pathsetter.setVisible(false);
        }
    });
    pathpanel.add(path);
    pathpanel.add(button);
    pathsetter.add(pathpanel);
    pathsetter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pathsetter.setVisible(true);

    frame = new JFrame("M O R S E C O D E S");
    frame.setSize(350,350);
    frame.setLayout(new GridLayout(3,3));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayout(1,5));

    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout(1,1));

    JPanel panel3 = new JPanel();
    panel3.setLayout(new GridLayout(2,5));

    createCharTextField(char1);
    createCharTextField(char2);
    createCharTextField(char3);
    createCharTextField(char4);
    createCharTextField(char5);

    JButton translate = new JButton("Play Audio");
    translate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            createSound(char1);
            createSound(char2);
            createSound(char3);
            createSound(char4);
            createSound(char5);
        }
    });

    createOutputTextField(outputA_1);
    createOutputTextField(outputA_2);   
    createOutputTextField(outputA_3);
    createOutputTextField(outputA_4);
    createOutputTextField(outputA_5);

    createOutputTextField(outputB_1);
    createOutputTextField(outputB_2);
    createOutputTextField(outputB_3);
    createOutputTextField(outputB_4);
    createOutputTextField(outputB_5);

    panel1.add(char1);
    panel1.add(char2);
    panel1.add(char3);
    panel1.add(char4);
    panel1.add(char5);
    panel2.add(translate);
    panel3.add(outputA_1);
    panel3.add(outputA_2);
    panel3.add(outputA_3);
    panel3.add(outputA_4);
    panel3.add(outputA_5);
    panel3.add(outputB_1);
    panel3.add(outputB_2);
    panel3.add(outputB_3);
    panel3.add(outputB_4);
    panel3.add(outputB_5);
    frame.add(panel1);
    frame.add(panel3);
    frame.add(panel2);
    pathsetter.setLocationRelativeTo(null);
    frame.setLocationRelativeTo(null);

    while(true) {

        update(char1, outputA_1, outputB_1);
        update(char2, outputA_2, outputB_2);
        update(char3, outputA_3, outputB_3);
        update(char4, outputA_4, outputB_4);
        update(char5, outputA_5, outputB_5);

    }

}

(我想我不必发布我的所有方法)

1 个答案:

答案 0 :(得分:2)

据我了解这个问题:

当您点击JButton(morseIcon)按钮时,会调用Shell main,其中包含无限循环while(true),因此无法重新绘制您的窗口,甚至退出申请。

  • 您可以将main调用到新主题中:

    new Thread(() - > Morsecodes :: main).start();

  • 或者将你的循环放入线程......