java combobox大小_每天一点儿Java--ComboBox

该博客演示了一个Java Swing应用,使用ComboBox来选择日期格式,并实时显示当前日期。用户可以从预定义的日期格式列表中选择,然后应用所选格式格式化当前日期。代码中实现了ComboBox监听器,以便在选择改变时更新日期显示。
摘要由CSDN通过智能技术生成

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import java.util.*;

import java.text.SimpleDateFormat;

/**

*

Title: ComboBox下拉域演示

*

Description: 通过选择或这输入一种日期格式来格式化今天的日期

*

Copyright: Copyright (c) 2014

*

Filename: ComboBoxDemo.java

* @author 王海涛

* @version 0.1

*/

public class ComboBoxDemo extends JPanel

implements ActionListener {

static JFrame frame;

JLabel result;

String currentPattern;

/**

*
方法说明:构造器。

初始化窗口构件

*
输入參数:

*
返回类型:

*/

public ComboBoxDemo() {

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

String[] patternExamples = {

"dd MMMMM yyyy",

"dd.MM.yy",

"MM/dd/yy",

"yyyy.MM.dd G ‘at‘ hh:mm:ss z",

"EEE, MMM d, ‘‘yy",

"h:mm a",

"H:mm:ss:SSS",

"K:mm a,z",

"yyyy.MMMMM.dd GGG hh:mm aaa"

};

currentPattern = patternExamples[0];

//设置一个规范的用户界面

JLabel patternLabel2 = new JLabel("从下拉列表中选择一种:");

JComboBox patternList = new JComboBox(patternExamples);

patternList.addActionListener(this);//patternList的监视器是这个面板

patternList.setForeground(Color.yellow);

//创建一个显示结果用户界面

JLabel resultLabel = new JLabel("当前 日期/时间",

JLabel.LEADING);//相当于LEFT

result = new JLabel();

result.setForeground(Color.black);

result.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createLineBorder(Color.cyan),

BorderFactory.createEmptyBorder(7,7,7,7)

));

//布置构件

JPanel patternPanel = new JPanel();

patternPanel.setLayout(new BoxLayout(patternPanel,

BoxLayout.PAGE_AXIS));

patternPanel.add(patternLabel2);

patternList.setAlignmentX(Component.LEFT_ALIGNMENT);

patternPanel.add(patternList);

JPanel resultPanel = new JPanel(new GridLayout(2, 1));//新建一个网格视图的面板

resultPanel.add(resultLabel);

resultPanel.add(result);

patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

add(patternPanel);

add(Box.createRigidArea(new Dimension(0, 10)));

add(resultPanel);

setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

reformat();

}

/**

*
方法说明:事件处理

*
输入參数:

*
返回类型:

*/

public void actionPerformed(ActionEvent e) { //patternList监视器

JComboBox cb = (JComboBox)e.getSource();

String newSelection = (String)cb.getSelectedItem();

currentPattern = newSelection;

reformat();

}

/**

*
方法说明:格式和显示今天的日期

*
输入參数:

*
返回类型:

*/

public void reformat() {

Date today = new Date();

SimpleDateFormat formatter =

new SimpleDateFormat(currentPattern);

try {

String dateString = formatter.format(today);

result.setForeground(Color.red);

result.setText(dateString);

} catch (IllegalArgumentException iae) {

result.setForeground(Color.red);

result.setText("Error: " + iae.getMessage());

}

}

/**

*
方法说明:主方法

*
输入參数:

*
返回类型:

*/

public static void main(String[] args) {

JFrame.setDefaultLookAndFeelDecorated(true);

//创建一个窗口

frame = new JFrame("ComboBox");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建一个面版容器

JComponent newContentPane = new ComboBoxDemo();

newContentPane.setOpaque(true);

frame.setContentPane(newContentPane);

frame.setForeground(Color.cyan);

//显示窗口

frame.pack();

frame.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值