import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.GridLayout; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Address extends JFrame { private String name; private String Phone_Number; private String address; private String id; private String habit; public static void main(String[] args) { Address a = new Address(); } public Address() { setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); BorderLayout b = new BorderLayout(); setTitle(" Address"); setSize(700, 700); add(new Button("Choose Your Information"), BorderLayout.NORTH); add(new Button(" <<= "), BorderLayout.WEST); JPanel p = new JPanel(); p.setBackground(Color.RED); this.add(p,BorderLayout.CENTER); p.setLayout(new GridLayout(3, 1, 10, 10)); for (int i = 0; i < 1; i++) { JButton x=new JButton("Information 1"); JButton y=new JButton("Information 2"); JButton z=new JButton("Information 3"); p.add(x); p.add(y); p.add(z); x.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str1 =name; String str2 =Phone_Number; String str3 =address; String str4 =id; String str5 =habit; JOptionPane.showMessageDialog(x, "Your Name is:"+str1+" Your Phone_Number is:"+str2+" Your address is:"+str3+" Your id is:"+str4+" Your habit is:"+str5); } }); y.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str1 =name; String str2 =Phone_Number; String str3 =address; String str4 =id; String str5 =habit; JOptionPane.showMessageDialog(y, "Your Name is:"+str1+" Your Phone_Number is:"+str2+" Your address is:"+str3+" Your id is:"+str4+" Your habit is
时间: 2023-06-25 17:04:55 浏览: 140
:"+str5); } }); z.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str1 =name; String str2 =Phone_Number; String str3 =address; String str4 =id; String str5 =habit; JOptionPane.showMessageDialog(z, "Your Name is:"+str1+" Your Phone_Number is:"+str2+" Your address is:"+str3+" Your id is:"+str4+" Your habit is:"+str5); } }); } } }
这段代码是一个简单的Java GUI程序,用于展示和管理用户的个人信息。界面由一个标题栏、一个左侧的“返回”按钮和一个中央的面板组成。面板上包含三个按钮,每个按钮代表一个不同的用户信息。当用户点击某个按钮时,程序会弹出一个对话框,显示该用户信息的各个字段。这个程序还比较简单,但是可以作为学习Java GUI编程的入门例子。
相关问题
用import java.awt.*设计一个简单计算器
以下是一个使用 Java AWT 设计的简单计算器的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener {
private TextField resultField;
private double operand1 = 0;
private double operand2 = 0;
private String operator = "";
private boolean isOperatorSelected = false;
public Calculator() {
setTitle("Calculator");
resultField = new TextField("0", 20);
resultField.setEditable(false);
Button btn1 = new Button("1");
Button btn2 = new Button("2");
Button btn3 = new Button("3");
Button btn4 = new Button("4");
Button btn5 = new Button("5");
Button btn6 = new Button("6");
Button btn7 = new Button("7");
Button btn8 = new Button("8");
Button btn9 = new Button("9");
Button btn0 = new Button("0");
Button btnPlus = new Button("+");
Button btnMinus = new Button("-");
Button btnMultiply = new Button("*");
Button btnDivide = new Button("/");
Button btnEquals = new Button("=");
Button btnClear = new Button("C");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
btnPlus.addActionListener(this);
btnMinus.addActionListener(this);
btnMultiply.addActionListener(this);
btnDivide.addActionListener(this);
btnEquals.addActionListener(this);
btnClear.addActionListener(this);
Panel buttonPanel = new Panel(new GridLayout(4, 4, 5, 5));
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btnPlus);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btnMinus);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btnMultiply);
buttonPanel.add(btnClear);
buttonPanel.add(btn0);
buttonPanel.add(btnEquals);
buttonPanel.add(btnDivide);
setLayout(new BorderLayout(5, 5));
add(resultField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
switch (buttonText) {
case "C":
operand1 = 0;
operand2 = 0;
operator = "";
isOperatorSelected = false;
resultField.setText("0");
break;
case "+":
case "-":
case "*":
case "/":
if (isOperatorSelected) {
calculate();
}
operator = buttonText;
operand1 = Double.parseDouble(resultField.getText());
isOperatorSelected = true;
break;
case "=":
if (isOperatorSelected) {
calculate();
isOperatorSelected = false;
}
break;
default:
if (isOperatorSelected) {
resultField.setText(buttonText);
operand2 = Double.parseDouble(buttonText);
} else {
resultField.setText(resultField.getText() + buttonText);
operand1 = Double.parseDouble(resultField.getText());
}
break;
}
}
private void calculate() {
operand2 = Double.parseDouble(resultField.getText());
switch (operator) {
case "+":
operand1 += operand2;
break;
case "-":
operand1 -= operand2;
break;
case "*":
operand1 *= operand2;
break;
case "/":
operand1 /= operand2;
break;
}
resultField.setText(Double.toString(operand1));
}
public static void main(String[] args) {
new Calculator();
}
}
```
该计算器界面包含一个文本框和一组按钮,用户可以通过点击按钮来输入数字和运算符,计算器支持加、减、乘、除四种基本运算,以及清除操作和等于操作。在用户输入数字和运算符的过程中,计算器会动态地更新文本框中的内容,用户点击等于操作后,计算器会根据之前的输入进行计算,并将计算结果显示在文本框中。
package test; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; public class Test3{ public static void main (String args[]) { Win win=new Win(); win.setTitle("输入序列号"); win.setBounds(10,10,460,360); } } class Police implements KeyListener,FocusListener{ public void keyPressed(KeyEvent e) { JTextField t=(JTextField)e.getSource(); if (t.getCarePosition()>=6) t.transferFocus(); } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void focusGained(FocusEvent e) { JTextField text=(JTextField)e.getSource(); text.setText(null); } public void focusLost(FocusEvent e){} } class Win extends JFrame { JTextField text[]=new JTextField[3]; Police police; JButton b; Win() { setLayout(new FlowLayout()); police = new Police(); for(int i=0;i<3;i++) { text[i]=new JTextField(7); text[i].addKeyListener(police); //监视键盘事件 text[i].addFocusListener(police); add(text[i]); } b=new JButton("确定"); add(b); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text[0].requestFocusInWindow(); } }哪里有错误
代码的缩进格式不够规范,需要进行调整。此外,代码中的一些注释也需要进行调整,以保证清晰易读。以下是修改后的代码:
```
package test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Test3{
public static void main (String args[]) {
Win win=new Win();
win.setTitle("输入序列号");
win.setBounds(10,10,460,360);
}
}
class Police implements KeyListener,FocusListener{
public void keyPressed(KeyEvent e) {
JTextField t=(JTextField)e.getSource();
if (t.getCarePosition()>=6)
t.transferFocus();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void focusGained(FocusEvent e) {
JTextField text=(JTextField)e.getSource();
text.setText(null);
}
public void focusLost(FocusEvent e){}
}
class Win extends JFrame {
JTextField text[]=new JTextField[3];
Police police;
JButton b;
Win() {
setLayout(new FlowLayout());
police = new Police();
for(int i=0;i<3;i++) {
text[i]=new JTextField(7);
text[i].addKeyListener(police); //监视键盘事件
text[i].addFocusListener(police);
add(text[i]);
}
b=new JButton("确定");
add(b);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text[0].requestFocusInWindow();
}
}
```
阅读全文