2011-04-23 122 views
-1

是否有任何代码可用于将所选单选按钮的值传递给另一个框架?Java - 将一个单选按钮的值传递给另一个框架

这是我的代码:

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

public class bookBatman extends JFrame implements ActionListener { 

    private JLabel jlbName, jlbTime, jlbPic, jlbDate, jlbDescription, jlbAuthor, jlbDateProduce, jlbDirector, jlbActor, jlbRate, jlbNoOfTicket, jlbPrice, jlbTotal; 
    private JTextField jtfNoOfTicket; 
    private JRadioButton jr1, jr2, jr3, jr4, jr5, jr6, jr7, jr8, jr9, jr10; 
    private JButton jTotal, jBook, jCancel; 

    Font f = new Font("Times",Font.BOLD,30); 

    public bookBatman() { 


     setLayout(null); //set LayoutManager 

     // initialized the label 
     jlbName = new JLabel ("Batman The Dark Knight"); 
     jlbTime = new JLabel ("Time :"); 
     jlbPrice = new JLabel ("RM 9.00"); 
     jlbPic = new JLabel(); 
     jlbPic.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\Batman.jpg")); 
     jlbTotal = new JLabel (" Total : RM 9.00"); 



     // add all the label on the frame 
     add(jlbName); 
     add(jlbPic); 
     add(jlbTime); 
     add(jlbPrice); 
     add(jlbTotal); 


     // set all the label positions 
     jlbName.setBounds(85, 78, 300, 18); //(int x, int y, int width, int height) 
     jlbPic.setBounds(74, 101, 180, 288); 
     jlbTime.setBounds(74, 400, 60, 18); 
     jlbPrice.setBounds (270, 477, 60, 18); 
     jlbTotal.setBounds (339, 475, 300, 22); 


     // initialized the textfield 
     jlbAuthor = new JLabel ("Directed by Christopher Nolan"); 
     jlbDateProduce = new JLabel ("Date : 17 July 2008"); 
     jlbDirector = new JLabel ("Author : Jonathan Nolan, Christopher Nolan"); 
     jlbActor = new JLabel ("Main Actor : Christian Bale"); 
     jlbRate = new JLabel ("Movie Rate : 13 PG (Parental Guidance)"); 
     jlbNoOfTicket = new JLabel ("Number of Ticket :"); 


     // add all the textfield on the frame 
     add(jlbAuthor); 
     add(jlbDateProduce); 
     add(jlbDirector); 
     add(jlbActor); 
     add(jlbRate); 
     add(jlbNoOfTicket); 


     // set the textfield position 
     jlbAuthor.setBounds (273, 102, 300, 18); 
     jlbDateProduce.setBounds (273, 132, 300, 18); 
     jlbDirector.setBounds (273, 162, 300, 18); 
     jlbActor.setBounds (273, 192, 300, 18); 
     jlbRate.setBounds (273, 222, 300, 18); 
     jlbNoOfTicket.setBounds (77, 478, 150, 18); 


     // initialize the Radio Button 
     jr1 = new JRadioButton ("11.40 AM"); 
     jr2 = new JRadioButton ("12.00 PM"); 
     jr3 = new JRadioButton ("1.40 PM"); 
     jr4 = new JRadioButton ("3.40 PM"); 
     jr5 = new JRadioButton ("5.40 PM"); 
     jr6 = new JRadioButton ("7.00 PM"); 
     jr7 = new JRadioButton ("9.00 PM"); 
     jr8 = new JRadioButton ("10.40 PM"); 
     jr9 = new JRadioButton ("11.40 PM"); 
     jr10 = new JRadioButton ("12.40 AM"); 


     // add all the radion button 
     add(jr1); 
     add(jr2); 
     add(jr3); 
     add(jr4); 
     add(jr5); 
     add(jr6); 
     add(jr7); 
     add(jr8); 
     add(jr9); 
     add(jr10); 


     // set the radion button positions 
     jr1.setBounds (75, 423, 100, 24); 
     jr2.setBounds (172, 423, 100, 24); 
     jr3.setBounds (269, 423, 100, 24); 
     jr4.setBounds (366, 423, 100, 24); 
     jr5.setBounds (463, 423, 100, 24); 
     jr6.setBounds (75, 447, 100, 24); 
     jr7.setBounds (172, 447, 100, 24); 
     jr8.setBounds (269, 447, 100, 24); 
     jr9.setBounds (366, 447, 100, 24); 
     jr10.setBounds (463, 447, 100, 24); 


     // group the button 
     ButtonGroup group = new ButtonGroup(); 
     group.add(jr1); 
     group.add(jr2); 
     group.add(jr3); 
     group.add(jr4); 
     group.add(jr5); 
     group.add(jr6); 
     group.add(jr7); 
     group.add(jr8); 
     group.add(jr9); 
     group.add(jr10); 

     jr1.setActionCommand("radio1"); // for ButtonGroup 
     String sel = group.getSelection().getActionCommand(); 


     // initialize all the button 
     jTotal = new JButton ("Total"); 
     jBook = new JButton ("Book Now"); 
     jCancel = new JButton ("Cancel"); 


     // add all the button 
     add (jTotal); 
     add (jBook); 
     add (jCancel); 


     // set the button positions 
     jTotal.setBounds (191, 519, 83, 28); 
     jBook.setBounds (285, 519, 93, 28); 
     jCancel.setBounds (389, 519, 83, 28); 


     // add actionlistener 
     jTotal.addActionListener (this); 
     jBook.addActionListener (this); 
     jCancel.addActionListener (this); 


     // initialize all text field 
     jtfNoOfTicket = new JTextField (15); 

     // add all the text field 
     add (jtfNoOfTicket); 


     // set the text field positions 
     jtfNoOfTicket.setBounds (200, 477, 56, 22); 
    } 



    public void actionPerformed(ActionEvent e){ 
       if((e.getSource() == jTotal)) { 

        double price = 12.00; 
        double number = (Integer.parseInt(jtfNoOfTicket.getText().trim())); 
        double total = 0.0; 
        total = price * number; 
        jlbTotal.setText(" Total : RM" + total +"0"); 
       } 

       if((e.getSource() == jBook)) { 


        String name = jlbName.getText(); 
        String date = jlbDateProduce.getText(); 
        String time = jr1.getText(); 
        int number = (Integer.parseInt(jtfNoOfTicket.getText().trim())); 
        String total = jlbTotal.getText(); 
        String price = jlbPrice.getText(); 

        //Passing 
        ticketReservation frame = new ticketReservation(name, date, time, price, total, String.valueOf(number)); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setTitle("Ticket Reservation"); //set title of the window 
        frame.setSize(800,600); //size of the window 
        frame.setVisible(true); //visible the window 
        frame.setLocationRelativeTo (null); //center the window 

       } 

       if((e.getSource() == jCancel)) { 

        listOfMovies frame = new listOfMovies(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setTitle("List of Movies"); //set title of thewindow 
        frame.setSize(800,600); //size of the window 
        frame.setVisible(true); //visible the window 
        frame.setLocationRelativeTo (null); //center the window 

       } 
     } 

     public static void main (String [] args) { 

      bookBatman frame = new bookBatman(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setTitle("Book Batman : The Dark Knight"); //set title of thewindow 
      frame.setSize(800,600); //size of the window 
      frame.setVisible(true); //visible the window 
      frame.setLocationRelativeTo (null); //center the window 

     } 


} 
+1

一些随机的想法,因为我检查是不可编译码。为了更快地获得更好的帮助,请发布[SSCCE](http://pscode.org/sscce.html)。 这种性质的SSCCE不应该需要10'JRadioButton'的图像或220行代码,最多2(等)。 类名应该是AllUpperCase。 使用JFrame实例比扩展一个更好。 **使用布局。** 创建并更新EDT上的Swing组件。 – 2011-04-23 15:30:55

+1

请看下面的(几乎)SSCCE例子。如果有什么不清楚的地方,请询问。 – 2011-04-23 16:58:46

+0

crosspost:[java-forums:passing-value](http://www.java-forums.org/awt-swing/42911-passing-value.html) – 2011-04-23 17:38:31

回答

4

不要把它想成从一个GUI将信息传递给对方,但在其最基本的OOP形式,而认为它:从一个物体传递对象状态到另一个。通常我们使用公共访问器方法(即“getter”方法)来实现这个目的,这也可以在这里工作。

您的ButtonGroup对象将包含所选JRadioButton的ButtonModel(如果未选中任何一个,则为null),因此您可以从模型中获取信息并从getter方法中返回。

顺便说一句,您的代码有很多冗余,可以通过使用数组和使用合适的布局管理器来减少冗余。

编辑1:
对于实例
说我们创建包含了一堆JradioButton将的一个JPanel:

import java.awt.GridLayout; 
import javax.swing.*; 

class RadioBtnDialogPanel extends JPanel { 
    private static final String[] BUTTON_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 
    private ButtonGroup buttonGroup = new ButtonGroup(); 

    public RadioBtnDialogPanel() { 
     setLayout(new GridLayout(0, 1)); // give JPanel a decent layout 

     // create radio buttons, add to button group and to JPanel 
     for (String buttonText : BUTTON_TEXTS) { 
     JRadioButton radioBtn = new JRadioButton(buttonText); 
     radioBtn.setActionCommand(buttonText); // set the actionCommand here 
     buttonGroup.add(radioBtn); 
     add(radioBtn); 
     } 
    } 

    // getter or accessor method to get selected JRadioButton's actionCommand text 
    public String getSelectedButtonText() { 
     ButtonModel model = buttonGroup.getSelection(); 
     if (model == null) { // no radiobutton selected 
     return ""; 
     } else { 
     return model.getActionCommand(); 
     } 
    } 
} 

我们也给它一个查询的ButtonGroup的状态,以一个公共的getter方法找出已经选择了哪个按钮模型,然后返回它的actionCommand,这是一个保存描述单选按钮的文本的字符串(在这里它与单选按钮的文本相同)。

然后我们就可以显示出此JPanel的JOptionPane的在我们的主界面和JOptionPane的完成后,通过调用其getSelectedButtonText()方法上面的查询对象:

​​
+0

+1使用Swing [可分离模型体系结构](http://java.sun.com/products/jfc/tsc/articles/architecture/#separable)工作的好例子。 – trashgod 2011-04-23 17:38:48

+0

@trashgod:谢谢! – 2011-04-23 18:24:47

相关问题