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 12:04:52 浏览: 120
:"+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); } }); } JPanel q = new JPanel(); q.setBackground(Color.BLUE); this.add(q,BorderLayout.SOUTH); q.setLayout(new GridLayout(1, 5, 10, 10)); q.add(new Label("Name:")); q.add(new Label("Phone Number:")); q.add(new Label("Address:")); q.add(new Label("ID:")); q.add(new Label("Habit:")); JPanel r = new JPanel(); r.setBackground(Color.YELLOW); this.add(r,BorderLayout.EAST); r.setLayout(new GridLayout(5, 1, 10, 10)); r.add(new Label("Enter Your Name:")); r.add(new Label("Enter Your Phone Number:")); r.add(new Label("Enter Your Address:")); r.add(new Label("Enter Your ID:")); r.add(new Label("Enter Your Habit:")); JPanel s = new JPanel(); s.setBackground(Color.GREEN); this.add(s,BorderLayout.WEST); s.setLayout(new GridLayout(5, 1, 10, 10)); s.add(new JButton("Submit Your Name")); s.add(new JButton("Submit Your Phone Number")); s.add(new JButton("Submit Your Address")); s.add(new JButton("Submit Your ID")); s.add(new JButton("Submit Your Habit")); } }
这是一个使用 Java 编写的地址簿程序,界面使用了 Swing 组件库,可以让用户输入和展示姓名、电话号码、地址、身份证号码和习惯等信息。程序使用了 BorderLayout 布局管理器,将主要的组件放在了框架的不同区域。其中,北部放置了一个“选择您的信息”的按钮,西部放置了一个“<<=”按钮,中央放置了一个 JPanel,用来展示用户的信息,南部放置了一个包含标签的 JPanel,用来展示用户信息的各个属性名称,东部放置了一个包含标签和按钮的 JPanel,用来让用户输入各个属性的值。每个属性的输入和展示都使用了 JButton 和 JOptionPane,使得程序使用起来更加方便。
阅读全文