GUI预留方程

时间:2013-10-07 11:18:42

标签: java swing user-interface

我正在为这个保留公共汽车座位的计划做点什么。这是一个学校项目。我是计算费用的一部分。例如:我会保留三个座位,所以我会点击我想要保留的按钮。文本框应显示三个席位的总费用。我做了这个累加器变量,期望它将保持该事务所按下的按钮总数,这样我就可以轻松地将它乘以价格。

package busreservationproject;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;






public class ReservationSubClass extends JFrame {

private JLabel A1;  private JLabel A2;
private JLabel B1;  private JLabel B2;
private JLabel C1;  private JLabel C2;
private JLabel D1;  private JLabel D2;
private JLabel E1;  private JLabel E2;
private JLabel F1;  private JLabel F2;
private JLabel G1;  private JLabel G2;
private JLabel H1;  private JLabel H2;
private JLabel I1;  private JLabel I2;
private JLabel J1;  private JLabel J2;



private JTextField counter; private JTextField bill;
private JLabel counterLab;  private JLabel bLabel;
private JButton r1button; private JButton r11button;
private JButton r2button; private JButton r12button;
private JButton r3button; private JButton r13button;
private JButton r4button; private JButton r14button;
private JButton r5button; private JButton r15button;
private JButton r6button; private JButton r16button;
private JButton r7button; private JButton r17button;
private JButton r8button; private JButton r18button;
private JButton r9button; private JButton r19button;
private JButton r10button; private JButton r20button;

private JPanel contentPane; 
private JButton billButton;




public ReservationSubClass()
{

    super("Bus Reservation");
    String string = "";


    contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());
    GridBagConstraints cons = new GridBagConstraints();
    add(contentPane);

    cons.insets = new Insets (0,0,10,10);
    A1 = new JLabel("A1");
    A1.setToolTipText("click the button below to reserve this seat");
    r1button = new JButton("reserve A1");
    cons.gridx = 0;
    cons.gridy = 1;
    contentPane.add(A1,cons);
    cons.gridx = 0;
    cons.gridy = 2;
    contentPane.add(r1button,cons);

    B1 = new JLabel("B1");
    B1.setToolTipText("click the button below to reserve this seat");
    r2button = new JButton("reserve B1"); 
    cons.gridx = 0;
    cons.gridy = 3;
    contentPane.add(B1,cons);
    cons.gridx = 0;
    cons.gridy = 4;
    contentPane.add(r2button,cons);

    C1 = new JLabel("C1");
    C1.setToolTipText("click the button below to reserve this seat");
    r3button = new JButton("reserve C1"); 
    cons.gridx = 0;
    cons.gridy = 5;
    contentPane.add(C1,cons);
    cons.gridx = 0;
    cons.gridy = 6;
    contentPane.add(r3button,cons);

    D1 = new JLabel("D1");
    D1.setToolTipText("click the button below to reserve this seat");
    r4button = new JButton("reserve D1"); 
    cons.gridx = 0;
    cons.gridy = 7;
    contentPane.add(D1,cons);
    cons.gridx = 0;
    cons.gridy = 8;
    contentPane.add(r4button,cons);

    E1 = new JLabel("E1");
    E1.setToolTipText("click the button below to reserve this seat");
    r5button = new JButton("reserve E1"); 
    cons.gridx = 0;
    cons.gridy = 9;
    contentPane.add(E1,cons);
    cons.gridx = 0;
    cons.gridy = 10;
    contentPane.add(r5button,cons);

    F1 = new JLabel("F1");
    F1.setToolTipText("click the button below to reserve this seat");
    r6button = new JButton("reserve F1"); 
    cons.gridx = 0;
    cons.gridy = 11;
    contentPane.add(F1,cons);
    cons.gridx = 0;
    cons.gridy = 12;
    contentPane.add(r6button,cons);

    G1 = new JLabel("G1");
    G1.setToolTipText("click the button below to reserve this seat");
    r7button = new JButton("reserve G1"); 
    cons.gridx = 0;
    cons.gridy = 13;
    contentPane.add(G1,cons);
    cons.gridx = 0;
    cons.gridy = 14;
    contentPane.add(r7button,cons);

    H1 = new JLabel("H1");
    H1.setToolTipText("click the button below to reserve this seat");
    r8button = new JButton("reserve H1"); 
    cons.gridx = 0;
    cons.gridy = 15;
    contentPane.add(H1,cons);
    cons.gridx = 0;
    cons.gridy = 16;
    contentPane.add(r8button,cons);

