jdialog 怎么添加单选框buttongroup

jdialog 怎么添加单选框buttongroup,第1张

JDialog添加单选框, 和 JFrame,JPanel等容器 添加单选框是一样的

步骤:

创建单选按钮.

并把相关的单选按钮都添加到ButtonGroup里,

然后把单选按钮添加到JDialog里

效果图

参考代码

import java.awt.*

import java.awt.event.*

import java.util.Enumeration

import javax.swing.*

public class MyTest {// 测试类

public static void main(String[] args) {

JDialog jd = new JDialog()

jd.setTitle("兴趣选择")

jd.setModal(true)

JPanel jp = new JPanel()// 流式布局

JLabel jl = new JLabel("兴趣[单选]")

ButtonGroup bg = new ButtonGroup()

JRadioButton jrb1 = new JRadioButton("跑步")

jrb1.setSelected(true)//默认选择此选项

JRadioButton jrb2 = new JRadioButton("唱歌")

JRadioButton jrb3 = new JRadioButton("跳舞")

bg.add(jrb1)//把单选按钮都加入到ButtonGroup 才能实现单选的效果

bg.add(jrb2)

bg.add(jrb3)

JButton jb = new JButton("确定")

jb.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Enumeration<AbstractButton> btns= bg.getElements()

 while(btns.hasMoreElements()){

           JRadioButton jr = (JRadioButton) btns.nextElement()

           if(jr.isSelected()) {

           JOptionPane.showMessageDialog(null, "兴趣是:"+jr.getText())

           }

        }

}

})

jp.add(jl)

jp.add(jrb1)

jp.add(jrb2)

jp.add(jrb3)

jp.add(jb)

jd.add(jp)

jd.setLayout(new FlowLayout())

jd.setSize(320, 100)// 大小

jd.setLocationRelativeTo(null)// 居中

jd.setVisible(true)

jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)

}

}

通过: d出框, 顾名思义就是刚开始看不见, 触发某个事件后d出的一个框

所以 我们要让JPanel响应指定的事件然后d出 . 比如常见的事件, 鼠标点击JPanel后d出

d出框. 一般有两种方法实现

方法一:JOptionPane 创建1个简单的d出框.(代码量少, 效果简单)

方法二Dialog/JDialog 创建1个d出框.(代码量长,可以实现复杂的效果)

效果图

参考代码

import java.awt.*

import java.awt.event.*

import javax.swing.*

public class Demo extends JFrame {

JPanel jp

static final String title = "Message"

static final String content = "Eggs aren't supposed to be green."

public Demo() {

jp = new JPanel()

jp.setBackground(Color.PINK)

jp.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

//方法1

JOptionPane.showMessageDialog(null, content, title, JOptionPane.INFORMATION_MESSAGE)

//方法2

new MyDailog(title, content).setVisible(true)// 创建1个对话框,并且设置为可见

}

})

add(jp)

setTitle("测试Demo")// 标题

setSize(280, 280)// 窗口大小

setLocationRelativeTo(null)// 窗口居中

setDefaultCloseOperation(EXIT_ON_CLOSE)// 窗口点击关闭时,退出程序

setVisible(true)// 窗口可见

}

public static void main(String[] args) {

new Demo()

}

}

// 对话框类

class MyDailog extends JDialog implements ActionListener {

String title

String content

public MyDailog(String title, String content) {

this.title = title

this.content = content

ImageIcon icon = new ImageIcon("tp.png")// 创建1个图标实例

JLabel jlImg = new JLabel(icon)// 1个图片标签,显示图片

JLabel jl = new JLabel(content)// 1个文字标签,显示文本

jl.setForeground(Color.BLUE)// 设置文字的颜色为蓝色

JButton jb = new JButton("确定")// 创建1个按钮

jb.addActionListener(this)// 给按钮添加响应事件

add(jlImg)// 向对话框加入图片标签

add(jl)// 向对话框加入文字标签

add(jb)// 向对话框添加按钮

setLayout(new FlowLayout())// 对话框流式布局

setIconImage(icon.getImage())// 窗口左上角的小图标

setTitle(title)// 设置标题

setModal(true)// 设置为模态窗口

setSize(275, 135)// 设置对话框大小

setLocationRelativeTo(null)// 对话框局域屏幕中央

setResizable(false)// 对话框不可缩放

setDefaultCloseOperation(DISPOSE_ON_CLOSE)// 当对话框窗口的关闭按钮[X]被点击时,销毁对话框

}

// 当确定按钮被点击时会执行下面的方法

@Override

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("确定")) {// 判断是不是确定按钮被点击

this.setVisible(false)// 对话框不可见

this.dispose()// 对话框销毁

}

}

}

//问题的关键在于,如何从jDialog中获取jFrame的引用。

//我是用内部类的形式解决该问题的。而且直接 *** 作jFrame的组件。

//还可以在新建自定义jDialog类时,传入jFrame的引用。

import java.awt.BorderLayout

import java.awt.GridLayout

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JDialog

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JTextArea

import javax.swing.JTextField

public class FrameTest extends JFrame {

    private JTextArea textArea

    public static void main(String[] args) {

        new FrameTest()

    }

    public FrameTest() {

        this.setSize(400, 300)

        this.setDefaultCloseOperation(EXIT_ON_CLOSE)

        initPanel()

        this.setVisible(true)

    }

    private void initPanel() {

        JButton button = new JButton("Add")

        textArea = new JTextArea()

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                openDialog()

            }

        })

        this.add(button, BorderLayout.NORTH)

        this.add(textArea, BorderLayout.CENTER)

    }

    private void openDialog() {

        final JDialog dialog = new JDialog(this, true)

        dialog.setSize(300, 200)

        dialog.setLocation(getX() + 50, getY() + 50)

        dialog.setLayout(new GridLayout(3, 2))

        final JTextField name = new JTextField(10)

        final JTextField phone = new JTextField(10)

        JButton save = new JButton("保存")

        JButton cancel = new JButton("取消")

        save.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                textArea.append(name.getText() + ":" + phone.getText() + "\n")

                dialog.dispose()

            }

        })

        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                dialog.dispose()

            }

        })

        dialog.add(new JLabel("姓名:"))

        dialog.add(name)

        dialog.add(new JLabel("电话:"))

        dialog.add(phone)

        dialog.add(save)

        dialog.add(cancel)

        dialog.setVisible(true)

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11237487.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存