当前位置: 技术问答>java相关
Button一问
来源: 互联网 发布时间:2015-05-29
本文导语: 我欲实现,当Button被单击凹入;再次单击凸出。该如何处理。 先谢谢! | 对JToggleButton反转按纽做文章! | 可以自己定义button,也可以在程序中直接写 JButton b1=new JButton(); int...
我欲实现,当Button被单击凹入;再次单击凸出。该如何处理。
先谢谢!
先谢谢!
|
对JToggleButton反转按纽做文章!
|
可以自己定义button,也可以在程序中直接写
JButton b1=new JButton();
int clickTime=1;
click事件函数中:
clickFunction(){
clickTime*=-1;
if(clickTime==1)
b1.setBorder(BorderFactory.createLoweredBevelBorder());
else
b1.setBorder(BorderFactory.createRaisedBevelBorder());
}
JButton b1=new JButton();
int clickTime=1;
click事件函数中:
clickFunction(){
clickTime*=-1;
if(clickTime==1)
b1.setBorder(BorderFactory.createLoweredBevelBorder());
else
b1.setBorder(BorderFactory.createRaisedBevelBorder());
}
|
你可以向下面的例子那样自己做button!
/*
* @(#)RoundButton.java 1.6 97/06/18 Jeff Dinkins
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
*/
import java.applet.*;
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* RoundButton - a class that produces a lightweight button.
*
* Lightweight components can have "transparent" areas, meaning that
* you can see the background of the container behind these areas.
*
*/
public class RoundButton extends Component {
ActionListener actionListener; // Post action events to listeners
String label; // The Button's text
protected boolean pressed = false; // true if the button is detented.
public static void main(String args[]){
JFrame test=new JFrame();
Container co=test.getContentPane();
RoundButton te=new RoundButton();
co.setLayout(new FlowLayout());
test.setBackground(Color.black);
co.add(te);
test.setSize(300,300);
test.setVisible(true);
/*
RoundButton te=new RoundButton();
//te.pack();
te.show();*/
}
/**
* Constructs a RoundButton with no label.
*/
public RoundButton() {
this("");
}
/**
* Constructs a RoundButton with the specified label.
* @param label the label of the button
*/
public RoundButton(String label) {
this.label = label;
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* gets the label
* @see setLabel
*/
public String getLabel() {
return label;
}
/**
* sets the label
* @see getLabel
*/
public void setLabel(String label) {
this.label = label;
invalidate();
repaint();
}
/**
* paints the RoundButton
*/
public void paint(Graphics g) {
int s = Math.min(getSize().width - 1, getSize().height - 1);
// paint the interior of the button
if(pressed) {
g.setColor(getBackground().darker().darker());
} else {
g.setColor(getBackground());
}
g.fillArc(0, 0, s, s, 0, 360);
// draw the perimeter of the button
g.setColor(getBackground().darker().darker().darker());
g.drawArc(0, 0, s, s, 0, 360);
// draw the label centered in the button
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
g.setColor(getForeground());
g.drawString(label,
s/2 - fm.stringWidth(label)/2,
s/2 + fm.getMaxDescent());
}
}
/**
* The preferred size of the button.
*/
public Dimension getPreferredSize() {
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
return new Dimension(max, max);
} else {
return new Dimension(100, 100);
}
}
/**
* The minimum size of the button.
*/
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
/**
* Adds the specified action listener to receive action events
* from this button.
* @param listener the action listener
*/
public void addActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.add(actionListener, listener);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* Removes the specified action listener so it no longer receives
* action events from this button.
* @param listener the action listener
*/
public void removeActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.remove(actionListener, listener);
}
/**
* Determine if click was inside round button.
*/
public boolean contains(int x, int y) {
int mx = getSize().width/2;
int my = getSize().height/2;
return (((mx-x)*(mx-x) + (my-y)*(my-y))
/*
* @(#)RoundButton.java 1.6 97/06/18 Jeff Dinkins
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
*/
import java.applet.*;
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* RoundButton - a class that produces a lightweight button.
*
* Lightweight components can have "transparent" areas, meaning that
* you can see the background of the container behind these areas.
*
*/
public class RoundButton extends Component {
ActionListener actionListener; // Post action events to listeners
String label; // The Button's text
protected boolean pressed = false; // true if the button is detented.
public static void main(String args[]){
JFrame test=new JFrame();
Container co=test.getContentPane();
RoundButton te=new RoundButton();
co.setLayout(new FlowLayout());
test.setBackground(Color.black);
co.add(te);
test.setSize(300,300);
test.setVisible(true);
/*
RoundButton te=new RoundButton();
//te.pack();
te.show();*/
}
/**
* Constructs a RoundButton with no label.
*/
public RoundButton() {
this("");
}
/**
* Constructs a RoundButton with the specified label.
* @param label the label of the button
*/
public RoundButton(String label) {
this.label = label;
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* gets the label
* @see setLabel
*/
public String getLabel() {
return label;
}
/**
* sets the label
* @see getLabel
*/
public void setLabel(String label) {
this.label = label;
invalidate();
repaint();
}
/**
* paints the RoundButton
*/
public void paint(Graphics g) {
int s = Math.min(getSize().width - 1, getSize().height - 1);
// paint the interior of the button
if(pressed) {
g.setColor(getBackground().darker().darker());
} else {
g.setColor(getBackground());
}
g.fillArc(0, 0, s, s, 0, 360);
// draw the perimeter of the button
g.setColor(getBackground().darker().darker().darker());
g.drawArc(0, 0, s, s, 0, 360);
// draw the label centered in the button
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
g.setColor(getForeground());
g.drawString(label,
s/2 - fm.stringWidth(label)/2,
s/2 + fm.getMaxDescent());
}
}
/**
* The preferred size of the button.
*/
public Dimension getPreferredSize() {
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
return new Dimension(max, max);
} else {
return new Dimension(100, 100);
}
}
/**
* The minimum size of the button.
*/
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
/**
* Adds the specified action listener to receive action events
* from this button.
* @param listener the action listener
*/
public void addActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.add(actionListener, listener);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* Removes the specified action listener so it no longer receives
* action events from this button.
* @param listener the action listener
*/
public void removeActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.remove(actionListener, listener);
}
/**
* Determine if click was inside round button.
*/
public boolean contains(int x, int y) {
int mx = getSize().width/2;
int my = getSize().height/2;
return (((mx-x)*(mx-x) + (my-y)*(my-y))
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!