2017-02-26 84 views
0

請有人可以幫助我,我正在爲我的電影票系統分配做一個座位預留部分...所以問題是我希望按鈕的文本顯示在選定的文本區域,而不是隻顯示具體的文本時取消選擇..但是當我取消選擇一個按鈕時,整個文本區域被清除。如何使這個文本區域顯示哪個切換按鈕被選中?

例如,當我選擇C1,C2,C3時 - 它正確顯示在文本區域中,但是如果我想取消選擇C3,文本區域現在必須僅顯示C1 & C2。相反,它會清除整個文本區域!

任何人都可以在邏輯中發現問題嗎?

import java.awt.*; 
    import static java.awt.Color.blue; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import javax.swing.border.Border; 


    public class Cw_Test2 extends JFrame { 

JButton btn_payment,btn_reset; 
JButton buttons; 
JTextField t1,t2; 



public static void main(String[] args) { 
    // TODO code application logic here 
    new Cw_Test2(); 
} 

public Cw_Test2() 
{ 
    Frame(); 

} 

    public void Frame() 
{ 
    this.setSize(1200,700);  //width, height 
    this.setTitle("Seat Booking"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 

    Font myFont = new Font("Serif", Font.ITALIC | Font.BOLD, 6); 
    Font newFont = myFont.deriveFont(20F); 

    t1=new JTextField(); 
    t1.setBounds(15, 240, 240,30); 

    JPanel thePanel = new JPanel() 
    { 
     @Override 
     public Dimension getPreferredSize() { 
     return new Dimension(350, 350); 
     }; 
    };  
    thePanel.setLayout(null); 

    JPanel ourPanel = new JPanel()   
    { 
     @Override 
     public Dimension getPreferredSize() { 
     return new Dimension(350, 350); 
     }; 
    }; 
    //ourPanel.setBackground(Color.green); 
    Border ourBorder = BorderFactory.createLineBorder(blue , 2); 
    ourPanel.setBorder(ourBorder); 
    ourPanel.setLayout(null); 
    ourPanel.setBounds(50,90, 1100,420); 

    JPanel newPanel = new JPanel()   
    { 
     @Override 
     public Dimension getPreferredSize() { 
     return new Dimension(350, 350); 
     }; 
    }; 
    //ourPanel.setBackground(Color.green); 
    Border newBorder = BorderFactory.createLineBorder(blue , 1); 
    newPanel.setBorder(newBorder); 
    newPanel.setLayout(null); 
    newPanel.setBounds(790,50, 270,340); 

    JLabel label1 = new JLabel(new ColorIcon(Color.GRAY, 400, 100)); 
    label1.setText("SCREEN"); 
    label1.setHorizontalTextPosition(JLabel.CENTER); 
    label1.setVerticalTextPosition(JLabel.CENTER); 
    label1.setBounds(130,50, 400,30); 

    JLabel label2 = new JLabel(" CINEMA TICKET MACHINE "); 
    label2.setBounds(250,50,400,30); 
    label2.setFont(newFont); 
    JLabel label3 = new JLabel("Please Select Your Seat"); 
    label3.setBounds(270,10,500,30); 
    label3.setFont(newFont); 
    JLabel label4 = new JLabel("Selected Seats:"); 
    label4.setBounds(20,10,200,30); 
    label4.setFont(newFont); 


    btn_payment=new JButton("PROCEED TO PAY"); 
    btn_payment.setBounds(35,290,200,30); 

    JLabel label6 = new JLabel("Center Stall"); 
    label6.setBounds(285,172,200,30); 

    JPanel seatPane, seatPane1, seatPane2, seatPane3, seatPane4, seatPane5; 
     JToggleButton[] seat2 = new JToggleButton[8]; 

    JTextArea chosen = new JTextArea(); 
chosen.setEditable(false); 
chosen.setLineWrap(true); 
chosen.setBounds(20, 60, 200, 100); 

    // Center Seats 
seatPane1 = new JPanel(); 
seatPane1.setBounds(200,200,250,65); 
seatPane1.setLayout(new FlowLayout()); 

for(int x=0; x<8; x++){ 


    seat2[x] = new JToggleButton("C"+(x+1)); 


    seatPane1.add(seat2[x]); 


    seat2[x].addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent ev){ 

      AbstractButton abstractButton = (AbstractButton) ev.getSource(); 
      boolean selected = abstractButton.getModel().isSelected(); 

      for(int x=0; x<8; x++){ 


      if(seat2[x]==ev.getSource()){ 
       if(selected){ 


        chosen.append(seat2[x].getText()+","); 


       }else{ 
        chosen.setText(seat2[x].getText().replace(seat2[x].getText(),"")); 





       } 



      } 

      } 

     } 

    }); 
} 

