1.一个java日历小程序...输入(或下拉框)一个日期,自动显示出是那一天......在frame里......
2.一个下拉框,里面有 圆 和 矩形两个 选项 点一个 自动画出图形..并在一个单行文本框内显示出 面积
2.一个下拉框,里面有 圆 和 矩形两个 选项 点一个 自动画出图形..并在一个单行文本框内显示出 面积
解决方案 »
- 这个String str为什么没变??
- 服务器端传递JTREE,用XMLEncoder,结果把SOCKET关了
- 30分求教,JAVA小程序
- JPA(Hibernate)+Spring : no session or session was closed !请注意是非J2EE(web)下如何解决!
- eclipse+loboz+tomcat开发JSP遇到的天大问题?
- 问一个用java实现的算法问题。。。。是不是要分开讨论!!!?
- 小问题,怎么判断这个控件是JTextField呢?请看下面代码
- 关于科学计数法的问题,请帮忙!
- 我昨天发的贴怎么找啊?大家帮帮忙啊!
- ~~进门就送分!~~ 如何执行包含多语种的SQL语句呢?
- checkstyle,文件结尾留空行
- 最近学习JAVA,新写的简单图书馆运行时出现的问题
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//*
* DrawJFrame.java
*
* Created on 2009-12-3, 9:44:03
*/package javaapplication1;import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;/**
*
* @author cangyingzhijia
*/
public class DrawFrame extends javax.swing.JFrame {
private final static double X1 = 50,Y1 = 20;
private final static double X2 = 400,Y2 = 400;
private final static double Radius = 200;
/** Creates new form NewJFrame */
public DrawFrame() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jPanelDraw = new JPanel(){
public void paint(Graphics g){
if("圆".equals(jComboBoxTuxing.getSelectedItem())){
jTextFieldMianJi.setText("" + (Math.PI * Math.pow(Radius ,2)));
paintCircle((Graphics2D)g);
}else{
jTextFieldMianJi.setText("" + ((X2 - X1) * (Y2 - Y1)));
paintRectangle((Graphics2D)g);
}
} public void paintRectangle(Graphics2D g){
g.draw(new Rectangle2D.Double(X1,Y1,X2,Y2) );
} public void paintCircle(Graphics2D g){
g.draw(new Ellipse2D.Double(X1,Y1,X1 +2 * Radius,Y1+2 * Radius) );
} };
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextFieldMianJi = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jComboBoxTuxing = new javax.swing.JComboBox(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("圆/矩形"); javax.swing.GroupLayout jPanelDrawLayout = new javax.swing.GroupLayout(jPanelDraw);
jPanelDraw.setLayout(jPanelDrawLayout);
jPanelDrawLayout.setHorizontalGroup(
jPanelDrawLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 638, Short.MAX_VALUE)
);
jPanelDrawLayout.setVerticalGroup(
jPanelDrawLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 452, Short.MAX_VALUE)
); getContentPane().add(jPanelDraw, java.awt.BorderLayout.CENTER); jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel1.setText("面积");
jPanel2.add(jLabel1); jTextFieldMianJi.setText("0");
jTextFieldMianJi.setFocusCycleRoot(true);
jTextFieldMianJi.setMinimumSize(new java.awt.Dimension(30, 21));
jTextFieldMianJi.setName("mianji"); // NOI18N
jTextFieldMianJi.setPreferredSize(new java.awt.Dimension(250, 21));
jPanel2.add(jTextFieldMianJi); jLabel2.setText("图形");
jPanel2.add(jLabel2); jComboBoxTuxing.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "圆", "矩形" }));
jComboBoxTuxing.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBoxTuxingItemStateChanged(evt);
}
});
jPanel2.add(jComboBoxTuxing); getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START); pack();
}// </editor-fold> private void jComboBoxTuxingItemStateChanged(java.awt.event.ItemEvent evt) {
jPanelDraw.repaint();
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DrawFrame().setVisible(true);
}
});
} // Variables declaration - do not modify
private javax.swing.JComboBox jComboBoxTuxing;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanelDraw;
private javax.swing.JTextField jTextFieldMianJi;
// End of variables declaration}
package util;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
//闰年共有366天(31,29,31,30,31,30,31,31,30,31,30,31)。其中二月天数比非闰年的二月天数大1
public class MyRiLi extends JPanel { protected int yy; protected int mm, dd; /** 存放按钮 */
protected JButton labs[][]; /** 每个月份开头的几个空白日期 */
protected int leadGap = 0; /** 日期对象 */
Calendar calendar = new GregorianCalendar(); /** 年份 */
protected final int thisYear = calendar.get(Calendar.YEAR); /** 月份 */
protected final int thisMonth = calendar.get(Calendar.MONTH); /** 取消按钮高亮时以这个按钮为基准 */
private JButton b0; /** 存放月份 */
private JComboBox monthChoice; /** 存放年份 */
private JComboBox yearChoice; /**
* 构造方法,获取今天的日期
*/
MyRiLi() {
super();
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
} MyRiLi(int year, int month, int today) {
super();
setYYMMDD(year, month, today);
buildGUI();
recompute();
} private void setYYMMDD(int year, int month, int today) {
yy = year;
mm = month;
dd = today;
} String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
"十月", "十一月", "十二月" }; private void buildGUI() {
getAccessibleContext().setAccessibleDescription("日历未上载");
setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); JPanel tp = new JPanel();
tp.add(monthChoice = new JComboBox());
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
monthChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = monthChoice.getSelectedIndex();
if (i >= 0) {
mm = i;
recompute();
}
}
});
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription("选择一个月份"); tp.add(yearChoice = new JComboBox());
yearChoice.setEditable(true);
for (int i = yy - 5; i < yy + 5; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = yearChoice.getSelectedIndex();
if (i >= 0) {
yy = Integer.parseInt(yearChoice.getSelectedItem()
.toString());
recompute();
}
}
});
add(BorderLayout.CENTER, tp); JPanel bp = new JPanel();
bp.setLayout(new GridLayout(7, 7));
labs = new JButton[6][7]; // 星期
bp.add(b0 = new JButton("日"));
bp.add(new JButton("一"));
bp.add(new JButton("二"));
bp.add(new JButton("三"));
bp.add(new JButton("四"));
bp.add(new JButton("五"));
bp.add(new JButton("六")); ActionListener dateSetter = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (!num.equals("")) {
// 将当前日期高亮
setDayActive(Integer.parseInt(num));
System.out.println(num);
}
}
}; // 创建所有的按钮
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
bp.add(labs[i][j] = new JButton(""));
labs[i][j].addActionListener(dateSetter);
} add(BorderLayout.SOUTH, bp);
} // 列出每个月的天数
public final static int dom[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 }; /** 计算每个月份的日期,显示在面板上 */
protected void recompute() {
if (mm < 0 || mm > 11)
throw new IllegalArgumentException("月份 " + mm + " 必须在0-11之间");
clearDayActive();
calendar = new GregorianCalendar(yy, mm, dd); // 计算每个月份开头的几个空白日期
leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = dom[mm];
if (isLeap(calendar.get(Calendar.YEAR)) && mm == 1)
++daysInMonth; for (int i = 0; i < leadGap; i++) {
labs[0][i].setText("");
} // 填入日期数字
for (int i = 1; i <= daysInMonth; i++) {
JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7];
b.setText(Integer.toString(i));
} for (int i = leadGap+daysInMonth; i < 6 * 7; i++) {
labs[(i) / 7][(i) % 7].setText("");
} if (thisYear == yy && mm == thisMonth)
setDayActive(dd); repaint();
} /**
* 判断是否闰年
*/
public boolean isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
return false;
} /** 设置年月日 */
public void setDate(int yy, int mm, int dd) {
this.yy = yy;
this.mm = mm;
this.dd = dd;
recompute();
} /** 取消按钮高亮 */
private void clearDayActive() {
JButton b; if (activeDay > 0) {
b = labs[(leadGap + activeDay - 1) / 7][(leadGap + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
} private int activeDay = -1; /** 设置按钮高亮 */
public void setDayActive(int newDay) { clearDayActive(); if (newDay <= 0)
dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else
dd = newDay;
Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
} public static void main(String[] args) {
JFrame f = new JFrame("MyRiLi");
Container c = f.getContentPane();
c.setLayout(new FlowLayout()); c.add(new MyRiLi()); f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
package util;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
//闰年共有366天(31,29,31,30,31,30,31,31,30,31,30,31)。其中二月天数比非闰年的二月天数大1
public class MyRiLi extends JPanel { protected int yy; protected int mm, dd; /** 存放按钮 */
protected JButton labs[][]; /** 每个月份开头的几个空白日期 */
protected int leadGap = 0; /** 日期对象 */
Calendar calendar = new GregorianCalendar(); /** 年份 */
protected final int thisYear = calendar.get(Calendar.YEAR); /** 月份 */
protected final int thisMonth = calendar.get(Calendar.MONTH); /** 取消按钮高亮时以这个按钮为基准 */
private JButton b0; /** 存放月份 */
private JComboBox monthChoice; /** 存放年份 */
private JComboBox yearChoice; /**
* 构造方法,获取今天的日期
*/
MyRiLi() {
super();
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
} MyRiLi(int year, int month, int today) {
super();
setYYMMDD(year, month, today);
buildGUI();
recompute();
} private void setYYMMDD(int year, int month, int today) {
yy = year;
mm = month;
dd = today;
} String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
"十月", "十一月", "十二月" }; private void buildGUI() {
getAccessibleContext().setAccessibleDescription("日历未上载");
setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); JPanel tp = new JPanel();
tp.add(monthChoice = new JComboBox());
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
monthChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = monthChoice.getSelectedIndex();
if (i >= 0) {
mm = i;
recompute();
}
}
});
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription("选择一个月份"); tp.add(yearChoice = new JComboBox());
yearChoice.setEditable(true);
for (int i = yy - 5; i < yy + 5; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = yearChoice.getSelectedIndex();
if (i >= 0) {
yy = Integer.parseInt(yearChoice.getSelectedItem()
.toString());
recompute();
}
}
});
add(BorderLayout.CENTER, tp); JPanel bp = new JPanel();
bp.setLayout(new GridLayout(7, 7));
labs = new JButton[6][7]; // 星期
bp.add(b0 = new JButton("日"));
bp.add(new JButton("一"));
bp.add(new JButton("二"));
bp.add(new JButton("三"));
bp.add(new JButton("四"));
bp.add(new JButton("五"));
bp.add(new JButton("六")); ActionListener dateSetter = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (!num.equals("")) {
// 将当前日期高亮
setDayActive(Integer.parseInt(num));
System.out.println(num);
}
}
}; // 创建所有的按钮
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
bp.add(labs[i][j] = new JButton(""));
labs[i][j].addActionListener(dateSetter);
} add(BorderLayout.SOUTH, bp);
} // 列出每个月的天数
public final static int dom[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 }; /** 计算每个月份的日期,显示在面板上 */
protected void recompute() {
if (mm < 0 || mm > 11)
throw new IllegalArgumentException("月份 " + mm + " 必须在0-11之间");
clearDayActive();
calendar = new GregorianCalendar(yy, mm, dd); // 计算每个月份开头的几个空白日期
leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = dom[mm];
if (isLeap(calendar.get(Calendar.YEAR)) && mm == 1)
++daysInMonth; for (int i = 0; i < leadGap; i++) {
labs[0][i].setText("");
} // 填入日期数字
for (int i = 1; i <= daysInMonth; i++) {
JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7];
b.setText(Integer.toString(i));
} for (int i = leadGap+daysInMonth; i < 6 * 7; i++) {
labs[(i) / 7][(i) % 7].setText("");
} if (thisYear == yy && mm == thisMonth)
setDayActive(dd); repaint();
} /**
* 判断是否闰年
*/
public boolean isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
return false;
} /** 设置年月日 */
public void setDate(int yy, int mm, int dd) {
this.yy = yy;
this.mm = mm;
this.dd = dd;
recompute();
} /** 取消按钮高亮 */
private void clearDayActive() {
JButton b; if (activeDay > 0) {
b = labs[(leadGap + activeDay - 1) / 7][(leadGap + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
} private int activeDay = -1; /** 设置按钮高亮 */
public void setDayActive(int newDay) { clearDayActive(); if (newDay <= 0)
dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else
dd = newDay;
Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
} public static void main(String[] args) {
JFrame f = new JFrame("MyRiLi");
Container c = f.getContentPane();
c.setLayout(new FlowLayout()); c.add(new MyRiLi()); f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}