简体   繁体   中英

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.

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 . There is also no need to call 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. Change your main method to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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