    I1 = new JLabel("I1");
    I1.setToolTipText("click the button below to reserve this seat");
    r9button = new JButton("reserve I1"); 
    cons.gridx = 0;
    cons.gridy = 17;
    contentPane.add(I1,cons);
    cons.gridx = 0;
    cons.gridy = 18;
    contentPane.add(r9button,cons);


    J1 = new JLabel("J1");
    J1.setToolTipText("click the button below to reserve this seat");
    r10button = new JButton("reserve J1"); 
    cons.gridx = 0;
    cons.gridy = 19;
    contentPane.add(J1,cons);
    cons.gridx = 0;
    cons.gridy = 20;
    contentPane.add(r10button,cons);


    //COL 2

    A2 = new JLabel("A2");
    A2.setToolTipText("click the button below to reserve this seat");
    r11button = new JButton("reserve A2");             
    cons.gridx = 100;
    cons.gridy = 1;
    contentPane.add(A2,cons);
    cons.gridx = 100;
    cons.gridy = 2;
    contentPane.add(r11button,cons);

    B2 = new JLabel("B2");
    B2.setToolTipText("click the button below to reserve this seat");
    r12button = new JButton("reserve B2");             
    cons.gridx = 100;
    cons.gridy = 3;
    contentPane.add(B2,cons);
    cons.gridx = 100;
    cons.gridy = 4;
    contentPane.add(r12button,cons);

    C2 = new JLabel("C2");
    C2.setToolTipText("click the button below to reserve this seat");
    r13button = new JButton("reserve C2");             
    cons.gridx = 100;
    cons.gridy = 5;
    contentPane.add(C2,cons);
    cons.gridx = 100;
    cons.gridy = 6;
    contentPane.add(r13button,cons);

    D2 = new JLabel("D2");
    D2.setToolTipText("click the button below to reserve this seat");
    r14button = new JButton("reserve D2");             
    cons.gridx = 100;
    cons.gridy = 7;
    contentPane.add(D2,cons);
    cons.gridx = 100;
    cons.gridy = 8;
    contentPane.add(r14button,cons);

    E2 = new JLabel("E2");
    E2.setToolTipText("click the button below to reserve this seat");
    r15button = new JButton("reserve E2");             
    cons.gridx = 100;
    cons.gridy = 9;
    contentPane.add(E2,cons);
    cons.gridx = 100;
    cons.gridy = 10;
    contentPane.add(r15button,cons);

    F2 = new JLabel("F2");
    F2.setToolTipText("click the button below to reserve this seat");
    r16button = new JButton("reserve F2");             
    cons.gridx = 100;
    cons.gridy = 11;
    contentPane.add(F2,cons);
    cons.gridx = 100;
    cons.gridy = 12;
    contentPane.add(r16button,cons);

    G2 = new JLabel("G2");
    G2.setToolTipText("click the button below to reserve this seat");
    r17button = new JButton("reserve G2");             
    cons.gridx = 100;
    cons.gridy = 13;
    contentPane.add(G2,cons);
    cons.gridx = 100;
    cons.gridy = 14;
    contentPane.add(r17button,cons);

    H2 = new JLabel("H2");
    H2.setToolTipText("click the button below to reserve this seat");
    r18button = new JButton("reserve H2");             
    cons.gridx = 100;
    cons.gridy = 15;
    contentPane.add(H2,cons);
    cons.gridx = 100;
    cons.gridy = 16;
    contentPane.add(r18button,cons);

    I2 = new JLabel("I2");
    I2.setToolTipText("click the button below to reserve this seat");
    r19button = new JButton("reserve I2");             
    cons.gridx = 100;
    cons.gridy = 17;
    contentPane.add(I2,cons);
    cons.gridx = 100;
    cons.gridy = 18;
    contentPane.add(r19button,cons);

    J2 = new JLabel("J2");
    J2.setToolTipText("click the button below to reserve this seat");
    r20button = new JButton("reserve J2");             
    cons.gridx = 100;
    cons.gridy = 19;
    contentPane.add(J2,cons);
    cons.gridx = 100;
    cons.gridy = 20;
    contentPane.add(r20button,cons);


    //COUNTER
    counterLab = new JLabel("counter");
    counter = new JTextField(20);
    counter.setEditable(false);
    cons.gridx = 300;
    cons.gridy = 5;
    contentPane.add(counterLab,cons);
    cons.gridx = 300;
    cons.gridy = 6;
    contentPane.add(counter,cons);

