📄 viewmonthtest.java
字号:
package Demo;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class ViewMonthTest extends JPanel implements ActionListener{
private ViewMonth vm;
private JTextField jt1,jt2;
private JButton jb1,jb2;
private JLabel jl1,jl2;
private JPanel jp;
public ViewMonthTest(){
int ye=new Date().getYear()+1900;
int mon=new Date().getMonth()+1;
jt1=new JTextField(""+ye);
jt2=new JTextField(""+mon);
jb1=new JButton("确定");
jb2=new JButton("修改");
jb1.addActionListener(this);
jb2.addActionListener(this);
JPanel jpb=new JPanel();
jpb.add(jb1);
jpb.add(jb2);
JPanel jpt=new JPanel(new GridLayout(1,4));
jpt.add(jl1=new JLabel("年"));
jpt.add(jt1);
jpt.add(jl2=new JLabel("月"));
jpt.add(jt2);
jl1.setHorizontalAlignment(JLabel.RIGHT);
jl2.setHorizontalAlignment(JLabel.RIGHT);
vm=new ViewMonth(ye,mon);
jp=new JPanel(new BorderLayout());
jp.add(vm,BorderLayout.CENTER);
this.setLayout(new BorderLayout());
this.add(jpt,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
this.add(jpb,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1){
try{
int year=Integer.parseInt(jt1.getText().trim());
int mon=Integer.parseInt(jt2.getText().trim());
if(year<1800 ||year>2100||mon>12||mon<1){
JOptionPane.showMessageDialog(null,"您输入的数字超过范围","错误",
JOptionPane.ERROR_MESSAGE);
}
else{
vm=new ViewMonth(year,mon);
jp.removeAll();
jp.add(vm,BorderLayout.CENTER);
SwingUtilities.updateComponentTreeUI(this);
jt1.setEditable(false);
jt2.setEditable(false);
}
}
catch(NumberFormatException nume){
JOptionPane.showMessageDialog(null,"您输入的数字有非法字符","错误",
JOptionPane.ERROR_MESSAGE);
}
}
else if(e.getSource()==jb2){
jt1.setEditable(true);
jt2.setEditable(true);
}
}
public static void main(String args[]){
JFrame j=new JFrame("测试");
j.getContentPane().add(new ViewMonthTest());
j.setBounds(300,400,300,300);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -