您的位置:首页 > 编程语言 > Java开发

Java Swing基础功能整理

2016-11-14 10:35 399 查看

这里简单记录下swing一些基础功能,使用过程中可能经常会用到的一些功能。

1.简单的提示弹窗,类似js中alert弹窗,手机中常见弹窗提示等

JOptionPane.showMessageDialog(this, "请填写正确参数!", "提示",
JOptionPane.INFORMATION_MESSAGE);

int flag = JOptionPane.showConfirmDialog(null, "清空日志前注意导出日志备份,确定清空日志?", "提示",
JOptionPane.YES_NO_OPTION);
//弹窗提示确定和取消,根据flag判断其操作从而进行后续逻辑。


2.此处介绍一种代码实现的提示弹窗,效果为弹窗提示后自动淡出提示框。

  调用方法:new对象后调用下面代码中show()方法。
package com.fisee.ui.utils;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import javax.swing.JButton;
import javax.swing.JWindow;

public class CPopWindow {

private int _height = 30; // 气泡提示高
private int _step = 60; // 设定循环的步长
private int _stepTime = 60; // 每步时间
private int _displayTime = 2000;  // 显示时间
private Font _font;    // 字体

/**
* 构造函数,初始化默认气泡提示设置
*
*/
public CPopWindow()
{
_font = new Font("微软雅黑", 0, 12);// 设定字体
try {
JWindow.class.getMethod("setAlwaysOnTop",
new Class[] { Boolean.class });
} catch (Exception e) {
}
}

/**
* 重构JWindow用于显示单一气泡提示框
*
*/
class ToolTipSingle extends Window
{
private static final long serialVersionUID = 1L;
private JButton _message = new JButton(){
/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void repaint()
{
}
};

@SuppressWarnings("deprecation")
public ToolTipSingle(String msg) {
super(null);

int cntText=msg.length();
int with = cntText*11+40;
int height = _height;

Rectangle screenRect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int screenHeight = (int) screenRect.height;
int screenWidth = (int) screenRect.width;

int posx = screenWidth/2-with/2;
int posy = screenHeight/2-height/2;

setBounds(posx, posy, with, height);
_message.setFont(getMessageFont());
_message.setText(msg);

_message.setFocusPainted(false);
//_message.setEnabled(false);
add(_message, BorderLayout.CENTER);

setAlwaysOnTop(true);
setLocationRelativeTo(null);

show(true);
}

/**
* 动画开始
*
*/
public void animate() {
new Animation(this).start();
}
}

/**
* 此类处则动画处理
*
*/
class Animation extends Thread
{

ToolTipSingle _single;

public Animation(ToolTipSingle single) {
this._single = single;
}

/**
* 开始动画处理
*/
@SuppressWarnings("deprecation")
public void run() {
try {
float value = 0.0f;
while((value+0.02)<1.0f)
{
value+=0.02f;
com.sun.awt.AWTUtilities.setWindowOpacity(_single, value);
Thread.sleep(10);
}

while(_displayTime>50)
{
Thread.sleep(50);
_displayTime = _displayTime-50;
}

value=1;
while((value-0.02)>0f)
{
value-=0.02f;
com.sun.awt.AWTUtilities.setWindowOpacity(_single, value);
Thread.sleep(20);
}

} catch (Exception e) {
throw new RuntimeException(e);
}

_single.show(false);
}
}

/**
* 设定显示信息
*
* @param icon
* @param msg
*/
public void show(String msg) {
new ToolTipSingle(msg).animate();
}

/**
* 获得当前消息字体
*
* @return
*/
public Font getMessageFont() {
return _font;
}

/**
* 设置当前消息字体
*
* @param font
*/
public void setMessageFont(Font font) {
_font = font;
}

/**
* 获得显示时间
*
* @return
*/
public int getDisplayTime() {
return _displayTime;
}

/**
* 设置显示时间
*
* @param displayTime
*/
public void setDisplayTime(int displayTime) {
this._displayTime = displayTime;
}

/**
* 获得循环步长
*
* @return
*/
public int getStep() {
return _step;
}

/**
* 设定循环步长
*
* @param _step
*/
public void setStep(int _step) {
this._step = _step;
}

public int getStepTime() {
return _stepTime;
}

public void setStepTime(int _stepTime) {
this._stepTime = _stepTime;
}

public static void main(String[] argv)
{
new CPopWindow().show("全部任务已经载入完成!");
}
}
更多提示效果可才考:http://www.th7.cn/Program/java/201511/689070.shtml

3.panel的设置边框,这里简单介绍两种个人常用的边框:凸起边框、带标题的边框
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createRaisedBevelBorder());//凸起
panel.setBorder(BorderFactory.createTitledBorder("标题"));//带标题的
更多边框样式可参考:http://vcsos.com/article/pageSource/120322/20120322081356.shtml

4.JTextArea自动滚动到底部和停止滚动
//自动滚动
public void updateTextarea() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (mCellRetrieveField.getText() != null && autoScrollToBottom) {
mCellRetrieveField.setSelectionStart(mCellRetrieveField
.getText().length());
}
}
});
}

//移动滚动条后停止滚动
final JScrollBar bar = jScrollPane.getVerticalScrollBar();
bar.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(bar.getMaximum() == bar.getValue()+bar.getHeight()){
autoScrollToBottom = true;
}else{
autoScrollToBottom = false;
}
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息