    bLabel = new JLabel("bill");
    bLabel.setToolTipText("enter the bill below");
    bill = new JTextField(20);

    billButton = new JButton("calculate");
    cons.gridx = 300;
    cons.gridy = 7;
    contentPane.add(bLabel,cons);
    cons.gridx = 300;
    cons.gridy = 8;
    contentPane.add(bill,cons);
    cons.gridx = 300;
    cons.gridy = 9;
    contentPane.add(billButton,cons);

    progHandler handler = new progHandler();

    r1button.addActionListener(handler);    r11button.addActionListener(handler);
    r2button.addActionListener(handler);    r12button.addActionListener(handler);
    r3button.addActionListener(handler);    r13button.addActionListener(handler);
    r4button.addActionListener(handler);    r14button.addActionListener(handler);
    r5button.addActionListener(handler);    r15button.addActionListener(handler);
    r6button.addActionListener(handler);    r16button.addActionListener(handler);
    r7button.addActionListener(handler);    r17button.addActionListener(handler);
    r8button.addActionListener(handler);    r18button.addActionListener(handler);    
    r9button.addActionListener(handler);    r19button.addActionListener(handler);    
    r10button.addActionListener(handler);   r20button.addActionListener(handler);

    bill.addActionListener(handler);
    billButton.addActionListener(handler);
    //counter.addActionListener(handler);


    }

    private class progHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e){

      int price = 450;
      int suc_price = 450;
      int total;
      int accumulator=1;





      if(e.getSource()==r1button)
      {
       total = price * accumulator;   
      counter.setText("" +total);
      r1button.setEnabled(false);
      accumulator++;


      }

     else if(e.getSource()==r2button)
      {
      total = price * accumulator;  
      counter.setText("" +accumulator);
      r2button.setEnabled(false);
      accumulator++;


      }
     else if(e.getSource()==r3button)
      {

      counter.setText("" +price);
      r3button.setEnabled(false);
      accumulator++;    

      }
     else if(e.getSource()==r4button)
      {

      counter.setText("" +price);
      r4button.setEnabled(false);


      }

     else if(e.getSource()==r5button)
      {

      counter.setText("" +price);
      r5button.setEnabled(false);


      }
     else if(e.getSource()==r6button)
      {

      counter.setText("" +price);
      r6button.setEnabled(false);


      }
     else if(e.getSource()==r7button)
      {

      counter.setText("" +price);
      r7button.setEnabled(false);


      }
     else if(e.getSource()==r8button)
      {

      counter.setText("" +price);
      r8button.setEnabled(false);


      }
     else if(e.getSource()==r9button)
      {

      counter.setText("" +price);
      r9button.setEnabled(false);


      }
     else if(e.getSource()==r10button)
      {

      counter.setText("" +price);
      r10button.setEnabled(false);


      }
     else if(e.getSource()==r11button)
      {

      counter.setText("" +price);
      r11button.setEnabled(false);


      }
     else if(e.getSource()==r12button)
      {

      counter.setText("" +price);
      r12button.setEnabled(false);


      }
     else if(e.getSource()==r13button)
      {

      counter.setText("" +price);
      r13button.setEnabled(false);


      }
     else if(e.getSource()==r14button)
      {

      counter.setText("" +price);
      r14button.setEnabled(false);


      }
     else if(e.getSource()==r15button)
      {

      counter.setText("" +price);
      r15button.setEnabled(false);


      }
     else if(e.getSource()==r16button)
      {

      counter.setText("" +price);
      r16button.setEnabled(false);


      }
     else if(e.getSource()==r17button)
      {

      counter.setText("" +price);
      r17button.setEnabled(false);


      }
     else if(e.getSource()==r18button)
      {

      counter.setText("" +price);
      r18button.setEnabled(false);


      }
     else if(e.getSource()==r19button)
      {

      counter.setText("" +price);
      r19button.setEnabled(false);


      }

     else if(e.getSource()==r20button)
      {

      counter.setText("" +price);
      r20button.setEnabled(false);


      }

    }
 }






 }

1 个答案:

答案 0 :(得分:2)

  • 使用JToggleButtons预订电影,普通或公共汽车

  • 在事件setSelectedIcon()上定义setBackground()或更改JToggleButton.isSelected()

  • 然后有两种方式(如何确定哪一个isSelected()

    1. 将所有JToggleButton放入MapList

    2. 在contianer中循环,其中JToggleButton被放置