Java GUI 文字框點選時提示資訊消失和彈出視窗在按鈕或者標題欄顯示倒計時
阿新 • • 發佈:2018-12-15
最近在做課程設計,遇到一些介面設計,比較麻煩,花了一些心思設計,所以在此記錄下來,雖然不是最好的,用其他語言或其他方式可能會更簡單些。
描述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();
}
}