Changing the JFrame title

前端 未结 4 503
终归单人心
终归单人心 2020-12-03 15:19

This code compiles, I just can\'t get the name to change on the title bar.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;  
import java.awt         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-03 15:37

    If your class extends JFrame then use this.setTitle(newTitle.getText());

    If not and it contains a JFrame let's say named myFrame, then use myFrame.setTitle(newTitle.getText());

    Now that you have posted your program, it is obvious that you need only one JTextField to get the new title. These changes will do the trick:

    JTextField poolLengthText, poolWidthText, poolDepthText, poolVolumeText, hotTub,
            hotTubLengthText, hotTubWidthText, hotTubDepthText, hotTubVolumeText, temp, results,
            newTitle;
    

    and:

        public void createOptions()
        {
            options = new JPanel();
            options.setLayout(null);
            JLabel labelOptions = new JLabel("Change Company Name:");
            labelOptions.setBounds(120, 10, 150, 20);
            options.add(labelOptions);
            newTitle = new JTextField("Some Title");
            newTitle.setBounds(80, 40, 225, 20);
            options.add(newTitle);
    //        myTitle = new JTextField("My Title...");
    //        myTitle.setBounds(80, 40, 225, 20);
    //        myTitle.add(labelOptions);
            JButton newName = new JButton("Set New Name");
            newName.setBounds(60, 80, 150, 20);
            newName.addActionListener(this);
            options.add(newName);
            JButton Exit = new JButton("Exit");
            Exit.setBounds(250, 80, 80, 20);
            Exit.addActionListener(this);
            options.add(Exit);
        }
    

    and:

    private void New_Name()
    {
        this.setTitle(newTitle.getText());
    }
    

提交回复
热议问题