Java 如何将组合框的选定项设置为其他值?
我当前正在尝试将组合框中的选定项设置为其他值。在本例中,我使用的是鞋码,我想选择鞋码,在选择鞋码并点击开始按钮后,我想让它读取尺码代码 这就是我努力实现的目标: 这就是我所拥有的:Java 如何将组合框的选定项设置为其他值?,java,swing,Java,Swing,我当前正在尝试将组合框中的选定项设置为其他值。在本例中,我使用的是鞋码,我想选择鞋码,在选择鞋码并点击开始按钮后,我想让它读取尺码代码 这就是我努力实现的目标: 这就是我所拥有的: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ComboBoxDemo extends JFrame implements ActionListener{ //declarations JBut
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBoxDemo extends JFrame implements ActionListener{
//declarations
JButton btnPay = new JButton("Start");
/*ShoeSizeArray
String[] comboLabels = {"5", "5.5","6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5",
"12.5", "13.5", "14", "14.5", "15.5", "16"};*/
//Sizecode Array
String[] comboLabels = {"560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", "690", "700", "710",
"720", "730", "740", "750", "760", "770"};
JComboBox <String> combo = new JComboBox<String>(comboLabels);
JTextArea display = new JTextArea(5,20);
//constructor
public ComboBoxDemo(){
super("Combo box");
//panel for button and combobox
JPanel buttonPanel = new JPanel();
//add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
//add button and combobox to panel
buttonPanel.add(btnPay);
buttonPanel.add(combo);
//register button with ActionListener
btnPay.addActionListener(this);
//add text area to center of frame
add(display, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
String sizecode = (String)combo.getSelectedItem();
display.append("\nYou selected the sizecode " + sizecode);
}
public static void main(String[] args){
JFrame fr = new ComboBoxDemo();
fr.setLocationRelativeTo(null);
fr.setSize(200,200);
fr.setVisible(true);
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类ComboBoxDemo扩展JFrame实现ActionListener{
//声明
JButton btnPay=新JButton(“开始”);
/*鞋底阵列
字符串[]组合标签={“5”、“5.5”、“6”、“6.5”、“7”、“7.5”、“8”、“8.5”、“9”、“9.5”、“10”、“10.5”、“11”、“11.5”,
"12.5", "13.5", "14", "14.5", "15.5", "16"};*/
//尺寸码阵列
字符串[]组合标签={“560”、“570”、“580”、“590”、“600”、“610”、“620”、“630”、“640”、“650”、“660”、“670”、“680”、“690”、“700”、“710”,
"720", "730", "740", "750", "760", "770"};
JComboBox组合=新的JComboBox(组合标签);
JTextArea显示=新的JTextArea(5,20);
//建造师
公共ComboxDemo(){
超级(“组合框”);
//按钮和组合框的面板
JPanel buttonPanel=新的JPanel();
//将面板添加到框架
添加(按钮面板,边界布局。南部);
//将按钮和组合框添加到面板
按钮面板添加(btnPay);
按钮面板添加(组合);
//使用ActionListener注册按钮
btnPay.addActionListener(此);
//将文本区域添加到框架的中心
添加(显示、边框布局、中心);
}
已执行的公共无效操作(操作事件e){
字符串大小代码=(字符串)组合。getSelectedItem();
display.append(“\n您选择了sizecode”+sizecode);
}
公共静态void main(字符串[]args){
JFrame fr=新ComboBoxDemo();
fr.setLocationRelativeTo(空);
fr.setSize(200200);
fr.setVisible(真);
}
}
创建一个Shoesize类并根据选择设置标签
下面的代码应该对您有所帮助
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ComboBoxDemo extends JFrame implements ActionListener {
// declarations
JButton btnPay = new JButton("Start");
// ShoeSizeArray
String[] sizes = { "5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", "12.5",
"13.5", "14", "14.5", "15.5", "16" };
// Sizecode Array
String[] comboLabels = { "560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680",
"690", "700", "710", "720", "730", "740", "750", "760", "770" };
JComboBox<Shoesize> combo = new JComboBox<Shoesize>();
JTextArea display = new JTextArea(5, 20);
// constructor
public ComboBoxDemo() {
super("Combo box");
// panel for button and combobox
JPanel buttonPanel = new JPanel();
// add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
for (int i = 0; i < sizes.length; i++) {
Shoesize size = new Shoesize(Double.parseDouble(sizes[i]), comboLabels[i]);
combo.addItem(size);
}
// add button and combobox to panel
buttonPanel.add(btnPay);
buttonPanel.add(combo);
// register button with ActionListener
btnPay.addActionListener(this);
// add text area to center of frame
add(display, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
Shoesize sizecode = (Shoesize) combo.getSelectedItem();
display.append("\nYou selected the sizecode " + sizecode.label);
}
public static void main(String[] args) {
JFrame fr = new ComboBoxDemo();
fr.setLocationRelativeTo(null);
fr.setSize(200, 200);
fr.setVisible(true);
}
class Shoesize {
double size;
String label;
public Shoesize(double size, String label) {
super();
this.size = size;
this.label = label;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
long temp;
temp = Double.doubleToLongBits(size);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Shoesize other = (Shoesize) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (Double.doubleToLongBits(size) != Double.doubleToLongBits(other.size))
return false;
return true;
}
private ComboBoxDemo getOuterType() {
return ComboBoxDemo.this;
}
@Override
public String toString() {
return String.valueOf(size);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
公共类ComboBoxDemo扩展JFrame实现ActionListener{
//声明
JButton btnPay=新JButton(“开始”);
//鞋底阵列
字符串[]大小={“5”、“5.5”、“6”、“6.5”、“7”、“7.5”、“8”、“8.5”、“9”、“9.5”、“10”、“10.5”、“11”、“11.5”、“12.5”,
"13.5", "14", "14.5", "15.5", "16" };
//尺寸码阵列
字符串[]组合标签={“560”、“570”、“580”、“590”、“600”、“610”、“620”、“630”、“640”、“650”、“660”、“670”、“680”,
"690", "700", "710", "720", "730", "740", "750", "760", "770" };
JComboBox组合=新的JComboBox();
JTextArea显示=新的JTextArea(5,20);
//建造师
公共ComboxDemo(){
超级(“组合框”);
//按钮和组合框的面板
JPanel buttonPanel=新的JPanel();
//将面板添加到框架
添加(按钮面板,边界布局。南部);
对于(int i=0;i>>32));
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
Shoesize-other=(Shoesize)obj;
如果(!getOuterType().equals(other.getOuterType()))
返回false;
if(Double.doubleToLongBits(大小)!=Double.doubleToLongBits(其他大小))
返回false;
返回true;
}
私有ComboBoxDemo getOuterType(){
返回ComboBoxDemo.this;
}
@凌驾
公共字符串toString(){
返回字符串.valueOf(大小);
}
}
}
创建一个Shoesize类并根据选择设置标签
下面的代码应该对您有所帮助
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ComboBoxDemo extends JFrame implements ActionListener {
// declarations
JButton btnPay = new JButton("Start");
// ShoeSizeArray
String[] sizes = { "5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", "12.5",
"13.5", "14", "14.5", "15.5", "16" };
// Sizecode Array
String[] comboLabels = { "560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680",
"690", "700", "710", "720", "730", "740", "750", "760", "770" };
JComboBox<Shoesize> combo = new JComboBox<Shoesize>();
JTextArea display = new JTextArea(5, 20);
// constructor
public ComboBoxDemo() {
super("Combo box");
// panel for button and combobox
JPanel buttonPanel = new JPanel();
// add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
for (int i = 0; i < sizes.length; i++) {
Shoesize size = new Shoesize(Double.parseDouble(sizes[i]), comboLabels[i]);
combo.addItem(size);
}
// add button and combobox to panel
buttonPanel.add(btnPay);
buttonPanel.add(combo);
// register button with ActionListener
btnPay.addActionListener(this);
// add text area to center of frame
add(display, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
Shoesize sizecode = (Shoesize) combo.getSelectedItem();
display.append("\nYou selected the sizecode " + sizecode.label);
}
public static void main(String[] args) {
JFrame fr = new ComboBoxDemo();
fr.setLocationRelativeTo(null);
fr.setSize(200, 200);
fr.setVisible(true);
}
class Shoesize {
double size;
String label;
public Shoesize(double size, String label) {
super();
this.size = size;
this.label = label;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
long temp;
temp = Double.doubleToLongBits(size);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Shoesize other = (Shoesize) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (Double.doubleToLongBits(size) != Double.doubleToLongBits(other.size))
return false;
return true;
}
private ComboBoxDemo getOuterType() {
return ComboBoxDemo.this;
}
@Override
public String toString() {
return String.valueOf(size);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
公共类ComboBoxDemo扩展JFrame实现ActionListener{
//声明
JButton btnPay=新JButton(“开始”);
//鞋底阵列
字符串[]大小={“5”、“5.5”、“6”、“6.5”、“7”、“7.5”、“8”、“8.5”、“9”、“9.5”、“10”、“10.5”、“11”、“11.5”、“12.5”,
"13.5", "14", "14.5", "15.5", "16" };
//尺寸码阵列
字符串[]组合
class FooRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Foo)
{
Foo foo = (Foo)value;
setText( foo.getSize() );
}
return this;
}
}