📄 listdialogrunner.java
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;/** * A 1.4 application that brings up a ListDialog. */public class ListDialogRunner { static JFrame frame; static String[] names = {"Arlo", "Cosmo", "Elmo", "Hugo", "Jethro", "Laszlo", "Milo", "Nemo", "Otto", "Ringo", "Rocco", "Rollo"}; public static void main(String[] args) { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); //Create and set up the window. frame = new JFrame("Name That Baby"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(createUI()); //Display the window. frame.pack(); frame.setVisible(true); } public static JPanel createUI() { //Create the labels. JLabel intro = new JLabel("The chosen name:"); final JLabel name = new JLabel("Cosmo"); intro.setLabelFor(name); //Use a wacky font if it exists. If not, this falls //back to the default font ("dialog") in 36 point, plain. name.setFont(new Font("Space Toaster", Font.PLAIN, 36)); //Create the button. final JButton button = new JButton("Pick a new name..."); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selectedName = ListDialog.showDialog( frame, button, "Baby names ending in O:", "Name Chooser", names, name.getText()); name.setText(selectedName); } }); //Create the content pane and set up the layout. JPanel contentPane = new JPanel(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.setBorder(BorderFactory.createEmptyBorder(20,20,10,20)); intro.setAlignmentX(JComponent.CENTER_ALIGNMENT); name.setAlignmentX(JComponent.CENTER_ALIGNMENT); button.setAlignmentX(JComponent.CENTER_ALIGNMENT); //Add the labels to the content pane. contentPane.add(intro); contentPane.add(name); //Add a vertical spacer that also guarantees us a minimum width: contentPane.add(Box.createRigidArea(new Dimension(150,10))); //Add the button. contentPane.add(button); return contentPane; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -