最近在做课程设计,遇到一些界面设计,比较麻烦,花了一些心思设计,所以在此记录下来,虽然不是最好的,用其他语言或其他方式可能会更简单些。
描述1:JTextField文本框未输入时,在文本框上的提示信息颜色设置为灰色,点击文本框时,提示信息消失,输入的字体颜色变成黑色,再次点击时,输入的信息不会被清空。
先上效果图:
代码Demo.java
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
/**
*
*
* @Title: Demo.java
* @Package test
* @Description: 文本框点击提示信息消失
* @author Cqh_i
* @date 2018年10月13日 下午11:48:16
*/
public class Demo extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField IDText;
private JTextField CardText;
private JLabel IDNumber;
private JLabel CardNumber;
private JButton ConfirmButton;
private JButton ResetButton;
public Demo() {
getContentPane().setLayout(null);
IDNumber = new JLabel("身份证号码");
IDNumber.setFont(new Font("宋体", Font.BOLD, 18));
IDNumber.setBounds(96, 66, 95, 18);
CardNumber = new JLabel("银行卡号码");
CardNumber.setFont(new Font("宋体", Font.BOLD, 18));
CardNumber.setBounds(96, 103, 95, 18);
IDText = new JTextField("身份证号码");
IDText.setToolTipText("身份证号码");
IDText.setBounds(204, 65, 132, 24);
IDText.setColumns(10);
IDText.setForeground(Color.lightGray);// 设置前景色为灰色
IDText.setEditable(false);// 设置为不可编辑状态
IDText.setBackground(Color.WHITE);// 设置背景色为白色
IDText.addMouseListener(new MouseAdapter() {
// 点击输入框去除文字,**文本框
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (!IDText.isEditable()) {
IDText.setText("");
IDText.setForeground(Color.BLACK);
IDText.setEditable(true);
IDText.requestFocus();
}
}
}
});
CardText = new JTextField("银行卡号码");
CardText.setToolTipText("银行卡号码");
CardText.setBounds(204, 102, 132, 24);
CardText.setColumns(10);
CardText.setForeground(Color.lightGray);// 设置前景色为灰色
CardText.setEditable(false);// 设置为不可编辑状态
CardText.setBackground(Color.WHITE);// 设置背景色为白色
CardText.addMouseListener(new MouseAdapter() {
// 点击输入框去除文字,**文本框
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (!CardText.isEditable()) {
CardText.setText("");
CardText.setForeground(Color.BLACK);
CardText.setEditable(true);
CardText.requestFocus();
}
}
}
});
ConfirmButton = new JButton("确认");
ConfirmButton.setFont(new Font("宋体", Font.BOLD, 18));
ConfirmButton.setBounds(96, 148, 113, 27);
ResetButton = new JButton("重置");
ResetButton.setFont(new Font("宋体", Font.BOLD, 18));
ResetButton.setBounds(223, 148, 113, 27);
ResetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IDText.setForeground(Color.lightGray);
CardText.setForeground(Color.lightGray);
IDText.setText("身份证号码");
CardText.setText("银行卡卡号");
IDText.setEditable(false);
CardText.setEditable(false);
}
});
getContentPane().add(IDNumber);
getContentPane().add(IDText);
getContentPane().add(CardNumber);
getContentPane().add(CardText);
getContentPane().add(ConfirmButton);
getContentPane().add(ResetButton);
setLocationRelativeTo(null);// 窗口居中
setTitle("Demo");
setVisible(true);
setSize(450, 301);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}
描述2:在弹出窗口的按钮(或者标题)中显示倒计时
先上效果图:
这种效果我是用JFrame做的,存在缺陷,因为JFrame没有模态,要用模态比较麻烦,父窗口不能及时子窗口的返回值。可以看看下面用JDialog做的,效果比较好。
CountDownFrame.java
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.WindowConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* @Title: CountDownFrame.java
* @Description: 倒计时弹出窗口
* @author Cqh_i
* @date 2018年10月14日 上午9:57:24
*/
public class CountDownFrame {
private int secends = 11;// 倒计时时间
private JButton ConfirmButton;
private JButton Cancelbutton;
private JLabel label;
private JFrame jf;
int res = 0;
/**
* @param jfather
* 非模态方式返回值意义不大
*/
public void showCountDownFrame(JFrame jfather) {
ConfirmButton = new JButton("确认");
ConfirmButton.setBounds(14, 44, 99, 27);
ConfirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
res = 1;
jf.dispose();
}
});
Cancelbutton = new JButton("取消");
Cancelbutton.setBounds(139, 44, 99, 27);
Cancelbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
res = 0;
jf.dispose();
}
});
label = new JLabel("这是一个倒计时弹出窗口");
label.setBounds(55, 13, 165, 18);
jf = new JFrame();
jf.getContentPane().setLayout(null);
jf.setSize(266, 119);
jf.setUndecorated(true); // 去掉窗口的装饰
jf.getRootPane().setWindowDecorationStyle(JRootPane.WARNING_DIALOG); // 采用指定的窗口装饰风格
jf.setResizable(false);
jf.setVisible(true);
jf.setLocationRelativeTo(jfather);// 窗口居中
jf.setTitle("请再次确认");
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.getContentPane().add(label);
jf.getContentPane().add(ConfirmButton);
jf.getContentPane().add(Cancelbutton);
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
secends--;
String str = "取消(" + String.valueOf(secends) + "秒)";
if (secends == 0) {
res = 0;
jf.dispose();
} else {
Cancelbutton.setText(str);
}
}
}, 1, 1, TimeUnit.SECONDS);
}
}
下面这个倒计时弹出窗口是用JDialog做的。
先上效果图:
CountDownDialog.java
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* @Title: CountDownDialog.java
* @Description: 倒计时弹出对话框
* @author Cqh_i
* @date 2018年10月14日 上午10:15:55
*/
public class CountDownDialog {
private JDialog Dialog;
private int secends = 11;//倒计时时间
private JButton ConfirmButton;
private JButton Cancelbutton;
private JLabel label;
private int res = -1;//确认为1,取消为0,关闭为-1
/**
* @param jfather
* @Description: 创建倒计时弹出对话框,并返回点击结果
* @return 确认为1,取消为0,关闭为-1
*/
public int showCountDownDialog(JFrame jfather){
Dialog = new JDialog(jfather, true);//创建一个具有空标题和指定模态的对话框,并以 jfather作为其所有者。
Dialog.setLayout(null);
Dialog.pack();
Dialog.setSize(new Dimension(266, 125));
Dialog.setLocationRelativeTo(jfather);
Dialog.setTitle("倒计时开始!");
Dialog.setResizable(false);
ConfirmButton = new JButton("确认");
ConfirmButton.setBounds(14, 44, 99, 27);
ConfirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
res = 1;
Dialog.dispose();
}
});
Cancelbutton = new JButton("取消");
Cancelbutton.setBounds(139, 44, 99, 27);
Cancelbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
res = 0;
Dialog.dispose();
}
});
label = new JLabel("这是一个倒计时弹出窗口");
label.setBounds(55, 13, 165, 18);
Dialog.add(ConfirmButton);
Dialog.add(Cancelbutton);
Dialog.add(label);
//任务调度
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
secends--;
String str = "取消(" + String.valueOf(secends) + "秒)";
Dialog.setTitle("提示: 对话框将在"+secends+"秒后自动关闭");
if (secends == 0) {
res = 0;
Dialog.dispose();
} else {
Cancelbutton.setText(str);
}
}
}, 1, 1, TimeUnit.SECONDS);
Dialog.setVisible(true);//模态下,setVisible要放在添加组件后面
return res;
}
}
Demo.java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
* @Title: Demo.java
* @Package test1
* @Description: 演示弹出倒计时窗口
* @author Cqh_i
* @date 2018年10月14日 上午12:00:38
*/
public class Demo extends JFrame{
private static final long serialVersionUID = 1L;
private JButton Button;
int res;
public Demo(){
Button = new JButton("点我弹出倒计时窗口");
Button.setToolTipText("点我弹出倒计时窗口");
Button.setFont(new Font("宋体", Font.BOLD, 18));
Button.setBounds(111, 111, 229, 27);
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*CountDownFrame cdf = new CountDownFrame();
cdf.showCountDownFrame(Demo.this);*/
CountDownDialog cdd = new CountDownDialog();
res = cdd.showCountDownDialog(Demo.this);
System.out.println("返回结果=="+res);
}
});
getContentPane().setLayout(null);
getContentPane().add(Button, BorderLayout.CENTER);
// 第一种窗口居中方式
//Demo.this.setLocationRelativeTo(null);
// 第二种窗口居中方式
//得到显示器屏幕的宽、高
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 得到窗体的宽、高
int windowsWidth = this.getWidth();
int windowsHeight = this.getHeight();
//System.out.println(windowsWidth+","+windowsHeight);
Demo.this.setBounds((width - windowsWidth) / 2,(height - windowsHeight) / 2, windowsWidth, windowsHeight);
setTitle("Demo");
setVisible(true);
setSize(450, 301);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}