面向对象程序设计(Java)实验8

实验目的及内容

一、实验目的

  1. 掌握Swing图形用户界面下的控件的生成和使用。
  2. 掌握Java窗口的布局设计。

二、实验内容
上机实现下列程序并观察程序的运行情况:

  1. 使用JDialog对话框显示问候语。
  2. 用单选按钮组进行志向选择程序,每次只能选一个。

实验过程

问候语程序

package test8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jdialog {
    
    
    public static void main(String[] args)throws Exception {
    
    
        //编写主窗口
        JButton but1 = new JButton("确定");
        JFrame f = new JFrame("显示问候语实验");
        f.setLayout(null);
        f.setTitle("问候程序");
        f.setSize(600, 150);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel txt = new JLabel("请输入你的名字");
        txt.setLocation(100,10);
        txt.setSize(100,30);
        f.add(txt);

        JTextField input1 = new JTextField(1);
        input1.setSize(200, 30);
        input1.setLocation(200, 10);

        f.add(input1);

        but1.setSize(60,30);
        but1.setLocation(200,50);
        f.add(but1);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭按钮
        f.setVisible(true);

        //添加点击事件
        but1.addActionListener(new ActionListener(){
    
    
                @Override
                public void actionPerformed(ActionEvent ae) {
    
    
                    String name = input1.getText().trim();
                    JDialog d1 = new JDialog();
                    d1.setSize(200, 100);
                    JTextField txt2 = new JTextField(name + ",你好!", 1);
                    txt2.setEditable(false);
                    d1.add(txt2);
                    d1.show();

                }


        });

    }
}

大小与位置可自己更改,这里不再调整
在这里插入图片描述

单选程序

package test8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class danxuan {
    
    
    public static void main(String[] args)throws Exception {
    
    
        //编写主窗口
        JFrame f = new JFrame("单选实验");
        f.setLayout(null);
        f.setTitle("单选程序");
        f.setSize(300, 150);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel txt = new JLabel("将来要当");
        txt.setLocation(10,10);
        txt.setSize(100,30);
        f.add(txt);

        JRadioButton choose1=new JRadioButton("教师",false);
        JRadioButton choose2=new JRadioButton("工程师",false);
        JRadioButton choose3=new JRadioButton("程序员",false);
        choose1.setLocation(70, 10);
        choose1.setSize(60, 30);
        choose2.setLocation(140, 10);
        choose2.setSize(80, 30);
        choose3.setLocation(220, 10);
        choose3.setSize(70, 30);


        f.add(choose1);
        f.add(choose2);
        f.add(choose3);

        JLabel out = new JLabel("你选择了将来要当:");
        out.setSize(200,50);
        out.setLocation(50,50);
        f.add(out);

        choose1.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                if(choose1.isSelected()){
    
    
                    out.setText("你选择了将来要当:教师");
                }
            }
        });
        choose2.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                if(choose2.isSelected()){
    
    
                    out.setText("你选择了将来要当:工程师");
                }
            }
        });
        choose3.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                if(choose3.isSelected()){
    
    
                    out.setText("你选择了将来要当:程序员");
                }
            }
        });
        f.setVisible(true);
    }
}

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_51594676/article/details/124958250