java的swing中,我想在JComboBox下拉框中添加文本内容,该如何去实现?

java的swing中,我想在JComboBox下拉框中添加文本内容,该如何去实现?,第1张

import java.awt.*

import java.awt.event.*

import javax.swing.*

/*

* CustomComboBoxDemo.java 你要有下列文件

* images/Bird.gif

* images/Cat.gif

* images/Dog.gif

* images/Rabbit.gif

* images/Pig.gif

*/

public class CustomComboBoxDemo extends JPanel {

ImageIcon[] images

String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"}

/*

* Despite its use of EmptyBorder, this panel makes a fine content

* pane because the empty border just increases the panel's size

* and is "painted" on top of the panel's normal background. In

* other words, the JPanel fills its entire background if it's

* opaque (which it is by default)adding a border doesn't change

* that.

*/

public CustomComboBoxDemo() {

super(new BorderLayout())

//Load the pet images and create an array of indexes.

images = new ImageIcon[petStrings.length]

Integer[] intArray = new Integer[petStrings.length]

for (int i = 0i <petStrings.lengthi++) {

intArray[i] = new Integer(i)

images[i] = createImageIcon("images/" + petStrings[i] + ".gif")

if (images[i] != null) {

images[i].setDescription(petStrings[i])

}

}

//Create the combo box.

JComboBox petList = new JComboBox(intArray)

ComboBoxRenderer renderer= new ComboBoxRenderer()

renderer.setPreferredSize(new Dimension(200, 130))

petList.setRenderer(renderer)

petList.setMaximumRowCount(3)

//Lay out the demo.

add(petList, BorderLayout.PAGE_START)

setBorder(BorderFactory.createEmptyBorder(20,20,20,20))

}

/** Returns an ImageIcon, or null if the path was invalid. */

protected static ImageIcon createImageIcon(String path) {

java.net.URL imgURL = CustomComboBoxDemo.class.getResource(path)

if (imgURL != null) {

return new ImageIcon(imgURL)

} else {

System.err.println("Couldn't find file: " + path)

return null

}

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("CustomComboBoxDemo")

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

//Create and set up the content pane.

JComponent newContentPane = new CustomComboBoxDemo()

newContentPane.setOpaque(true)//content panes must be opaque

frame.setContentPane(newContentPane)

//Display the window.

frame.pack()

frame.setVisible(true)

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI()

}

})

}

class ComboBoxRenderer extends JLabel

implements ListCellRenderer {

private Font uhOhFont

public ComboBoxRenderer() {

setOpaque(true)

setHorizontalAlignment(CENTER)

setVerticalAlignment(CENTER)

}

/*

* This method finds the image and text corresponding

* to the selected value and returns the label, set up

* to display the text and image.

*/

public Component getListCellRendererComponent(

JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus) {

//Get the selected index. (The index param isn't

//always valid, so just use the value.)

int selectedIndex = ((Integer)value).intValue()

if (isSelected) {

setBackground(list.getSelectionBackground())

setForeground(list.getSelectionForeground())

} else {

setBackground(list.getBackground())

setForeground(list.getForeground())

}

//Set the icon and text. If icon was null, say so.

ImageIcon icon = images[selectedIndex]

String pet = petStrings[selectedIndex]

setIcon(icon)

if (icon != null) {

setText(pet)

setFont(list.getFont())

} else {

setUhOhText(pet + " (no image available)",

list.getFont())

}

return this

}

//Set the font and text when no image was found.

protected void setUhOhText(String uhOhText, Font normalFont) {

if (uhOhFont == null) { //lazily create this font

uhOhFont = normalFont.deriveFont(Font.ITALIC)

}

setFont(uhOhFont)

setText(uhOhText)

}

}

}

1、单选钮组件

在Swing中使用JRadioButton类来创建单选钮。ButtonGroup类是按钮组,可以使用ButtonGroup类的add()方法 将一组单选钮添加到按钮组中。

setSelected(boolean b)方法:设置单选钮为选中状态。

isSelected()方法:判断单选钮是否选中。

示例:创建单选钮,并设置默认选项

// 创建单选钮

JRadioButton rdoMan = new JRadioButton("男")

JRadioButton rdoWoman = new JRadioButton("女")

// 创建按钮组

ButtonGroup group = new ButtonGroup()

group.add(rdoMan)

group.add(rdoWoman)

// 设置默认选择

rdoMan.setSelected(true)

2、下拉框组件

在Swing中使用JComboBox类创建下拉框对象。

addItem(E item)方法:用于添加下拉框选项。

getSelectedItem()方法:用于获取被选中的下拉框选项。

示例:创建下拉框,并添加下拉框选项。

// 创建下拉框

JComboBox comboBox = new JComboBox()

// 绑定下拉框选项

String[] strArray = { "学生", "军人", "工人" }

for (String item : strArray)

{

comboBox.addItem(item)

}

3、复选框

在Swing中使用JCheckBox类创建复选框对象。

isSelected()方法:判断复选框是否被选中。

示例:创建复选框。

JCheckBox ckbA = new JCheckBox("足球")

JCheckBox ckbB = new JCheckBox("篮球")

JCheckBox ckbC = new JCheckBox("羽毛球")

4、综合示例

示例:单选钮、下拉框、复选框的使用,如下图:

完整代码:

import java.awt.EventQueue

import java.awt.Font

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.ButtonGroup

import javax.swing.JButton

import javax.swing.JCheckBox

import javax.swing.JComboBox

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JRadioButton

