编写一个JFrame窗口,要求如下: 1. 在窗口的NORTH区放置一个JPanel面板。 2. JPanel面板放置如下组件: (1) JLable标签,标签文本为“兴趣”,右边接着是三个JChec

package 窗口;


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


public class Window extends JFrame implements ActionListener  {
JLabel lb1,lb2;
JCheckBox cb1,cb2,cb3;
JRadioButton rb1,rb2;
JTextArea area=new JTextArea(12,12);
ButtonGroup group;
JScrollPane sp;
Box box1,box2;
Window(){
super("JFrame");
Container contentPane=getContentPane();
JFrame frame=new JFrame();
    JPanel p1=new JPanel();    
    GridLayout grid=new GridLayout(2,1);
    p1.setLayout(grid);
    box1=Box.createHorizontalBox();
    box2=Box.createHorizontalBox();
    lb1=new JLabel("兴趣");
    cb1=new JCheckBox("羽毛球");
    cb2=new JCheckBox("乒乓球");
    cb3=new JCheckBox("唱歌");
    
    lb2=new JLabel("性别");
    group=new ButtonGroup();
    rb1=new JRadioButton("男");
    rb2=new JRadioButton("女");
    group.add(rb1);
    group.add(rb2);
    
    box1.add(lb1);
    box1.add(cb1);
    box1.add(cb2);
    box1.add(cb3);
    
    
    box2.add(lb2);
    box2.add(rb1);
    box2.add(rb2);
    
    p1.add(box1);
    p1.add(box2);
    contentPane.add(p1,BorderLayout.NORTH);
    
    sp=new JScrollPane(area);
    JTextArea area=new JTextArea();  
    sp.add(area);
    contentPane.add(sp,BorderLayout.CENTER);
    validate();
    setSize(300,400);
    setVisible(true); 
    
    rb1.addActionListener(this);
    rb2.addActionListener(this);
    cb1.addActionListener(this);
    cb2.addActionListener(this);
    cb3.addActionListener(this);


}
public void actionPerformed (ActionEvent e){
if(e.getSource()==cb1){
area.append("羽毛球"+"\n");
}
if(e.getSource()==cb2){
area.append("乒乓球"+"\n");
}
if(e.getSource()==cb3){
area.append("唱歌"+"\n");
}
if(e.getSource()==rb1){
area.append("男"+"\n");
}
if(e.getSource()==rb2){
area.append("女"+"\n");
}

}
public static void main(String[] args) {
        // TODO Auto-generated method stub
        Window win=new Window();
        win.setBounds(100,100,500,300);
        win.setVisible(true);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    }


}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的回答有一些错误。以下是修复了您提到的问题的代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CustomWindow extends JFrame { private JPanel titleBar; private JButton minimizeButton; private JButton maximizeButton; private JButton closeButton; private int xx, xy; public CustomWindow() { // 设置窗口背景色 getContentPane().setBackground(new Color(204, 204, 204)); // 去掉窗口边框默认样式 setUndecorated(true); // 固定窗口大小 setSize(1100, 700); // 窗口显示 setLocationRelativeTo(null); // 添加标题栏 titleBar = new JPanel(); titleBar.setBackground(Color.WHITE); titleBar.setLayout(null); // 使用自定义布局 // 创建最小化按钮 minimizeButton = new JButton("-"); minimizeButton.setBackground(Color.WHITE); minimizeButton.setFocusPainted(false); minimizeButton.setBounds(1000, 0, 50, 30); // 设置按钮的位置和大小 // 创建最大化按钮 maximizeButton = new JButton("口"); maximizeButton.setBackground(Color.WHITE); maximizeButton.setFocusPainted(false); maximizeButton.setBounds(1050, 0, 50, 30); // 创建关闭按钮 closeButton = new JButton("×"); closeButton.setBackground(Color.WHITE); closeButton.setFocusPainted(false); closeButton.setBounds(1100, 0, 50, 30); // 添加鼠标移动事件监听器,实现窗口拖动效果 titleBar.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { xx = e.getX(); xy = e.getY(); } }); titleBar.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getXOnScreen(); int y = e.getYOnScreen(); setLocation(x - xx, y - xy); } }); // 添加按钮到标题栏 titleBar.add(minimizeButton); titleBar.add(maximizeButton); titleBar.add(closeButton); // 添加标题栏到窗口 add(titleBar, BorderLayout.NORTH); // 设置关闭窗口时退出进程 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class ButtonMouseListener extends MouseAdapter { @Override public void mouseEntered(MouseEvent e) { JButton button = (JButton) e.getSource(); button.setBackground(Color.LIGHT_GRAY); } @Override public void mouseExited(MouseEvent e) { JButton button = (JButton) e.getSource(); button.setBackground(Color.WHITE); } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { CustomWindow window = new CustomWindow(); window.setVisible(true); }); } } ``` 请将以上代码保存在一个名为`CustomWindow.java`的文件,并重新编译运行。现在,您应该能够通过鼠标拖动标题栏来移动窗口了。按钮也应该正确地显示在标题栏的最右边。再次对之前的错误表示歉意,感谢您的指正!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值