下面列出了怎么用java.awt.Choice的API类实例代码及写法,或者点击链接到github查看源代码。
private static void test(final Point tmp) throws Exception {
Choice choice = new Choice();
for (int i = 1; i < 7; i++) {
choice.add("Long-long-long-long-long text in the item-" + i);
}
Frame frame = new Frame();
try {
frame.setAlwaysOnTop(true);
frame.setLayout(new FlowLayout());
frame.add(choice);
frame.pack();
frameWidth = frame.getWidth();
frame.setSize(frameWidth, SIZE);
frame.setVisible(true);
frame.setLocation(tmp.x, tmp.y);
openPopup(choice);
} finally {
frame.dispose();
}
}
private static void UI() {
Frame frame = new Frame("Test frame");
Choice choice = new Choice();
Stream.of(new String[]{"item 1", "item 2", "item 3"}).forEach(choice::add);
frame.add(choice);
frame.setBounds(100, 100, 400, 200);
frame.setVisible(true);
Font font = choice.getFont();
int size = font.getSize();
int height = choice.getBounds().height;
try {
if (height < size) {
throw new RuntimeException("Test failed");
}
} finally {
frame.dispose();
}
}
protected void addListeners(
final GenericDialog gd,
final Choice choice,
final Label label1,
final Label label2 )
{
choice.addItemListener( new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
update( spimData, choice, label1, label2 );
}
});
update( spimData, choice, label1, label2 );
}
private void replaceStrings(Choice choice, String[] strings, String defaultString)
{
choice.removeAll();
for (String str : strings)
{
choice.add(str);
}
choice.select(defaultString);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component) e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
public DitherControls(DitherTest app, int s, int e, DitherMethod type,
boolean vertical) {
applet = app;
setLayout(dcLayout);
add(new Label(vertical ? "Vertical" : "Horizontal"));
add(choice = new Choice());
for (DitherMethod m : DitherMethod.values()) {
choice.addItem(m.toString().substring(0, 1)
+ m.toString().substring(1).toLowerCase());
}
choice.select(type.ordinal());
add(start = new CardinalTextField(Integer.toString(s), 4));
add(end = new CardinalTextField(Integer.toString(e), 4));
}
@SuppressWarnings("LeakingThisInConstructor")
public DrawControls(DrawPanel target) {
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component) e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
public DitherControls(DitherTest app, int s, int e, DitherMethod type,
boolean vertical) {
applet = app;
setLayout(dcLayout);
add(new Label(vertical ? "Vertical" : "Horizontal"));
add(choice = new Choice());
for (DitherMethod m : DitherMethod.values()) {
choice.addItem(m.toString().substring(0, 1)
+ m.toString().substring(1).toLowerCase());
}
choice.select(type.ordinal());
add(start = new CardinalTextField(Integer.toString(s), 4));
add(end = new CardinalTextField(Integer.toString(e), 4));
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component) e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
@Override
public boolean dialogItemChanged(GenericDialog gd, AWTEvent evt)
{
if (gd.wasCanceled() || gd.wasOKed())
{
return true;
}
@SuppressWarnings({ "unchecked" })
Vector<Choice> choices = gd.getChoices();
if (choices == null)
{
IJ.log("empty choices array...");
return false;
}
// Change of the data table
if (evt.getSource() == choices.get(0))
{
String tableName = ((Choice) evt.getSource()).getSelectedItem();
Frame tableFrame = WindowManager.getFrame(tableName);
this.table = ((TextWindow) tableFrame).getTextPanel().getResultsTable();
// Choose current headings
String[] headings = this.table.getHeadings();
replaceStrings(choices.get(1), headings, chooseDefaultHeading(headings, xPosHeaderName));
replaceStrings(choices.get(2), headings, chooseDefaultHeading(headings, yPosHeaderName));
replaceStrings(choices.get(3), headings, chooseDefaultHeading(headings, valueHeaderName));
}
return true;
}
@SuppressWarnings("LeakingThisInConstructor")
public DrawControls(DrawPanel target) {
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component) e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
@Override
public void queryAdditionalParameters( final GenericDialog gd )
{
if ( Fusion.defaultInterpolation >= Fusion.interpolationTypes.length )
Fusion.defaultInterpolation = Fusion.interpolationTypes.length - 1;
if ( this.getFusionType() == WeightedAvgFusionType.FUSEDATA )
{
int maxViews = 0;
for ( final TimePoint t : timepointsToProcess )
for ( final Channel c : channelsToProcess )
maxViews = Math.max( maxViews, FusionHelper.assembleInputData( spimData, t, c, viewIdsToProcess ).size() );
// any choice but all views
final String[] views = new String[ maxViews ];
views[ 0 ] = "All";
for ( int i = 1; i < views.length; ++i )
views[ i ] = "" + i;
if ( defaultNumParalellViewsIndex < 0 || defaultNumParalellViewsIndex >= views.length )
defaultNumParalellViewsIndex = 0;
gd.addChoice( "Process_views_in_paralell", views, views[ defaultNumParalellViewsIndex ] );
this.sequentialViews = (Choice)gd.getChoices().lastElement();
}
if ( this.getFusionType() == WeightedAvgFusionType.FUSEDATA )
{
gd.addCheckbox( "Blend images smoothly", Fusion.defaultUseBlending );
gd.addCheckbox( "Content-based fusion", Fusion.defaultUseContentBased );
}
gd.addChoice( "Interpolation", Fusion.interpolationTypes, Fusion.interpolationTypes[ Fusion.defaultInterpolation ] );
}
/** @return Propertydescriptors */
@Override
protected PropertyDescriptor[] createPDs() throws IntrospectionException {
return new PropertyDescriptor[] {
new PropertyDescriptor("selectedObjects", Choice.class, "getSelectedObjects", null), // NOI18N
new PropertyDescriptor("selectedIndex", Choice.class, "getSelectedIndex", null), // NOI18N
new PropertyDescriptor("itemCount", Choice.class, "getItemCount", null), // NOI18N
new PropertyDescriptor("item", Choice.class, "getItem", null), // NOI18N
new PropertyDescriptor("selectedItem", Choice.class, "getSelectedItem", null), // NOI18N
};
}
private void updateComboBox(Choice certificateComboBox){
certificateComboBox.removeAll();
certificateComboBox.addItem("Loading Certificates...");
certificateComboBox.select(0);
ArrayList<CertificateData> certList = new ArrayList<CertificateData>();
try {
certList = signEngine.loadSmartCardCertificateList(readAllCertificates).certificateList;
} catch (Exception e) {
e.printStackTrace();
SignUtils.playBeeps(1);
JOptionPane.showMessageDialog(null, "ERROR LOADING CERTIFICATES:\n"+e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
certificateComboBox.removeAll();
certificateComboBox.addItem("--Select Certificate--");
for(int i=0;i<certList.size();i++)
certificateComboBox.addItem(certList.get(i).id);
if(certificateComboBox.getItemCount()==1){
certificateComboBox.removeAll();
certificateComboBox.addItem("--No Certificates Available!--");
SignUtils.playBeeps(2);
}
else{
if(certificateComboBox.getItemCount()==2){
certificateComboBox.remove(0);
}
SignUtils.playBeeps(1);
}
}
public DitherControls(DitherTest app, int s, int e, DitherMethod type,
boolean vertical) {
applet = app;
setLayout(dcLayout);
add(new Label(vertical ? "Vertical" : "Horizontal"));
add(choice = new Choice());
for (DitherMethod m : DitherMethod.values()) {
choice.addItem(m.toString().substring(0, 1)
+ m.toString().substring(1).toLowerCase());
}
choice.select(type.ordinal());
add(start = new CardinalTextField(Integer.toString(s), 4));
add(end = new CardinalTextField(Integer.toString(e), 4));
}
@SuppressWarnings("LeakingThisInConstructor")
public DrawControls(DrawPanel target) {
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component) e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
public DitherControls(DitherTest app, int s, int e, DitherMethod type,
boolean vertical) {
applet = app;
setLayout(dcLayout);
add(new Label(vertical ? "Vertical" : "Horizontal"));
add(choice = new Choice());
for (DitherMethod m : DitherMethod.values()) {
choice.addItem(m.toString().substring(0, 1)
+ m.toString().substring(1).toLowerCase());
}
choice.select(type.ordinal());
add(start = new CardinalTextField(Integer.toString(s), 4));
add(end = new CardinalTextField(Integer.toString(e), 4));
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component) e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
@SuppressWarnings("LeakingThisInConstructor")
public DrawControls(DrawPanel target) {
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
}
/**
* Returns information about component.
*/
@Override
public Hashtable<String, Object> getDump() {
Hashtable<String, Object> result = super.getDump();
if (((Choice) getSource()).getSelectedItem() != null) {
result.put(SELECTED_ITEM_DPROP, ((Choice) getSource()).getSelectedItem());
}
String[] items = new String[((Choice) getSource()).getItemCount()];
for (int i = 0; i < ((Choice) getSource()).getItemCount(); i++) {
items[i] = ((Choice) getSource()).getItem(i);
}
addToDump(result, ITEM_PREFIX_DPROP, items);
return result;
}
/**
* Maps {@code Choice.add(String)} through queue
*/
public void add(final String item) {
runMapping(new MapVoidAction("add") {
@Override
public void map() {
((Choice) getSource()).add(item);
}
});
}
/**
* Maps {@code Choice.addItemListener(ItemListener)} through queue
*/
public void addItemListener(final ItemListener itemListener) {
runMapping(new MapVoidAction("addItemListener") {
@Override
public void map() {
((Choice) getSource()).addItemListener(itemListener);
}
});
}
/**
* Maps {@code Choice.getItem(int)} through queue
*/
public String getItem(final int index) {
return (runMapping(new MapAction<String>("getItem") {
@Override
public String map() {
return ((Choice) getSource()).getItem(index);
}
}));
}
/**
* Maps {@code Choice.getItemCount()} through queue
*/
public int getItemCount() {
return (runMapping(new MapIntegerAction("getItemCount") {
@Override
public int map() {
return ((Choice) getSource()).getItemCount();
}
}));
}
/**
* Maps {@code Choice.getSelectedIndex()} through queue
*/
public int getSelectedIndex() {
return (runMapping(new MapIntegerAction("getSelectedIndex") {
@Override
public int map() {
return ((Choice) getSource()).getSelectedIndex();
}
}));
}
/**
* Maps {@code Choice.getSelectedItem()} through queue
*/
public String getSelectedItem() {
return (runMapping(new MapAction<String>("getSelectedItem") {
@Override
public String map() {
return ((Choice) getSource()).getSelectedItem();
}
}));
}
public boolean queryOptions() {
MesquiteInteger buttonPressed = new MesquiteInteger(1);
ExtensibleDialog dialog = new ExtensibleDialog(containerOfModule(), getName() + " Options",buttonPressed); //MesquiteTrunk.mesquiteTrunk.containerOfModule()
dialog.addLabel(getName() + " Options");
String helpString = "Please choose the options for consensus trees. ";
dialog.appendToHelpString(helpString);
queryOptionsSetup(dialog);
String[] rootingStrings = {"As specified in first tree", "Rooted", "Unrooted"};
Choice rootingChoice = dialog.addPopUpMenu("Treat trees as rooted or unrooted:", rootingStrings, 0);
//TextArea PAUPOptionsField = queryFilesDialog.addTextArea(PAUPOptions, 20);
dialog.completeAndShowDialog(true);
if (buttonPressed.getValue()==0) {
queryOptionsProcess(dialog);
int choiceValue = rootingChoice.getSelectedIndex();
if (choiceValue>=0)
rooting = choiceValue;
storePreferences();
}
dialog.dispose();
return (buttonPressed.getValue()==0);
}