Swing gui消失了

时间:2014-12-01 23:06:55

标签: java swing user-interface

我正在开发一个简单的GUI,其中包含添加,删除,写入磁盘等选项。当我编写程序时,我遇到了GUI开始消失并在设计选项卡中仅打开空白帧的问题/编辑器(也在运行时),我试图撤消多次,它回来了,但这次我到了一个我不能再撤消的点。我发布了我的所有代码。什么可以解决这个问题?

   package nikola.lozanovski.bitola;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Hashtable;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
    public class VozenRed extends JFrame {
        public VozenRed() {
        }
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private JPanel contentPane;
        private JButton Delete;
        private JButton Transporter;
        private JTextField IdBox;
        private JTextField InitialBox;
        private JTextField FinalBox;
        private JTextField Hbox;
        private JTextField PriceBox;
        private JTextField AgencyBox;
        private JTextField SaveBox;
        private JTextField Mbox;
        private JTextField DeleteBox;
        private JTextField DestinationsBox;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        VozenRed frame = new VozenRed();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        /**
         * Create the frame.
         * @return 
         */
        public void pishi(){ //tried deleting from here
        Hashtable<Broj, Elementi> h1=new Hashtable<Broj, Elementi>();
                try
                {
                Broj B=new Broj(IdBox.getText());
                Elementi elem=new Elementi(InitialBox.getText(), FinalBox.getText(), Hbox.getText(), Mbox.getText(), PriceBox.getText(), AgencyBox.getText());
                h1.put(B , elem);
                }
                catch(Exception e)//i tried NullPointerException still the same
                {
                    System.out.println("asd");//not realy what i meant
                }
        } //deleting to here, still no frame
        public void VozenRed1() {
            setTitle("Transport me");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 502, 396);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JButton InitialDestination = new JButton("Initial Destination");
            InitialDestination.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Input the initial destination, I.E. My Home", "Initial Destination", 2);
                }
            });
            InitialDestination.setBounds(255, 63, 194, 23);
            contentPane.add(InitialDestination);

            JButton FinalDestination = new JButton("Final Destination");
            FinalDestination.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Input final destination, I.E. Your Home", "Final Destination", 2);
                }
            });
            FinalDestination.setBounds(255, 97, 194, 23);
            contentPane.add(FinalDestination);

            JButton TimeDeparture = new JButton("Departure Time");
            TimeDeparture.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Input time, Hours in first box, Minutes in second box, I.E. 00:00", "Departure Time", 2);
                }
            });
            TimeDeparture.setBounds(255, 128, 194, 23);
            contentPane.add(TimeDeparture);

            JButton TicketPrice = new JButton("Ticket Price");
            TicketPrice.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null,"Input price, digits only, I.E. 100","TicketPrice",2);
                }
            });
            TicketPrice.setBounds(255, 159, 194, 23);
            contentPane.add(TicketPrice);

            Transporter = new JButton("Transport Agency");
            Transporter.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null,"Input transporter name, I.E. MyTrans","Transporter",2);
                }
            });
            Transporter.setBounds(255, 190, 194, 23);
            contentPane.add(Transporter);

            JButton SaveRoute = new JButton("Save Route");
            SaveRoute.setBounds(255, 320, 194, 23);
            contentPane.add(SaveRoute);

            Delete = new JButton("Delete");
            Delete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
            Delete.setBounds(255, 286, 194, 23);
            contentPane.add(Delete);

            JButton Destinations = new JButton("Destinations");
            Destinations.setBounds(255, 252, 194, 23);
            contentPane.add(Destinations);

            JLabel InputLabel = new JLabel("Input Elements");
            InputLabel.setBounds(44, 11, 211, 14);
            contentPane.add(InputLabel);

            JLabel CommandsLabel = new JLabel("Additional Commands");
            CommandsLabel.setBounds(44, 222, 201, 14);
            contentPane.add(CommandsLabel);

            JButton IdentificationNumber = new JButton("Identification Number");
            IdentificationNumber.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    JOptionPane.showMessageDialog(null, "Enter Identification number , only numbers, I.E. 0123","IdentificationNumber", 2);
                }
            });
            IdentificationNumber.setBounds(255, 29, 194, 23);
            contentPane.add(IdentificationNumber);

            JButton ClearAll = new JButton("Clear All");
            ClearAll.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    IdBox.setText(null);
                    InitialBox.setText(null);
                    FinalBox.setText(null);
                    Hbox.setText(null);
                    Mbox.setText(null);
                    PriceBox.setText(null);
                    AgencyBox.setText(null);
                }
            });
            ClearAll.setFont(new Font("Tahoma", Font.BOLD, 11));
            ClearAll.setBounds(255, 218, 194, 23);
            contentPane.add(ClearAll);

            JLabel lblHelpButtons = new JLabel("Help Buttons");
            lblHelpButtons.setBounds(314, 11, 76, 14);
            contentPane.add(lblHelpButtons);

            JButton HelpDestinations = new JButton("");
            HelpDestinations.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Outputs Destinations in a new box","Destinations", 2);
                }
            });
            HelpDestinations.setBounds(459, 252, 17, 23);
            contentPane.add(HelpDestinations);

            JButton HelpDelete = new JButton("");
            HelpDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Deletes selected Identification","Delete", 2);
                }
            });
            HelpDelete.setBounds(459, 286, 17, 23);
            contentPane.add(HelpDelete);

            JButton HelpSave = new JButton("");
            HelpSave.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Choose Directory to save","Save Document", 2);
                }
            });
            HelpSave.setBounds(459, 320, 17, 23);
            contentPane.add(HelpSave);

            JButton HelpClearAll = new JButton("");
            HelpClearAll.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Clears all the boxes above","Clear All", 2);
                }
            });
            HelpClearAll.setBounds(459, 218, 17, 23);
            contentPane.add(HelpClearAll);

            JButton IdentificationHelpButton = new JButton("");
            IdentificationHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    IdBox.setText(null);
                }
            });
            IdentificationHelpButton.setBounds(10, 29, 17, 23);
            contentPane.add(IdentificationHelpButton);

            JButton InitialDestinationHelpButton = new JButton("");
            InitialDestinationHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    InitialBox.setText(null);
                }
            });
            InitialDestinationHelpButton.setBounds(10, 60, 17, 23);
            contentPane.add(InitialDestinationHelpButton);

            JButton FinalDestinationHelpButton = new JButton("");
            FinalDestinationHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    FinalBox.setText(null);
                }
            });
            FinalDestinationHelpButton.setBounds(10, 97, 17, 23);
            contentPane.add(FinalDestinationHelpButton);



            JButton DeparturetimeHelpButton = new JButton("");
            DeparturetimeHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Hbox.setText(null);
                    Mbox.setText(null);
                }
            });
            DeparturetimeHelpButton.setBounds(10, 129, 17, 23);
            contentPane.add(DeparturetimeHelpButton);

            JButton TicketPriceHelpButton = new JButton("");
            TicketPriceHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    PriceBox.setText(null);
                }
            });
            TicketPriceHelpButton.setBounds(10, 159, 17, 23);
            contentPane.add(TicketPriceHelpButton);

            JButton TransporterHelpButton = new JButton("");
            TransporterHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    AgencyBox.setText(null);
                }
            });
            TransporterHelpButton.setBounds(10, 190, 17, 23);
            contentPane.add(TransporterHelpButton);

            JLabel label = new JLabel("        :");
            label.setBounds(113, 132, 39, 14);
            contentPane.add(label);

            JButton SaveDocumentButton = new JButton("");
            SaveDocumentButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    SaveBox.setText(null);
                }
            });
            SaveDocumentButton.setBounds(10, 320, 17, 23);
            contentPane.add(SaveDocumentButton);

            IdBox = new JTextField();


            IdBox.setBounds(44, 30, 201, 20);
            contentPane.add(IdBox);
            IdBox.setColumns(10);

            InitialBox = new JTextField();
            InitialBox.setColumns(10);
            InitialBox.setBounds(44, 64, 201, 20);
            contentPane.add(InitialBox);

            FinalBox = new JTextField();
            FinalBox.setColumns(10);
            FinalBox.setBounds(44, 98, 201, 20);
            contentPane.add(FinalBox);

            Hbox = new JTextField();
            Hbox.setColumns(10);
            Hbox.setBounds(44, 129, 86, 20);
            contentPane.add(Hbox);

            PriceBox = new JTextField();
            PriceBox.setColumns(10);
            PriceBox.setBounds(44, 160, 201, 20);
            contentPane.add(PriceBox);

            AgencyBox = new JTextField();
            AgencyBox.setColumns(10);
            AgencyBox.setBounds(44, 191, 201, 20);
            contentPane.add(AgencyBox);

            SaveBox = new JTextField();
            SaveBox.setColumns(10);
            SaveBox.setBounds(44, 321, 201, 20);
            contentPane.add(SaveBox);

            Mbox = new JTextField();
            Mbox.setColumns(10);
            Mbox.setBounds(145, 129, 100, 20);
            contentPane.add(Mbox);

            DeleteBox = new JTextField();
            DeleteBox.setColumns(10);
            DeleteBox.setBounds(44, 287, 201, 20);
            contentPane.add(DeleteBox);

            DestinationsBox = new JTextField();
            DestinationsBox.setColumns(10);
            DestinationsBox.setBounds(44, 253, 201, 20);
            contentPane.add(DestinationsBox);

            JButton DeleteHelpButton = new JButton("");
            DeleteHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    DeleteBox.setText(null);
                }
            });
            DeleteHelpButton.setBounds(10, 286, 17, 23);
            contentPane.add(DeleteHelpButton);

            JButton DestinationHelpButton = new JButton("");
            DestinationHelpButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    DestinationsBox.setText(null);
                }
            });
            DestinationHelpButton.setBounds(10, 252, 17, 23);
            contentPane.add(DestinationHelpButton);

        }
    }

2 个答案:

答案 0 :(得分:1)

我认为您在jframe的构造函数中错过了initComponents()

答案 1 :(得分:1)

您的代码似乎永远不会调用VozenRed1()中的所有UI内容。

您创建了VozenRed对象,该对象调用空构造函数,然后将其设置为可见。从不致电VozenRed1()