import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; 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; private String[] btnTexts = { "Information 1", "Information 2", "Information 3" }; private String dialogMsg = "Your Name is:%s Your Phone_Number is:%s Your address is:%s Your id is:%s Your habit is:%s"; public Address() { setTitle("Address"); setSize(700, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); add(new JButton("Choose Your Information"), BorderLayout.NORTH); add(new JButton("<<="), BorderLayout.WEST); JPanel panel = new JPanel(); panel.setBackground(Color.RED); panel.setLayout(new GridLayout(3, 1, 10, 10)); add(panel, BorderLayout.CENTER); for (String text : btnTexts) { JButton btn = new JButton(text); panel.add(btn); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String msg = String.format(dialogMsg, name, phone_Number, address, id, habit); JOptionPane.showMessageDialog(btn, msg); } }); } } public static void main(String[] args) { Address a = new Address(); a.setVisible(true); } } 将这段代码展现出的结果通过修改代码来达到美观化和高级化的目的
时间: 2023-06-25 07:04:15 浏览: 238
JAVA AWT图形界面编程方法.pdf
好的,我来为您修改代码,让它更美观和高级。以下是修改后的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
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;
private String[] btnTexts = { "Information 1", "Information 2", "Information 3" };
private String dialogMsg = "Your Name: %s\nYour Phone Number: %s\nYour Address: %s\nYour ID: %s\nYour Habit: %s";
private JLabel infoLabel; //新建一个标签用于显示当前选中的信息编号
public Address() {
setTitle("Address Book");
setSize(700, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//创建顶部面板
JPanel topPanel = new JPanel();
topPanel.setBackground(new Color(255, 218, 185)); //设置背景色
topPanel.setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
//创建"Choose Your Information"标签
JLabel titleLabel = new JLabel("Choose Your Information");
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
titleLabel.setHorizontalAlignment(JLabel.CENTER); //水平居中
topPanel.add(titleLabel, BorderLayout.CENTER);
//创建"Info"标签
infoLabel = new JLabel("Info 1");
infoLabel.setFont(new Font("Arial", Font.PLAIN, 16));
infoLabel.setHorizontalAlignment(JLabel.CENTER); //水平居中
topPanel.add(infoLabel, BorderLayout.EAST);
//创建左侧面板
JPanel leftPanel = new JPanel();
leftPanel.setBackground(new Color(255, 222, 173)); //设置背景色
leftPanel.setLayout(new GridLayout(3, 1, 10, 10));
add(leftPanel, BorderLayout.WEST);
//创建左侧面板的按钮
for (int i = 0; i < btnTexts.length; i++) {
JButton btn = new JButton(btnTexts[i]);
leftPanel.add(btn);
final int index = i + 1; //记录按钮编号
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infoLabel.setText("Info " + index); //更新当前选中的信息编号
//根据选择更新对话框信息
switch(index) {
case 1:
name = "John Doe";
phone_Number = "123-456-7890";
address = "123 Main St, Anytown USA";
id = "A123456789";
habit = "Jogging";
break;
case 2:
name = "Jane Smith";
phone_Number = "555-123-4567";
address = "456 Elm St, Anytown USA";
id = "B987654321";
habit = "Reading";
break;
case 3:
name = "Bob Johnson";
phone_Number = "888-555-1212";
address = "789 Oak St, Anytown USA";
id = "C24681012";
habit = "Gardening";
break;
}
String msg = String.format(dialogMsg, name, phone_Number, address, id, habit);
JOptionPane.showMessageDialog(btn, msg);
}
});
}
}
public static void main(String[] args) {
Address a = new Address();
a.setVisible(true);
}
}
修改后的代码主要做了以下几点改进:
1. 增加了两个面板,一个顶部面板和一个左侧面板,使界面分布更清晰。
2. 修改了按钮的排列方式和颜色,使它们更美观。
3. 新增了一个标签用于显示当前选中的信息编号,方便用户知道当前查看的信息。
4. 更新了对话框的信息,使它更具有可读性和易理解性。
5. 增加了一个switch语句来根据用户的选择更新对话框信息,避免了重复的代码。
阅读全文