新创建的框架的SetTitle与“单击的按钮”相同

时间:2018-12-01 16:56:10

标签: java swing button jframe

:)

单击按钮后,我想设置新创建的框架的标题,就像我单击的按钮一样。例如,如果我创建一个名为“ Hello”的按钮和另一个名为“ Goodbye”的按钮,我想在单击“ Hello”按钮后创建一个名为“ Hello”的新框架,并在单击“再见”按钮。我写了一个代码,您已经可以在其中添加新的按钮,希望您可以解决我的问题:)

代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class NamedButtons extends JFrame{

private static final long serialVersionUID = 1L;
private JPanel panel;
private JPanel panelRight;
private JButton addButton;
private JButton button;

private JFrame firstframe;
private JPanel openpanel;
private JPanel inputpanel;
private JTextField inputfield;
private JButton buttonenter;

public NamedButtons() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setLocation(400, 400);
    setLayout(new BorderLayout());


    paneladdRight();
    paneladdLeft();

}


private void paneladdLeft() {
    panel = new JPanel();
    addButton = new JButton("Add a Button");
    panel.add(addButton);
    addButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            createFrame1();
        }

    });
    getContentPane().add(BorderLayout.WEST, panel);


}

private void paneladdRight() {
    panelRight = new JPanel();
    panelRight.setLayout(new BoxLayout(panelRight, BoxLayout.PAGE_AXIS));
    getContentPane().add(BorderLayout.EAST, panelRight);

}

private void createFrame1() 
{
    EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            firstframe = new JFrame("Name");
            firstframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            openpanel = new JPanel();
            openpanel.setLayout(new BoxLayout(openpanel, BoxLayout.Y_AXIS));
            openpanel.setOpaque(true);

            inputpanel = new JPanel();
            inputpanel.setLayout(new FlowLayout());
            inputfield = new JTextField(10);
            buttonenter = new JButton("Enter");
            buttonenter.addActionListener(new ActionListener() 
            {
                String s = inputfield.getText();
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    // TODO Auto-generated method stub

                     if(s.equals(inputfield.getText())) 
                     {
                        System.out.println("The Field is empty");

                        }else {
                        button = new JButton(); 
                        button.setText(inputfield.getText());
                        button.addActionListener(new ActionListener() 
                        {

                            @Override
                            public void actionPerformed(ActionEvent arg0) // 
here is the Problem
                            {
                                // TODO Auto-generated method stub
                                System.out.println("New Button pressed");
                                JFrame frame = new 
JFrame(button.getText());// I want that the Frame is named like the Button i 
clicked
                                frame.setSize(500, 500);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                                frame.setVisible(true);



                            }                                   
                        });
                        panelRight.add(button);
                        panelRight.revalidate();
                        panelRight.repaint();   
                        firstframe.dispose();

                        }

                }                   
            });         

            inputpanel.add(inputfield);
            inputpanel.add(buttonenter);
            openpanel.add(inputpanel);
            firstframe.getContentPane().add(BorderLayout.CENTER, openpanel);
            firstframe.pack();
            firstframe.setLocationRelativeTo(addButton);
            firstframe.setVisible(true);
            firstframe.setResizable(false);
        }
    });
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    EventQueue.invokeLater(new Runnable() 
    {

        @Override
        public void run() 
        {
            // TODO Auto-generated method stub
            NamedButtons ex = new NamedButtons();
            ex.setVisible(true);


        }

    });
}

}

0 个答案:

没有答案