corejava第10版
This commit is contained in:
1 parent
2021c3c227
commit
6165543dd3
1088 files changed
+94411
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package comboBox;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Font;
|
||||
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
/**
|
||||
* A frame with a sample text label and a combo box for selecting font faces.
|
||||
*/
|
||||
public class ComboBoxFrame extends JFrame
|
||||
{
|
||||
private JComboBox<String> faceCombo;
|
||||
private JLabel label;
|
||||
private static final int DEFAULT_SIZE = 24;
|
||||
|
||||
public ComboBoxFrame()
|
||||
{
|
||||
// add the sample text label
|
||||
|
||||
label = new JLabel("The quick brown fox jumps over the lazy dog.");
|
||||
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
|
||||
add(label, BorderLayout.CENTER);
|
||||
|
||||
// make a combo box and add face names
|
||||
|
||||
faceCombo = new JComboBox<>();
|
||||
faceCombo.addItem("Serif");
|
||||
faceCombo.addItem("SansSerif");
|
||||
faceCombo.addItem("Monospaced");
|
||||
faceCombo.addItem("Dialog");
|
||||
faceCombo.addItem("DialogInput");
|
||||
|
||||
// the combo box listener changes the label font to the selected face name
|
||||
|
||||
faceCombo.addActionListener(event ->
|
||||
label.setFont(
|
||||
new Font(faceCombo.getItemAt(faceCombo.getSelectedIndex()),
|
||||
Font.PLAIN, DEFAULT_SIZE)));
|
||||
|
||||
// add combo box to a panel at the frame's southern border
|
||||
|
||||
JPanel comboPanel = new JPanel();
|
||||
comboPanel.add(faceCombo);
|
||||
add(comboPanel, BorderLayout.SOUTH);
|
||||
pack();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user