java - 如何使用 JButton 为 ActionEvent 创建 If 语句

标签 java if-statement jbutton actionlistener joptionpane

如何仅显示属于用户按下的按钮的文本。截至目前,此代码打开显示其他按钮文本的所有窗口。我正在考虑 IF 语句,但我不确定为单击的按钮添加什么内容。如果有人知道如何为单击的按钮生成 if 语句,请分享信息。

第一个类

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class rohwcg extends JFrame
{

// adds the buttons
private JButton minerbutton;
private JButton farmerbutton;
private JButton lumberjackbutton;
    private JButton blacksmithbutton;

public rohwcg()
{
    super ("Realms of Havenwood Class Guide");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setBackground(Color.GREEN);

    //miner button
    Icon mbutton = new ImageIcon (getClass() .getResource("miner.png"));
    minerbutton = new JButton(mbutton);
    add(minerbutton);

    //farmer button
    Icon fbutton = new ImageIcon (getClass() .getResource("farmer.png"));
    farmerbutton = new JButton(fbutton);
    add(farmerbutton);

    //lumberjack button
    Icon lbutton = new ImageIcon (getClass() .getResource("lumberjack.png"));
    lumberjackbutton = new JButton(lbutton);
    add(lumberjackbutton);

    //blacksmith button
    Icon bbutton = new ImageIcon (getClass() .getResource("blacksmith.png"));
    blacksmithbutton = new JButton(bbutton);
    add(blacksmithbutton);

    //the action of the button.
    HandlerClass handler = new HandlerClass();
    minerbutton.addActionListener(handler);
    farmerbutton.addActionListener(handler);
    lumberjackbutton.addActionListener(handler);
    blacksmithbutton.addActionListener(handler);

    //sets the position of the button to center.
    blacksmithbutton.setAlignmentX(Component.CENTER_ALIGNMENT);
    minerbutton.setAlignmentX(Component.CENTER_ALIGNMENT);
    lumberjackbutton.setAlignmentX(Component.CENTER_ALIGNMENT);
    farmerbutton.setAlignmentX(Component.CENTER_ALIGNMENT);    
}

private class HandlerClass implements ActionListener
{
    //what happens when you click the button, below.
    public void actionPerformed(ActionEvent event)
    {           
        //farmer
        String farmertext = "null farmer";  
        JOptionPane.showMessageDialog(farmerbutton,farmertext,"The Farmer Class",JOptionPane.PLAIN_MESSAGE);

        //miner
        String minertext = "null miner";
        JOptionPane.showMessageDialog(minerbutton,minertext, "The Miner Class", JOptionPane.PLAIN_MESSAGE);

        //blacksmith
        String blacksmithtext ="null blacksmith";
        JOptionPane.showMessageDialog(blacksmithbutton,blacksmithtext, "The BlackSmith Class", JOptionPane.PLAIN_MESSAGE);

        //lumberjack
        String lumberjacktext = "null lumberjack";
        JOptionPane.showMessageDialog(lumberjackbutton, lumberjacktext, "The Lumberjack Class", JOptionPane.PLAIN_MESSAGE);         
    }

} 
}

二等

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class thehandler {
public static void main(String args []) 
{
    rohwcg classes1 = new rohwcg();
    classes1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    classes1.setSize(700,300);
    classes1.setVisible(true);

    //Sets the position of the window to a comman ratio. 
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    int x = d.width / 2;
    int y = (d.height / 2 ) - classes1.getHeight();
    classes1.setLocation(x,y);
}

}

最佳答案

没关系,我找到了答案!非常简单,您所需要做的就是添加一个被点击的对象

private class HandlerClass implements ActionListener

{

public void actionPerformed(ActionEvent click)  {

    Object source = click.getSource();

    if(source == farmerbutton)
    {           
    String farmertext = "Blocks a Farmer cannot break:" +"\r\n" +"\r\n" + "Any type of logs" + "\r\n" + "Stone" + "\r\n" + " Coal ore" + "\r\n" + "Iron ore" + "\r\n" + "Gold ore" + "\r\n" + "Diamond Ore" + "\r\n" + "Redstone ore" + "\r\n" + "Lapiz ore";  
    JOptionPane.showMessageDialog(farmerbutton,farmertext,"The Farmer Class",JOptionPane.PLAIN_MESSAGE);
    }


    if(source == minerbutton)
    {
    String minertext = "null miner";
    JOptionPane.showMessageDialog(minerbutton,minertext, "The Miner Class", JOptionPane.PLAIN_MESSAGE);
    }

    if(source == blacksmithbutton)
    {
    //blacksmith
    String blacksmithtext ="null blacksmith";
    JOptionPane.showMessageDialog(blacksmithbutton,blacksmithtext, "The BlackSmith Class", JOptionPane.PLAIN_MESSAGE);
    }

    if(source == lumberjackbutton)
    {
    //lumberjack
    String lumberjacktext = "null lumberjack";
    JOptionPane.showMessageDialog(lumberjackbutton, lumberjacktext, "The Lumberjack Class", JOptionPane.PLAIN_MESSAGE); 
    }
}

}

}

关于java - 如何使用 JButton 为 ActionEvent 创建 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13113505/

相关文章:

java - 将float和double转换为Java中的位和十六进制表示

java - Grails Quartz 插件在启用持久性的情况下在启动时删除触发器

java - 在Java中,将内部本地数组提取到外部是内存效率高还是速度优势?

java - if 语句只输入 if 之前打印

java - Action 监听器中实例化按钮的 NullPointerException

java - MongoDB Java API : Difference between com. mongodb.DBCollection.Save() 和 com.mongodb.DBCollection.Insert()?

c - 转换字符大小写的程序

R语言: If and else statement (loop)

java - 禁用时如何阻止 JButton 变灰?

java - 鼠标位置更新不正确