newPanel.add(chosen); 
ourPanel.add(seatPane1); 
ourPanel.add(label6); 
thePanel.add(label2); 
    ourPanel.add(label3); 
    ourPanel.add(label1); 
    newPanel.add(btn_payment); 
    newPanel.add(label4); 

    ourPanel.add(newPanel); 
    thePanel.add(ourPanel); 
    add(thePanel); 
    setResizable(false); 
    this.setVisible(true); 

} 


    public static class ColorIcon implements Icon 
{ 
    private Color color; 
    private int width; 
    private int height; 

    public ColorIcon(Color color, int width, int height) 
    { 
     this.color = color; 
     this.width = width; 
     this.height = height; 
    } 

    public int getIconWidth() 
    { 
     return width; 
    } 

    public int getIconHeight() 
    { 
     return height; 
    } 

    public void paintIcon(Component c, Graphics g, int x, int y) 
    { 
     g.setColor(color); 
     g.fillRect(x, y, width, height); 
    } 
} 
+0

你明白'setText'呢?也許你應該考慮使用'JList'來代替它,它會更容易更新 – MadProgrammer

+1

1)爲了更好地提供幫助,可以發佈一個[MCVE]或[Short,Self Contained,Correct Example](http://www.sscce。組織/)。這個問題確實只需要2-3個切換按鈕和一個文本區域來描述! 2)Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –

+0

@MadProgrammer ..好吧好友會嘗試一下 – Rvn

回答

2

當選擇或取消選擇的任何座位,該代碼迭代的座椅的陣列(在該方法中showSelectedSeats())並更新文本區域,以顯示所選擇的座椅。

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class CinemaTicketMachine { 

    private JComponent ui = null; 
    private JToggleButton[] seats = new JToggleButton[80]; 
    private JTextArea selectedSeats = new JTextArea(3, 40); 

    CinemaTicketMachine() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui!=null) return; 

     ui = new JPanel(new BorderLayout(4,4)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     selectedSeats.setLineWrap(true); 
     selectedSeats.setWrapStyleWord(true); 
     selectedSeats.setEditable(false); 
     ui.add(new JScrollPane(selectedSeats), BorderLayout.PAGE_END); 

     JPanel cinemaFloor = new JPanel(new BorderLayout(40, 0)); 
     cinemaFloor.setBorder(new TitledBorder("Available Seats")); 
     ui.add(cinemaFloor, BorderLayout.CENTER); 
     JPanel leftStall = new JPanel(new GridLayout(0, 2, 2, 2)); 
     JPanel centerStall = new JPanel(new GridLayout(0, 4, 2, 2)); 
     JPanel rightStall = new JPanel(new GridLayout(0, 2, 2, 2)); 

     cinemaFloor.add(leftStall, BorderLayout.WEST); 
     cinemaFloor.add(centerStall, BorderLayout.CENTER); 
     cinemaFloor.add(rightStall, BorderLayout.EAST); 

     ActionListener seatSelectionListener = new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       showSelectedSeats(); 
      } 
     }; 

     for (int ii=0; ii <seats.length; ii++) { 
      JToggleButton tb = new JToggleButton("S-" + (ii+1)); 
      tb.addActionListener(seatSelectionListener); 
      seats[ii] = tb; 
      int colIndex = ii%8; 
      if (colIndex<2) { 
       leftStall.add(tb); 
      } else if (colIndex<6) { 
       centerStall.add(tb); 
      } else { 
       rightStall.add(tb); 
      } 
     } 
    } 

    private void showSelectedSeats() { 
     StringBuilder sb = new StringBuilder(); 
     for (int ii=0; ii<seats.length; ii++) { 
      JToggleButton tb = seats[ii]; 
      if (tb.isSelected()) { 
       sb.append(tb.getText()); 
       sb.append(", "); 
      } 
     } 
     String s = sb.toString(); 
     if (s.length()>0) { 
      s = s.substring(0, s.length()-2); 
     } 
     selectedSeats.setText(s); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       CinemaTicketMachine o = new CinemaTicketMachine(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

Omg哥們,謝謝你sooo我一直在撓頭思考如何做到這一點..儘量花很多不眠之夜做這個任務(這只是它的一部分)..並且你給了我完美的解決方案,我需要..它的作品完美,就像我想要它..最後我明白了這個概念! – Rvn

+1

我也考慮看看['StringJoiner'](https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html);) – MadProgrammer

+0

*「buddy」*我不是你的朋友。我可能在FaceBook上,但這不是一個社交網站。 *「這不是我對此有疑問。」*我認爲'懷疑'你的意思是'問題'。保存你對政治家和二手車銷售人員的陳述的懷疑。至於你的進一步的問題,開始一個新的問題線程。 –

相關問題