/**

* Swing中单选钮、下拉框、复选框的使用

* @author pan_junbiao

*

*/

public class TestFrame

{

private JFrame frame

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable() {

public void run()

{

try

{

TestFrame window = new TestFrame()

window.frame.setVisible(true)

} catch (Exception e)

{

e.printStackTrace()

}

}

})

}

public TestFrame()

{

initialize()

}

private void initialize()

{

frame = new JFrame()

frame.setBounds(100, 100, 270, 192)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.getContentPane().setLayout(null)

frame.setLocationRelativeTo(null)// 窗体居中

JLabel label = new JLabel("类型:")

label.setFont(new Font("宋体", Font.PLAIN, 14))

label.setBounds(10, 24, 54, 15)

frame.getContentPane().add(label)

JLabel lblNewLabel = new JLabel("性别:")

lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14))

lblNewLabel.setBounds(10, 53, 44, 15)

frame.getContentPane().add(lblNewLabel)

JLabel label_1 = new JLabel("爱好:")

label_1.setFont(new Font("宋体", Font.PLAIN, 14))

label_1.setBounds(10, 78, 44, 15)

frame.getContentPane().add(label_1)

// 创建下拉框

JComboBox comboBox = new JComboBox()

// 绑定下拉框选项

String[] strArray = { "学生", "军人", "工人" }

for (String item : strArray)

{

comboBox.addItem(item)

}

comboBox.setFont(new Font("宋体", Font.PLAIN, 14))

comboBox.setBounds(53, 20, 140, 23)

frame.getContentPane().add(comboBox)

// 创建单选钮

JRadioButton rdoMan = new JRadioButton("男")

JRadioButton rdoWoman = new JRadioButton("女")

// 创建按钮组

ButtonGroup group = new ButtonGroup()

group.add(rdoMan)

group.add(rdoWoman)

// 设置默认选择

rdoMan.setSelected(true)

rdoMan.setFont(new Font("宋体", Font.PLAIN, 14))

rdoMan.setBounds(48, 49, 54, 23)

frame.getContentPane().add(rdoMan)

rdoWoman.setFont(new Font("宋体", Font.PLAIN, 14))

rdoWoman.setBounds(104, 49, 54, 23)

frame.getContentPane().add(rdoWoman)

JCheckBox ckbA = new JCheckBox("足球")

ckbA.setFont(new Font("宋体", Font.PLAIN, 14))

ckbA.setBounds(48, 74, 54, 23)

frame.getContentPane().add(ckbA)

JCheckBox ckbB = new JCheckBox("篮球")

ckbB.setFont(new Font("宋体", Font.PLAIN, 14))

ckbB.setBounds(104, 74, 54, 23)

frame.getContentPane().add(ckbB)

JCheckBox ckbC = new JCheckBox("羽毛球")

ckbC.setFont(new Font("宋体", Font.PLAIN, 14))

ckbC.setBounds(163, 74, 71, 23)

frame.getContentPane().add(ckbC)

JButton btnSubmit = new JButton("确定")

btnSubmit.setFont(new Font("宋体", Font.PLAIN, 14))

btnSubmit.setBounds(10, 115, 93, 23)

frame.getContentPane().add(btnSubmit)

JButton btnReset = new JButton("重置")

btnReset.setFont(new Font("宋体", Font.PLAIN, 14))

btnReset.setBounds(141, 115, 93, 23)

frame.getContentPane().add(btnReset)

// 重置按钮事件

btnSubmit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

String msg = ""

// 获取下拉框选择

String typeStr = comboBox.getSelectedItem().toString()

msg = "类型:" + typeStr

JOptionPane.showMessageDialog(null, msg, "提示", JOptionPane.INFORMATION_MESSAGE)

// 获取单选钮选项

String sexStr = rdoMan.isSelected() ? "男" : "女"

msg = "性别:" + sexStr

JOptionPane.showMessageDialog(null, msg, "提示", JOptionPane.INFORMATION_MESSAGE)

// 获取复选框选项

msg = "爱好:"

if (ckbA.isSelected())

{

msg += ckbA.getText() + ""

}

if (ckbB.isSelected())

{

msg += ckbB.getText() + ""

}

if (ckbC.isSelected())

{

msg += ckbC.getText() + ""

}

JOptionPane.showMessageDialog(null, msg, "提示", JOptionPane.INFORMATION_MESSAGE)

}

})

// 重置按钮事件

btnReset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

comboBox.setSelectedIndex(0)

rdoMan.setSelected(true)

ckbA.setSelected(false)

ckbB.setSelected(false)

ckbC.setSelected(false)

}

})

}

}

文章知识点与官方知识档案匹配

Java技能树首页概览

88264 人正在系统学习中

打开CSDN,阅读体验更佳

Swing-下拉框、列表框_Jchoi_Tang的博客_swing 下拉框

1、下拉框 选择地区,或者一些单个选项 package com.gui.lesson06import javafx.scene.control.ComboBoximport javax.swing.*import java.awt.*public class TestComboBoxDemo01 extends JFrame { public TestComboBoxDemo01(...

java swing 下拉复选框_Java Swing JCheckBox复选框的实现方法

1、首先在javaswingpanel内双击打开MyEclipse开发软件。

2、其次新建Java类SelectFrame,继承JFrame。

3、最后在添加的下拉框内设置只有特定用户才可以查看,这样就完成了设置情况。


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

原文地址: https://outofmemory.cn/bake/11896679.html

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

发表评论

登录后才能评论

评论列表(0条)

保存