分享
 
 
 

LunarPhases

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

本文提到了如下内容:

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();

}

});

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有