JtextField Settext和静态类型

时间:2016-09-27 09:01:43

标签: java swing variables static jtextfield

我有一个Swing表单(Java)。在这种形式我有字段,例如Name1。我初始化它是这样的:

private JTextField Name1;

在此代码中,我将JTextField Name1添加到我的表单中:

tabbedPane.addTab("T6", null, panel, "T6");
panel.setLayout(null);
Name1.setBounds(73, 11, 674, 20);
panel.add(Name1);

另外,我的表格上有Button1。此按钮中的事件正在更改Name1的值。它的工作正常。

此外,我有一个Button 2,用Name1隐藏Tab:

tabbedPane.remove(1);
tabbedPane.repaint();
tabbedPane.revalidate();
frame.repaint();
frame.revalidate();

(当然,此后我再次打开我的tabpane)

毕竟,通过按下按钮4,我想将Name1的vlue更改为某些文本。

但它不起作用!!!!!! SetTex不起作用。该字段为空。

所以,如果我从

更改Name1声明
private JTextField Name1;

static JTextField Name1;

是的,它有效。但!然后,我无法使用

更改Name1的值
Name1.Settext("Example");

按钮4按下并变换后,我需要做什么才能使Name1可用????

所有代码是:

public class GUI {


    public JTextField Name_textField;



    public static void main(String[] args) {

        DB_Initialize();

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI window = new GUI();
                    window.frame.setVisible(true);
                    window.frame.setResizable(false);
                    FirstConnect FC = window.new FirstConnect();
                    ConnectStatus = true;
                    FC.FirstEntry();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public GUI() {

        CurrentEnty_textField = new JTextField();
        Name_textField = new JTextField();

        EntriesCountlbl = new JLabel("New label");
        initialize();
        EntriesCountlbl.setText(Integer.toString(EC1));

        if (LoginStatus == true) {

            System.out.println("LoginStatus == true");
            AdminPartOn();
            btnNewButton.setEnabled(false);

        } else {

            btnNewButton_3.setEnabled(false);

            tabbedPane.setEnabledAt(1, false);
            // UnableForm();
            AdminPartOff();

        }

    }



    public static void DB_Initialize() {

        conn3 = con3.DoConnect(); 

        int ID;

        ArrayList<String> list = new ArrayList<String>();
        String l;
        String p;

        list = con3.LoginFileRead();

        if (list.size() == 2) {

            l = list.get(0);
            p = list.get(1);

            System.out.println("Логин из файла = " + l);
            System.out.println("Пароль из файла = " + p);

            ID = con3.CRMUserRequest(conn3, l, p);
            AdminPanelData = con3.CRMUserFullData(conn3, l, p);

            if (ID != 0) {

                System.out.println("ID Юзера = " + ID);
                LoginStatus = true;

            }
        }

        EC1 = con3.CRMQuery_EntriesCount(conn3); // запрашиваем кол-во записей
        StatusTableEntriesCount = con3.CRMQueryStatus_EntriesCount(conn3);

        StatusTableFromCount = con3.CRMQueryFRom_EntriesCount(conn3);

        System.out.println("Entries count(Из модуля GYU): " + EC1);

        if (EC1 > 0) {
            CurrentEntry = 1;
            System.out.println("Все ОК, текущая запись - " + CurrentEntry);

        } else {

            System.out.println("Выскакивает обработчик ошибок");

        }

        con3.Ini();
        con3.CRMQuery2(conn3, EC1 + 1);

        StatusColumn = con3.CRMQueryStatus(conn3, StatusTableEntriesCount);
        FromColumn = con3.CRMQueryFrom(conn3, StatusTableFromCount);

    }

    public class FirstConnect {
        public void FirstEntry() {

            int CurStatus = F.GetStatus(CurrentEntry - 1);

            int CurFrom = F.GetFrom(CurrentEntry - 1);

            if (LoginStatus != false) {

                Name_textField.setText(F.GetName(CurrentEntry - 1));

            } else {

                Name_textField.setText("-");

            }

        }
    }


    private void initialize() {

        frame = new JFrame();
        panel = new JPanel();

        frame.setBounds(100, 100, 816, 649);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(10, 250, 780, 361);
        frame.getContentPane().add(tabbedPane);

        JPanel panel_1 = new JPanel();
        tabbedPane.addTab("\u0412\u0445\u043E\u0434", null, panel_1, null);

        btnNewButton = new JButton("\u0412\u0445\u043E\u0434");
        btnNewButton.setBounds(263, 285, 226, 37);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int ID;

                ID = con3.CRMUserRequest(conn3, LoginField.getText(), PasswordField.getText());
                if (ID == 0) {


                } else {


                    MainTab();
                    FirstEntry3();

                }

            }
        });
        panel_1.setLayout(null);
        panel_1.add(btnNewButton);


tabbedPane.addTab("\u041A\u043B\u0438\u0435\u043D\u0442", null, panel,
                "\u041A\u043E\u043D\u0442\u0430\u043A\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u043A\u043B\u0438\u0435\u043D\u0442\u0430");
        panel.setLayout(null);

        // Name_textField = new JTextField();
        Name_textField.setBounds(73, 11, 674, 20);
        panel.add(Name_textField);


        Name_textField.setHorizontalAlignment(SwingConstants.CENTER);
        Name_textField.setColumns(10);


        NextEntryButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if (NextEntryButton.isEnabled() != false) {



                    if (CurrentEntry < EC1) {


                        CurrentEntry = CurrentEntry + 1;


                        int CurStatus = F.GetStatus(CurrentEntry - 1);

                        int CurFrom = F.GetFrom(CurrentEntry - 1);


                        Name_textField.setText(F.GetName(CurrentEntry - 1));



                    } else {

                    }


                }

            }
        });

    }

    public void MainTab() {

        tabbedPane.addTab("1", null, panel,
                "1");
        tabbedPane.setEnabledAt(1, true);
        panel.setLayout(null);
    }

    public void FirstEntry3() {
        Name_textField.setText(F.GetName(CurrentEntry - 1));

    }
}

0 个答案:

没有答案