📄 listsandcomboboxes.java
字号:
package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** An example class used to demonstrate the use of
* the JList and JComboBox components
*/
public class ListsAndComboBoxes extends JFrame {
private JComboBox crustBox;
private JList toppingList;
/** Class constructor method
* @param titleText Window's title bar text
*/
public ListsAndComboBoxes( String titleText ) {
super( titleText );
addWindowListener( new WindowCloser() );
crustBox = new JComboBox( new Object[] {
"thick and chewy",
"thin and crispy",
"Chicago deep dish"
}
);
toppingList = new JList( new Object[] {
"pepperoni",
"sausage",
"ham",
"grilled chicken",
"mushrooms",
"green peppers",
"hot peppers",
"black olives",
"tomato slices",
"sun-dried tomatoes",
"extra cheese",
"pineapple",
"anchovies"
}
);
toppingList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
JScrollPane scrollToppingList
= new JScrollPane( toppingList );
JPanel lists = new JPanel( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets( 10, 10, 10, 10 );
c.weightx = 0.0;
c.weighty = 0.0;
lists.add( new JLabel( "Toppings:" ), c );
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.weighty = 1.0;
lists.add( scrollToppingList, c );
c.gridwidth = 1;
c.weightx = 0.0;
c.weighty = 0.0;
lists.add( new JLabel( "Crust type:" ), c );
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.weighty = 1.0;
lists.add( crustBox, c );
JPanel buttons = new JPanel();
JButton order = new JButton( "Place order",
new ImageIcon( "pizza.gif" ) );
order.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
confirmOrder();
}
}
);
buttons.add( order );
Container cp = getContentPane();
cp.add( lists, BorderLayout.CENTER );
cp.add( buttons, BorderLayout.SOUTH );
setSize( 400, 350 );
setVisible( true );
}
/** Confirm a customer's order
*/
public void confirmOrder() {
StringBuffer question = new StringBuffer();
question.append( "Order a " +
crustBox.getSelectedItem() +
" crust pizza topped with" );
Object[] toppings = toppingList.getSelectedValues();
for ( int i=0; i < toppings.length; i++ ) {
question.append( " " + toppings[i] );
question.append ( ( i + 1 < toppings.length)
? " and" : "?" );
}
JOptionPane.showConfirmDialog( this,
question );
}
/** The test method for the class
* @param args not used
*/
public static void main( String[] args ) {
new ListsAndComboBoxes(
"Example Lists and Combo Boxes" );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -