LunarPhases

类别:Java 点击:0 评论:0 推荐:

本文提到了如下内容:

Using Layout Managers
Compound Borders
Combo Boxes
Multiple Images

该程序的主框架如下:

//Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();

//Add various widgets to the sub panels.
addWidgets();

//Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

//Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);

还比较好理解。

常见的布局管理器有:BorderLayout, BoxLayout, FlowLayout, GridLayout, GridBagLayout和SpringLayout。

边框:

selectPanel.setBorder(BorderFactory.createCompoundBorder(
                      BorderFactory.createTitledBorder("Select Phase"),
                      BorderFactory.createEmptyBorder(5,5,5,5)));

下拉选框:

JComboBox phaseChoices = null;
...
//Create combo box with lunar phase choices.
String[] phases = { "New", "Waxing Crescent", "First Quarter",
                    "Waxing Gibbous", "Full", "Waning Gibbous",
                    "Third Quarter", "Waning Crescent" };
phaseChoices = new JComboBox(phases);
phaseChoices.setSelectedIndex(START_INDEX);

下拉选框的事件处理:

phaseChoices.addActionListener(this);
...
public void actionPerformed(ActionEvent event) {
    if ("comboBoxChanged".equals(event.getActionCommand())) {
        //Update the icon to display the new phase
        phaseIconLabel.setIcon(images[phaseChoices.getSelectedIndex()]);
    }
}

多图片操作:

final static int NUM_IMAGES = 8;
final static int START_INDEX = 3;

ImageIcon[] images = new ImageIcon[NUM_IMAGES];
...
//Get the images and put them into an array of ImageIcon.
for (int i = 0; i < NUM_IMAGES; i++) {
    String imageName = "images/image" + i + ".jpg";
    System.out.println("getting image: " + imageName);
    URL iconURL = LunarPhases.class.getResource(imageName);
    ImageIcon icon = new ImageIcon(iconURL);
    images[i] = icon;
}

importjava.awt.event.*;

import javax.swing.*;

 

public class LunarPhases implements ActionListener {

    final static int NUM_IMAGES = 8;

    final static int START_INDEX = 3;

 

    ImageIcon[] images = new ImageIcon[NUM_IMAGES];

    JPanel mainPanel, selectPanel, displayPanel;

 

    JComboBox phaseChoices = null;

    JLabel phaseIconLabel = null;

 

    public LunarPhases() {

       //创建状态选择面板和显示面板。

        selectPanel = new JPanel();

        displayPanel = new JPanel();

 

       //增加组件到子面板。

        addWidgets();

 

       //创建主面板来包含上面的两个子面板。

        mainPanel = new JPanel();

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

 

       //把两个子面板增加到主面板中。

        mainPanel.add(selectPanel);

        mainPanel.add(displayPanel);

    }

 

    /*

     * 获取图片并安装组件。

     */

    private void addWidgets() {

       //获取图片并把他们放入一个 ImageIcons 数组中。

       for (int i = 0; i < NUM_IMAGES; i++) {

          images[i] = createImageIcon("/images/image" + i + ".jpg");

       }

 

       /*

        * 创建一个用来显示图片的 label 并给他边框。

        */

        phaseIconLabel = new JLabel();

        phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);

        phaseIconLabel.setVerticalAlignment(JLabel.CENTER);

        phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);

        phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);

        phaseIconLabel.setBorder(

           BorderFactory.createCompoundBorder(

               BorderFactory.createLoweredBevelBorder(),

               BorderFactory.createEmptyBorder(5, 5, 5, 5)));

 

        phaseIconLabel.setBorder(

           BorderFactory.createCompoundBorder(

               BorderFactory.createEmptyBorder(0, 0, 10, 0),

               phaseIconLabel.getBorder()));

 

       //创建一个可以选择月亮状态的 combo box 。

        String[] phases =

           {

               "New",

               "Waxing Crescent",

               "First Quarter",

               "Waxing Gibbous",

               "Full",

               "Waning Gibbous",

               "Third Quarter",

               "Waning Crescent" };

        phaseChoices = new JComboBox(phases);

        phaseChoices.setSelectedIndex(START_INDEX);

 

       //显示第一副图片。

        phaseIconLabel.setIcon(images[START_INDEX]);

        phaseIconLabel.setText("");

 

       //给选择面板增加边框。

        selectPanel.setBorder(

           BorderFactory.createCompoundBorder(

               BorderFactory.createTitledBorder("Select Phase"),

               BorderFactory.createEmptyBorder(5, 5, 5, 5)));

 

       //给显示面板增加边框。

        displayPanel.setBorder(

           BorderFactory.createCompoundBorder(

               BorderFactory.createTitledBorder("Display Phase"),

               BorderFactory.createEmptyBorder(5, 5, 5, 5)));

 

       //增将 combo box 到选择面板以及图像 label 到显示面板。

        displayPanel.add(phaseIconLabel);

        selectPanel.add(phaseChoices);

 

       //给 combo box 增加事件。

        phaseChoices.addActionListener(this);

    }

 

    public void actionPerformed(ActionEvent event) {

       if ("comboBoxChanged".equals(event.getActionCommand())) {

           //更新图标以便显示新的状态。

           phaseIconLabel.setIcon(images[phaseChoices.getSelectedIndex()]);

       }

    }

 

    /** Returns an ImageIcon, or null if the path was invalid. */

    protected static ImageIcon createImageIcon(String path) {

        java.net.URL imageURL = LunarPhases.class.getResource(path);

 

       if (imageURL == null) {

           System.err.println("Resource not found: " + path);

           return null;

       } else {

           return new ImageIcon(imageURL);

       }

    }

 

    /**

     * 创建图形用户界面以便显示,为了线呈得安全,这里使用了 event-dispatching thread 调用

     */

    private static void createAndShowGUI() {

        //Make sure we have nice window decorations.

        JFrame.setDefaultLookAndFeelDecorated(true);

 

        //Create a new instance of LunarPhases.

        LunarPhases phases = new LunarPhases();

 

        //Create and set up the window.

        JFrame lunarPhasesFrame = new JFrame("Lunar Phases");

        lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        lunarPhasesFrame.setContentPane(phases.mainPanel);

 

        //Display the window.

        lunarPhasesFrame.pack();

        lunarPhasesFrame.setVisible(true);

    }

 

    public static void main(String[] args) {

        //Schedule a job for the event-dispatching thread:

        //creating and showing this application's GUI.

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

           public void run() {

               createAndShowGUI();

           }

       });

    }

}

本文地址:http://com.8s8s.com/it/it14922.htm