使用for循环创建TextField

时间:2019-11-14 15:42:24

标签: java multithreading

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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

public class Main extends JFrame {
    private JTextField noSeats,noAgent,maxWaitTime;
    public JPanel panel;
    private JButton btnCreate,btnBook;

    public Main () {

        JFrame frame = new JFrame("Book Application");
        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(1000,1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(700,700));

        noSeats = new JTextField("Number of Seats");
        noSeats.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                noSeats.setText("");
            }
        });

        noAgent = new JTextField("Number of Agent");
        noAgent.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                noAgent.setText("");
            }
        });

        maxWaitTime = new JTextField("Maximum waiting time");
        maxWaitTime.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                maxWaitTime.setText("");
            }
        });


        btnCreate = new JButton("Create Seats");
        btnCreate.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                int seatsNumber = Integer.parseInt(noSeats.getText());
                int waitingTime = Integer.parseInt(maxWaitTime.getText());
                TestProduceThread t1 = new TestProduceThread(seatsNumber, waitingTime);


            }
        });

        btnBook = new JButton("Book");


        frame.getContentPane().add(noSeats);
        frame.getContentPane().add(noAgent);
        frame.getContentPane().add(maxWaitTime);
        frame.getContentPane().add(btnCreate);
        frame.getContentPane().add(btnBook);

        frame.getContentPane().add(panel);
        frame.setVisible(true);

    }

    public synchronized void showThread (int number) {
        JTextField txtField;

        for (int i=0; i<number; i++) {
            txtField=new JTextField("Not Booked");
            panel.add(txtField);
        }

    }

    public static void main(String[] args) {
        new Main();
    }

}

我想使用foor循环创建TextField并显示在框架中,但是当我运行我的应用程序时,我将打开多画面,并为其分配座位数。如何解决此问题? ..................................... ............................................... ................................................... 。 。 .. 。 。

0 个答案:

没有答案