String str=e.getActioncommand()作用
时间: 2024-03-30 18:33:43 浏览: 16
这段代码的作用是获取 ActionEvent 对象中触发事件的命令字符串。在 Java 中,很多组件(比如按钮、菜单等)都可以设置一个字符串作为其命令字符串(command string),当该组件被点击或者按下回车键时,就会触发一个动作事件,并且该事件中会包含触发事件的组件的命令字符串。通过调用 ActionEvent 对象的 getActionCommand 方法,就可以获取到该命令字符串。在 actionPerformed 方法中,通常会根据命令字符串进行不同的操作,从而实现不同的功能。例如,如果有两个按钮分别设置了 "save" 和 "cancel" 作为它们的命令字符串,那么在 actionPerformed 方法中就可以通过获取命令字符串来区分用户点击的是哪个按钮,从而执行不同的操作。
相关问题
public static void main(String[] args) throws Exception { JFrame frame=new JFrame("记事本程序"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setVisible(true); JTextField textField=new JTextField(); frame.add(textField); // 制作菜单栏 JMenuBar jMenuBar=new JMenuBar(); // 2个菜单文件和编辑 JMenu jMenu1=new JMenu("文件"); JMenu jMenu2=new JMenu("编辑"); // 菜单功能,按钮 JMenuItem jMenuItem1=new JMenuItem("打开"); jMenuItem1.addActionListener(new Hello()); jMenuItem1.setActionCommand("打开"); JMenuItem jMenuItem2=new JMenuItem("保存"); JMenuItem jMenuItem3=new JMenuItem("xing'j"); // 添加菜单栏 frame.setJMenuBar(jMenuBar); // 添加2个菜单 jMenuBar.add(jMenu1); jMenuBar.add(jMenu2); // 菜单加功能 jMenu1.add(jMenuItem1); jMenu2.add(jMenuItem2); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("打开")) { //文件选择框 JFileChooser fileChooser = new JFileChooser(); // 定义标题 fileChooser.setDialogTitle("选择文件"); // 设置默认路径 fileChooser.showOpenDialog(null); // 弹出窗口 fileChooser.setVisible(true); // 保存绝对路径 String address = fileChooser.getSelectedFile().getAbsolutePath(); try { FileReader fileReader = new FileReader(address); BufferedReader bufferedReader = new BufferedReader(fileReader); String str = ""; String strAll = ""; while ((str = bufferedReader.readLine()) != null) { strAll += str; } textField.setText(strAll); bufferedReader.close(); fileReader.close(); } catch (IOException fileNotFoundException) { fileNotFoundException.printStackTrace(); } } }
这段代码使用Java Swing编写了一个简单的记事本程序。程序打开后,会展示一个文本框和一个菜单栏,菜单栏包括文件和编辑两个菜单。文件菜单下有打开和保存功能,编辑菜单下有一个名为“xing'j”的按钮,但没有实现具体功能。当点击打开菜单下的打开功能时,会弹出一个文件选择框,用户可以选择任意一个文件,程序会将文件内容读入到文本框中展示出来。
利用JAVA语言采用GUI来实现计算器,并采用Swing组件,AWT事件处理实现加减乘除及开根号、对数、平方、三角正/余弦、求倒数、进制转换的功能代码。
好的,以下是一个简单的Java计算器的代码实现,包括加减乘除、开根号、对数、平方、三角正/余弦、求倒数、进制转换等功能。代码中使用了Swing组件和AWT事件处理。
```
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
//定义组件
private JPanel jp1,jp2;
private JTextField jtf;
private JButton jb[] = new JButton[20];
private String str[] = {"7","8","9","+","4","5","6","-","1","2","3","*","0",".","C","/","√","log","sin","cos"};
private double n1,n2,result;
private int op;
private boolean flag = false;//判断是否进行了运算操作
public Calculator(){
jp1 = new JPanel();
jtf = new JTextField(20);
jp1.add(jtf);
this.add(jp1,BorderLayout.NORTH);
jp2 = new JPanel();
jp2.setLayout(new GridLayout(5,4));
for(int i=0;i<19;i++) {
jb[i] = new JButton(str[i]);
jp2.add(jb[i]);
jb[i].addActionListener(this);//为按钮添加事件监听器
}
this.add(jp2,BorderLayout.CENTER);
//设置窗口属性
this.setTitle("Calculator");
this.setSize(300, 320);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if("0".equals(s) || "1".equals(s) || "2".equals(s) || "3".equals(s) || "4".equals(s) || "5".equals(s) || "6".equals(s) || "7".equals(s) || "8".equals(s) || "9".equals(s)) {
if(flag) {
jtf.setText("");
flag = false;
}
jtf.setText(jtf.getText()+s);//显示数字
}
else if("C".equals(s)) {
jtf.setText("");//清除文本框内容
}
else if("+".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
op = 1;
flag = true;
}
else if("-".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
op = 2;
flag = true;
}
else if("*".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
op = 3;
flag = true;
}
else if("/".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
op = 4;
flag = true;
}
else if("√".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
result = Math.sqrt(n1);
jtf.setText(String.valueOf(result));
}
else if("log".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
result = Math.log10(n1);
jtf.setText(String.valueOf(result));
}
else if("sin".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
result = Math.sin(n1);
jtf.setText(String.valueOf(result));
}
else if("cos".equals(s)) {
n1 = Double.parseDouble(jtf.getText());
result = Math.cos(n1);
jtf.setText(String.valueOf(result));
}
else if(".".equals(s)) {
jtf.setText(jtf.getText()+".");//显示小数点
}
else if("=".equals(s)) {
n2 = Double.parseDouble(jtf.getText());
switch(op) {
case 1:
result = n1 + n2;
break;
case 2:
result = n1 - n2;
break;
case 3:
result = n1 * n2;
break;
case 4:
result = n1 / n2;
break;
default:
break;
}
jtf.setText(String.valueOf(result));
flag = true;
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器可以实现基本的加减乘除、开根号、对数、平方、三角正/余弦、求倒数、进制转换等功能。希望对你有所帮助。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)