简体   繁体   English

Java GUI无法正常工作

[英]Java gui not working properly

I have the following code to set up a gui to input source and target language from user using a JComboBox but on running it nothing except the button is displayed please suggest something.I have done initialization and all other stuff in the constructor. 我有以下代码来设置gui,以使用JComboBox从用户输入源语言和目标语言,但是在运行它时,除了显示按钮外,什么都没有显示,请提出一些建议。我已经在构造函数中完成了初始化以及所有其他工作。

import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Insets;
import java.io.FileNotFoundException;

public class SrcAndTargLangInput implements ActionListener {
    public static JFrame frame;
    public static JComboBox sourcLang;
    public static JComboBox targLang;
    public static JLabel setSrcLang;
    public static JLabel setTargLang;
    public static JButton ok;
    String[] lang = new String[2];

    public SrcAndTargLangInput() {
        ok = new JButton("Ok");
        ok.setBounds(150, 400, 100, 50);
        frame = new JFrame();
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(ok);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        Insets ins = frame.getInsets();
        frame.setSize(400 + ins.left + ins.right, 500 + ins.bottom + ins.top);
        setSrcLang = new JLabel("Source Language");
        setSrcLang.setBounds(50, 100, 100, 40);
        setTargLang = new JLabel("Target Language");
        setTargLang.setBounds(50, 200, 100, 40);
        String[] srcLangList = { "English", "Spanish", "French" };
        sourcLang = new JComboBox(srcLangList);
        sourcLang.setBounds(250, 100, 100, 40);
        String[] targLangList = { "English", "Spanish", "French" };
        targLang = new JComboBox(targLangList);
        targLang.setBounds(250, 200, 100, 40);
        frame.setVisible(true);
        ok.addActionListener(this);
    }

    public static void main(String args[]) {
        new SrcAndTargLangInput();
    }

    public void actionPerformed(ActionEvent e) {
        lang[0] = (sourcLang.getSelectedItem().toString());
        lang[1] = (targLang.getSelectedItem().toString());
        frame.setVisible(false);
    }
}

Did you double check your code first? 您是否首先仔细检查了代码? You've added your JButton to your frame's content pane but you have not added your JComboBox . 您已将JButton添加到框架的内容窗格中,但尚未添加JComboBox There is also no need to call frame.setVisible(true); 也无需调用frame.setVisible(true); twice; 两次; simply call it once you are done adding elements to the frame. 将元素添加到框架后,只需调用它即可。

Finally, you should not be running your Swing GUI code outside of the Event Dispatch Thread (EDT) or you may run into problems later with threading. 最后,您不应该在事件调度线程(EDT)之外运行Swing GUI代码,否则以后可能会遇到线程问题。 Change your main method to: 将您的主要方法更改为:

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SrcAndTargLangInput();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM