2012-01-11 79 views
0

我在NetBeans中創建了一個GUI。這是一個聊天程序,我有4名突擊隊員像/加入,/留下,/耳語和/離開Java GUI JButton到actionlistner

private void CommandoActionPerformed(java.awt.event.ActionEvent evt) {             
     JOptionPane.showMessageDialog(rootPane, "The following commandos are:" + "\n" + "\n" + "/join Channel name" + "\n" + "/leave channel name" + "\n" + "/whisper nick message" + "\n" + "/quit - quit the program"); 
    } 

這是確定的,但我想actionlister代替showMessageDialog,所以我可以把他們和它的來自我的JTextField。我想我可以讓他們在那裏,但我不知道如何得到與此結合的actionlistener。

編輯: 我想要的是推動突擊隊按鈕,並得到一個窗口,我有4個新的按鈕,每個有一個突擊隊(/加入,/離開,/耳語和/退出),所以當我推1這些按鈕中,我得到了我的文本字段中的突擊隊,所以我只需要寫下其餘的。 所以,如果我推「/加入」按鈕,我只需要寫通道名稱。

EDIT2:如果我是在描述問題很糟糕,我可以給我想要的和已經完成的工作:

private void showCommandActionPerformed(java.awt.event.ActionEvent evt) {             
     Object[] options = { "/join", "/leave", "/whisper", "/quit", "Ingenting" }; 


     int choice= JOptionPane.showOptionDialog(rootPane, "What do u want to do? ", null, WIDTH, WIDTH, null, options, rootPane); 

     switch (choice) { 
       case 0: 
        skrivTekst.setText("/Join "); 
       skrivTekst.requestFocus(); 
        break; 
       case 1: 
        skrivTekst.setText("/Leave"); 
       skrivTekst.requestFocus(); 
        break; 
       case 2: 
        skrivTekst.setText("/Whisper"); 
       skrivTekst.requestFocus(); 
        break; 
       case 3: 
       skrivTekst.setText("/Join "); 
       skrivTekst.requestFocus(); 

       case 4: 

        System.exit(1); //this is wrong. i just want to close this window, not the whole program 
       default: 

        JOptionPane.showMessageDialog(null, "donno what!?!?!?!?!?!?!" + choice); 
      } 


    }     

我希望這個節目是我想要的,我做了什麼。 Ty to all :) 所以我留下的唯一問題是關閉一個JOptionPane窗口而不是程序

+0

你的問題不是很清楚,心態要改變嗎? – MByD 2012-01-11 12:19:07

+0

如果你想添加actionListener到JButton,然後使用'button.addActionListener(actionListener)'方法。 – 2012-01-11 12:20:52

+0

也許你已經看過http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html,有http://docs.oracle.com/javase/tutorial/uiswing/components/dialog。 html#按鈕和http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input, – mKorbel 2012-01-11 12:41:50

回答

1

你想要4個按鈕,每個按鈕在文本字段中設置一個命令文本,對嗎?

joinButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     theTextField.setText("/join"); 
    } 
}); 

並且對其他3個按鈕也做同樣的事情。

這是非常基本的東西。閱讀tutorial about event listeners

+0

@down_voter請希望你在這裏發一些描述 – mKorbel 2012-01-11 12:32:32

+0

downvoter:小心解釋你的downvote ? – 2012-01-11 12:33:05

+0

無論答案如何,只是問題本身有點不完整+1至少可以抵消downvote。 – Lion 2012-01-11 12:40:13

2

1)您可以在ButtonGroup實現JRadioButtons,那麼只有選擇之一將是可供選擇,也可以implelements ActionListener,並且內部ActionListenersetText()JTextField

2)請使用標準Swing JComponents而不是從palette準備Components,有時太硬,要覆蓋基本Swing方法

enter image description here

基於JRadioButton教程示例的簡單示例

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

import javax.swing.BorderFactory; 
import javax.swing.ButtonGroup; 
import javax.swing.ImageIcon; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

/* 
* RadioButtonDemo.java is a 1.4 application that requires these files: 
* images/Bird.gif images/Cat.gif images/Dog.gif images/Rabbit.gif 
* images/Pig.gif 
*/ 
public class RadioButtonDemo extends JPanel implements ActionListener { 

