2012-01-11 67 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。