定义一个窗口类myframe

定义一个窗口类myframe,创建一个窗口实例f1,具体如下图,用于输入:姓名,年龄,性别信息,点确定按钮后,把信息显示在内容区,点重置按扭后,清空内容区内容.年龄的下拉框的内选项是1-150. (50分)

img

这个地址可以帮助你:https://blog.csdn.net/weixin_42291074/article/details/110440024

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame {
  private JTextField nameField;
  private JTextField ageField;
  private JTextField genderField;
  private JTextArea contentArea;
  private JComboBox ageCombo;
  
  public MyFrame() {
    setTitle("Input Information");
    setSize(600, 400);
    setLayout(new FlowLayout());
    
    JLabel nameLabel = new JLabel("Name:");
    add(nameLabel);
    
    nameField = new JTextField(20);
    add(nameField);
    
    JLabel ageLabel = new JLabel("Age:");
    add(ageLabel);
    
    ageCombo = new JComboBox();
    for (int i = 1; i <= 150; i++) {
      ageCombo.addItem(i);
    }
    add(ageCombo);
    
    JLabel genderLabel = new JLabel("Gender:");
    add(genderLabel);
    
    genderField = new JTextField(20);
    add(genderField);
    
    JButton confirmButton = new JButton("Confirm");
    confirmButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String name = nameField.getText();
        int age = (int) ageCombo.getSelectedItem();
        String gender = genderField.getText();
        contentArea.setText("Name: " + name + "\nAge: " + age + "\nGender: " + gender);
      }
    });
    add(confirmButton);
    
    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        nameField.setText("");
        ageCombo.setSelectedIndex(0);
        genderField.setText("");
        contentArea.setText("");
      }
    });
    add(resetButton);
    
    contentArea = new JTextArea(10, 50);
    add(contentArea);
  }
  
  public static void main(String[] args) {
    MyFrame f1 = new MyFrame();
    f1.setVisible(true);
  }
}

UI布局可能不一样,仅参考,望采纳

如何用java创建一个窗口
可以自己跟着学,了解后,你会发现非常简单
https://blog.csdn.net/weixin_51768412/article/details/117712972

我之前写了这个,你那个其实可以写,不过得加,有意找我

img

我强项

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame {
  private JTextField nameField;
  private JTextField ageField;
  private JTextField genderField;
  private JTextArea contentArea;
  private JComboBox ageCombo;
  public MyFrame() {
    setTitle("Input Information");
    setSize(600, 400);
    setLayout(new FlowLayout());
    JLabel nameLabel = new JLabel("Name:");
    add(nameLabel);
    nameField = new JTextField(20);
    add(nameField);
    JLabel ageLabel = new JLabel("Age:");
    add(ageLabel);
    ageCombo = new JComboBox();
    for (int i = 1; i <= 150; i++) {
      ageCombo.addItem(i);
    }
    add(ageCombo);
    JLabel genderLabel = new JLabel("Gender:");
    add(genderLabel);
    genderField = new JTextField(20);
    add(genderField);
    JButton confirmButton = new JButton("Confirm");
    confirmButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String name = nameField.getText();
        int age = (int) ageCombo.getSelectedItem();
        String gender = genderField.getText();
        contentArea.setText("Name: " + name + "\nAge: " + age + "\nGender: " + gender);
      }
    });
    add(confirmButton);
    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        nameField.setText("");
        ageCombo.setSelectedIndex(0);
        genderField.setText("");
        contentArea.setText("");
      }
    });
    add(resetButton);
    contentArea = new JTextArea(10, 50);
    add(contentArea);
  }
  public static void main(String[] args) {
    MyFrame f1 = new MyFrame();
    f1.setVisible(true);
  }
}

swing+awt,不费劲

使用的坐标布局,可以自行优化位置。
setBounds的四个参数分别是 x 坐标、y坐标、宽度、高度。


import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame {

    private JTextField nameField;
    private JComboBox<Integer> ageCombo;
    private JRadioButton manRadio;
    private JRadioButton womanRadio;
    private JTextArea contentArea;

    public MyFrame() {
        setLayout(null);
        setLocationRelativeTo(null);
        setSize(300, 400);

        JLabel titleLabel = new JLabel("信息录入");
        titleLabel.setBounds(120, 10, 100, 40);
        add(titleLabel);


        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setBounds(20, 50, 50, 30);
        add(nameLabel);

        nameField = new JTextField(20);
        nameField.setBounds(60, 50, 80, 30);
        add(nameField);

        JLabel ageLabel = new JLabel("年龄:");
        ageLabel.setBounds(150, 50, 50, 30);
        add(ageLabel);

        ageCombo = new JComboBox();
        ageCombo.setBounds(200, 50, 50, 30);
        for (int i = 1; i <= 150; i++) {
            ageCombo.addItem(i);
        }
        add(ageCombo);

        JLabel genderLabel = new JLabel("性别:");
        genderLabel.setBounds(20, 90, 50, 30);
        add(genderLabel);

        manRadio = new JRadioButton("男");
        womanRadio = new JRadioButton("女");
        manRadio.setBounds(60, 90, 50, 30);
        womanRadio.setBounds(120, 90, 50, 30);
        add(manRadio);
        add(womanRadio);

        JLabel contentLabel = new JLabel("内容区");
        contentLabel.setBounds(20, 120, 40, 30);
        add(contentLabel);

        contentArea = new JTextArea();
        contentArea.setBounds(40, 160, 220, 100);
        add(contentArea);

        JButton confirmButton = new JButton("录入");
        confirmButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                int age = (int) ageCombo.getSelectedItem();
                String gender = "";
                if (manRadio.isSelected()) {
                    gender = "男";
                }
                if (womanRadio.isSelected()) {
                    gender = "女";
                }
                contentArea.setText("姓名: " + name + "\n年龄: " + age + "\n性别: " + gender);
            }
        });
        confirmButton.setBounds(80, 280, 60, 30);
        add(confirmButton);

        JButton resetButton = new JButton("重置");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                contentArea.setText("");
            }
        });
        resetButton.setBounds(180, 280, 60, 30);
        add(resetButton);
    }

    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
        myFrame.setVisible(true);
    }
}