java 容器限制大小,[求助]关于容器大小的

发布于:2021-10-22 03:49:44

package temp;


import javax.swing.*;


import java.awt.*;


import java.awt.event.*;


import java.util.Date;


public class PassAddBean extends JPanel implements ActionListener,PassEventListener


{


JLabel bel_PassengerNumber,bel_name,bel_address,bel_phone;


JTextField Tfield_PassengerNumber,Tfield_name,Tfield_address,Tfield_phone;


JButton but_PassengerSubmit;


JPanel panl_massage;


JLabel bel_date,bel_place,bel_number,bel_tiete;


JTextField Tfield_place,Tfield_number;


JFormattedTextField formaTfile;


GridBagConstraints gbc;


JButton but_submit;


String[] st_message = {"","",""};


int i_temp,i_counter;


public PassAddBean()


{


i_counter = 2;


//this.setSize(800,300);


panl_massage = new JPanel();


bel_PassengerNumber = new JLabel("Passenger Number:");


bel_name = new JLabel("Passenger Name:");


bel_address = new JLabel("Passenger Address:");


bel_phone = new JLabel("Passenger Phone:");


Tfield_PassengerNumber = new JTextField(15);


Tfield_PassengerNumber.setEditable(false);


Tfield_name = new JTextField(15);


Tfield_address = new JTextField(15);


Tfield_phone = new JTextField(15);


but_PassengerSubmit = new JButton("Submit");


but_PassengerSubmit.setPreferredSize(new Dimension(350, 23));


panl_massage.setLayout(new java.awt.GridLayout(5,2));


panl_massage.add(bel_PassengerNumber);panl_massage.add(Tfield_PassengerNumber);


panl_massage.add(bel_name);panl_massage.add(Tfield_name);


panl_massage.add(bel_address);panl_massage.add(Tfield_address);


panl_massage.add(bel_phone);panl_massage.add(Tfield_phone);


panl_massage.add(but_PassengerSubmit);


bel_date = new JLabel("Enter Date of Travel:");


bel_place = new JLabel("Enter Place of Travel:");


bel_number = new JLabel("Enter Number of seats to be booled:");


bel_tiete = new JLabel("Fly My Way AirLines");


bel_tiete.setFont(new Font("宋体", 0, 24));


//JFormattedTextField的使用翻API


formaTfile = new JFormattedTextField();


formaTfile.setPreferredSize(new Dimension(250, 23));


formaTfile.setValue(new Date());


Tfield_place = new JTextField(18);


Tfield_number = new JTextField(7);


but_submit = new JButton("BOOK SEATS");


setLayout(new java.awt.GridBagLayout());


GridBagConstraints gbc=new GridBagConstraints();


gbc.anchor=GridBagConstraints.WEST;


diy_add(1,0,bel_tiete,gbc);


diy_add(0,1,bel_date,gbc);diy_add(3,1,formaTfile,gbc);


diy_add(0,2,bel_place,gbc);diy_add(3,2,Tfield_place,gbc);


diy_add(0,3,bel_number,gbc);diy_add(3,3,Tfield_number,gbc);


diy_add(1,4,but_submit,gbc);


gbc.gridx=0;


gbc.gridy=8;


gbc.gridwidth=4;


add(panl_massage,gbc);


but_PassengerSubmit.addActionListener(this);


but_submit.addActionListener(this);


panl_massage.setVisible(false);


}


//因为不固定是那个Component所以得传GridBagConstraints进去,


//Component是传递进去对象的上限,再往上就认不到了...


void diy_add(int x,int y,JComponent c,GridBagConstraints gbc)


{


gbc.gridx=x;


gbc.gridy=y;


add(c,gbc);


}


public void actionPerformed(ActionEvent evt)


{


Object obj=evt.getSource();


if(obj==but_PassengerSubmit)


{


if (i_counter<=i_temp)


{


Tfield_PassengerNumber.setText(st_message[0]+st_message[1]+i_counter);


Tfield_name.setText("");


Tfield_address.setText("");


Tfield_phone.setText("");


i_counter++;


return;


}


else


{


String st_temp = "Number Passenger Added:"+(i_counter-1);


JOptionPane.showMessageDialog(null,st_temp);


panl_massage.setVisible(false);


but_submit.setEnabled(true);


firePassAddEvent();


}


}


if(obj==but_submit)


{


st_message[0] = formaTfile.getText();


st_message[1] = Tfield_place.getText();


st_message[2] = Tfield_number.getText();


if (st_message[1].equals("") || st_message[2].equals(""))


JOptionPane.showMessageDialog(null,"请输入你的信息");


else


{


try{


i_temp = Integer.parseInt(st_message[2]);


}


catch(NumberFormatException e)


{


JOptionPane.showMessageDialog(null,"请不要在票数眶里面输入非数字");


return;


}


if (i_temp == 0)


{


JOptionPane.showMessageDialog(null,"无效的定票数量");


return;


}


panl_massage.setVisible(true);


Tfield_PassengerNumber.setText(st_message[0]+st_message[1]+1);


but_submit.setEnabled(false);


this.setSize(800,300);


SwingUtilities.updateComponentTreeUI(this);


//this.repaint();


}


}


}


PassEventListener PelObj;


public void addPassEventListener(PassEventListener Pel)


{


PelObj=Pel;


}


public void firePassAddEvent()


{


}


public void Pass(PassAddEvent evt)


{


}


/*public static void main(String args[])


{


JFrame f= new JFrame("");


PassAddBean p= new PassAddBean();


f.getContentPane().add(p);


f.setSize(710,300);


f.setVisible(true);


}*/


}


[此贴子已经被作者于2007-3-11 13:34:32编辑过]







相关资源:JAVA*偈道绰胍约翱聪钅吭创

相关推荐

最新更新

猜你喜欢