    private static String birdString = "Bird"; 
    private static String catString = "Cat"; 
    private static String dogString = "Dog"; 
    private static String rabbitString = "Rabbit"; 
    private static String pigString = "Pig"; 
    private static final long serialVersionUID = 1L; 
    private JLabel picture; 

    public RadioButtonDemo() { 
     super(new BorderLayout()); 
     //Create the radio buttons. 
     JRadioButton birdButton = new JRadioButton(birdString); 
     birdButton.setMnemonic(KeyEvent.VK_B); 
     birdButton.setActionCommand(birdString); 
     birdButton.setSelected(true); 
     JRadioButton catButton = new JRadioButton(catString); 
     catButton.setMnemonic(KeyEvent.VK_C); 
     catButton.setActionCommand(catString); 
     JRadioButton dogButton = new JRadioButton(dogString); 
     dogButton.setMnemonic(KeyEvent.VK_D); 
     dogButton.setActionCommand(dogString); 
     JRadioButton rabbitButton = new JRadioButton(rabbitString); 
     rabbitButton.setMnemonic(KeyEvent.VK_R); 
     rabbitButton.setActionCommand(rabbitString); 
     JRadioButton pigButton = new JRadioButton(pigString); 
     pigButton.setMnemonic(KeyEvent.VK_P); 
     pigButton.setActionCommand(pigString); 
     //Group the radio buttons. 
     ButtonGroup group = new ButtonGroup(); 
     group.add(birdButton); 
     group.add(catButton); 
     group.add(dogButton); 
     group.add(rabbitButton); 
     group.add(pigButton); 
     //Register a listener for the radio buttons. 
     birdButton.addActionListener(this); 
     catButton.addActionListener(this); 
     dogButton.addActionListener(this); 
     rabbitButton.addActionListener(this); 
     pigButton.addActionListener(this); 
     //Set up the picture label. 
     picture = new JLabel("Narrative"); 
     //The preferred size is hard-coded to be the width of the 
     //widest image and the height of the tallest image. 
     //A real program would compute this. 
     //picture.setPreferredSize(new Dimension(177, 122)); 
     //Put the radio buttons in a column in a panel. 
     JPanel radioPanel = new JPanel(new GridLayout(0, 1)); 
     radioPanel.add(birdButton); 
     radioPanel.add(catButton); 
     radioPanel.add(dogButton); 
     radioPanel.add(rabbitButton); 
     radioPanel.add(pigButton); 
     add(radioPanel, BorderLayout.LINE_START); 
     pigButton.setVisible(false); 
     rabbitButton.setVisible(false); 
     add(picture, BorderLayout.CENTER); 
     setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
    } 

    /** Listens to the radio buttons. 
    * @param e 
    */ 
    public void actionPerformed(ActionEvent e) { 
     String narr = e.getActionCommand(); 
     picture.setText(narr); 
    } 

    /** Returns an ImageIcon, or null if the path was invalid. 
    * @param path 
    * @return 
    */ 
    protected static ImageIcon createImageIcon(String path) { 
     java.net.URL imgURL = RadioButtonDemo.class.getResource(path); 
     if (imgURL != null) { 
      return new ImageIcon(imgURL); 
     } else { 
      System.err.println("Couldn't find file: " + path); 
      return null; 
     } 
    } 

    /** 
    * Create the GUI and show it. For thread safety, this method should be 
    * invoked from the event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
     //Make sure we have nice window decorations. 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     //Create and set up the window. 
     JFrame frame = new JFrame("RadioButtonDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //Create and set up the content pane. 
     JComponent newContentPane = new RadioButtonDemo(); 
     newContentPane.setOpaque(true); //content panes must be opaque 
     frame.setContentPane(newContentPane); 
     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
0

類似這樣的事情?

public void actionPerformed(ActionEvent event) { 
    Object source = event.getSource(); 
    if (source == join) { 
     textField.setText("/join"); 
    } else if (source == leave) { 
     textField.setText("/leave"); 
    } else if (source == whisper) { 
     textField.setText("/join"); 
    } else { 
     textField.setText("/exit"); 
    } 
} 

這是假設您的按鈕被命名爲join,leave,whisper和exit。