实验八 Swing图形用户界面程序设计(一)
一、实验目的
1.掌握Swing图形用户界面下的控件的生成和使用。
2.掌握Java窗口的布局设计。
3.理解Java程序设计中创建图形界面的基本方法。
4.掌握Java图形界面下布局设计的思路和技巧。
二、实验内容
上机实现下列程序并观察程序的运行情况:
1.使用JDialog对话框显示问候语,如图1所示。
图1 JDialog对话框显示问候语
2.用单选按钮组进行志向选择程序,每次只能选一个,如图2所示。
图2 单选按钮操作
三、实验要求(必做实验,2学时)
1.在“四、实验结果”小节,附上每道习题的程序代码和运行结果截图,具体格式要求见“实验报告撰写要求”。
2.在“五、问题总结”小节,总结在编程过程中遇到的难题、错误、解决方法及收获。
3.在“六、心得体会”小节,总结对本次实验涵盖的知识点掌握程度、对编程工具的使用心得体会、以及对实验内容的建议等。
四、实验结果
1、使用JDialog对话框显示问候语程序代码及其结果截图:
package code;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GreetingDialogApp extends JFrame {
private JTextField nameField;
public GreetingDialogApp() {
// 设置窗口属性
setTitle("自定义的JFrame窗体");
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
mainPanel.setBackground(new Color(240, 248, 255));
// 添加标题
JLabel titleLabel = new JLabel("请输入您的姓名:");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
titleLabel.setForeground(new Color(70, 130, 180));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// 创建输入面板
JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
inputPanel.setBackground(new Color(240, 248, 255));
nameField = new JTextField();
nameField.setFont(new Font("宋体", Font.PLAIN, 16));
nameField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(100, 149, 237)),
BorderFactory.createEmptyBorder(5, 10, 5, 10)
));
inputPanel.add(nameField, BorderLayout.CENTER);
// 创建按钮
JButton confirmButton = new JButton("确定");
confirmButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
confirmButton.setBackground(new Color(100, 149, 237));
confirmButton.setForeground(Color.WHITE);
confirmButton.setFocusPainted(false);
confirmButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showGreetingDialog();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(240, 248, 255));
buttonPanel.add(confirmButton);
mainPanel.add(inputPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
add(mainPanel);
}
// 显示问候对话框
private void showGreetingDialog() {
String name = nameField.getText().trim();
if (name.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入您的姓名", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
JDialog dialog = new JDialog(this, "JDialog对话框", true);
dialog.setSize(300, 180);
dialog.setLocationRelativeTo(this);
dialog.setLayout(new BorderLayout());
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPanel.setBackground(new Color(240, 248, 255));
JLabel greetingLabel = new JLabel(name + ",您好!", SwingConstants.CENTER);
greetingLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
greetingLabel.setForeground(new Color(70, 130, 180));
contentPanel.add(greetingLabel, BorderLayout.CENTER);
JButton closeButton = new JButton("关闭");
closeButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
closeButton.addActionListener(e -> dialog.dispose());
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(240, 248, 255));
buttonPanel.add(closeButton);
dialog.add(contentPanel, BorderLayout.CENTER);
dialog.add(buttonPanel, BorderLayout.SOUTH);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
GreetingDialogApp app = new GreetingDialogApp();
app.setVisible(true);
}
});
}
}
图8.1 实验八习题1对话框运行结果
2、用单选按钮组进行志向选择程序程序代码及其结果截图:
package code;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CareerSelectionApp extends JFrame {
private JLabel resultLabel;
public CareerSelectionApp() {
// 设置窗口属性
setTitle("关于单选按钮");
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
mainPanel.setBackground(new Color(248, 248, 255));
// 添加标题
JLabel titleLabel = new JLabel("将来要当:", SwingConstants.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
titleLabel.setForeground(new Color(106, 90, 205));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// 创建单选按钮面板
JPanel radioPanel = new JPanel(new GridLayout(3, 1, 10, 10));
radioPanel.setBackground(new Color(248, 248, 255));
ButtonGroup careerGroup = new ButtonGroup();
String[] careers = {"经理", "工程师", "教师"};
// 先创建结果标签
resultLabel = new JLabel("您选择了将来要当:", SwingConstants.CENTER);
resultLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
resultLabel.setForeground(new Color(106, 90, 205));
resultLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
for (String career : careers) {
JRadioButton radioButton = new JRadioButton(career);
radioButton.setFont(new Font("宋体", Font.PLAIN, 16));
radioButton.setBackground(new Color(248, 248, 255));
radioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateResultLabel(career);
}
});
careerGroup.add(radioButton);
radioPanel.add(radioButton);
}
// 设置默认选择(现在 resultLabel 已创建)
((JRadioButton) radioPanel.getComponent(0)).setSelected(true);
updateResultLabel(careers[0]);
mainPanel.add(radioPanel, BorderLayout.CENTER);
mainPanel.add(resultLabel, BorderLayout.SOUTH);
add(mainPanel);
}
// 更新结果标签
private void updateResultLabel(String career) {
resultLabel.setText("您选择了将来要当:" + career);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
CareerSelectionApp app = new CareerSelectionApp();
app.setVisible(true);
}
});
}
}
图8.2 实验八习题2单选按钮运行结果
五、问题总结
六、心得体会请根据我的代码及要求回答问题总结和心得体会,请注意问题总结和心得体会不要分太多点,最好各自一段
最新发布