当前位置: 技术问答>java相关
Applet中的button的样式和形状,用记事本编写(没有其它工具),还有它的单击事件如何做
来源: 互联网 发布时间:2015-06-11
本文导语: 在Applet中,创建一个button,名字为btnEng 我想让它成多边形形状,且让它平面显示 当它单击时,把页面转到另一个页面 我该如何做? | 发一个代码给你!!!??????? | 这是一个...
在Applet中,创建一个button,名字为btnEng
我想让它成多边形形状,且让它平面显示
当它单击时,把页面转到另一个页面
我该如何做?
我想让它成多边形形状,且让它平面显示
当它单击时,把页面转到另一个页面
我该如何做?
|
发一个代码给你!!!???????
|
这是一个圆形的button的例子!
你可以改一改!
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class RoundButton extends JButton {
public RoundButton(String label) {
super(label);
// These statements enlarge the button so that it
// becomes a circle rather than an oval.
Dimension size = getPreferredSize();
//size.width = size.height = Math.max(size.width,
// size.height);
size.width=100;
size.height=46;
setPreferredSize(size);
// This call causes the JButton not to paint
// the background.
// This allows us to paint a round background.
setContentAreaFilled(false);
}
// Paint the round background and label.
protected void paintComponent(Graphics g) {
Graphics2D g2D=(Graphics2D)g;
if (getModel().isArmed()) {
if (getModel().isPressed()){
g2D.setColor(Color.darkGray);
setForeground(Color.white);
}else
// You might want to make the highlight color
// a property of the RoundButton class.
g2D.setColor(Color.lightGray);
setForeground(Color.black);
} else {
g2D.setColor(getBackground());
}
g2D.fillOval(0, 0, getSize().width-1,
getSize().height-1);
// This call will paint the label and the
// focus rectangle.
super.paintComponent(g2D);
}
// Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
Graphics2D g2D=(Graphics2D)g;
g2D.setColor(getForeground());
//g2D.setColor(Color.blue);
g2D.drawOval(0, 0,getSize().width-1,
getSize().height-1);
}
/*
// Hit detection.
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size,
// make a new shape object.
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0,
getWidth(), getHeight());
}
return shape.contains(x, y);
}
*/
// Test routine.
public static void main(String[] args) {
// Create a button with the label "Jackpot".
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.green);
// Create a frame in which to show the button.
JFrame frame = new JFrame();
//frame.getContentPane().setBackground(Color.magenta);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(150, 150);
frame.setVisible(true);
}
}
你可以改一改!
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class RoundButton extends JButton {
public RoundButton(String label) {
super(label);
// These statements enlarge the button so that it
// becomes a circle rather than an oval.
Dimension size = getPreferredSize();
//size.width = size.height = Math.max(size.width,
// size.height);
size.width=100;
size.height=46;
setPreferredSize(size);
// This call causes the JButton not to paint
// the background.
// This allows us to paint a round background.
setContentAreaFilled(false);
}
// Paint the round background and label.
protected void paintComponent(Graphics g) {
Graphics2D g2D=(Graphics2D)g;
if (getModel().isArmed()) {
if (getModel().isPressed()){
g2D.setColor(Color.darkGray);
setForeground(Color.white);
}else
// You might want to make the highlight color
// a property of the RoundButton class.
g2D.setColor(Color.lightGray);
setForeground(Color.black);
} else {
g2D.setColor(getBackground());
}
g2D.fillOval(0, 0, getSize().width-1,
getSize().height-1);
// This call will paint the label and the
// focus rectangle.
super.paintComponent(g2D);
}
// Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
Graphics2D g2D=(Graphics2D)g;
g2D.setColor(getForeground());
//g2D.setColor(Color.blue);
g2D.drawOval(0, 0,getSize().width-1,
getSize().height-1);
}
/*
// Hit detection.
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size,
// make a new shape object.
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0,
getWidth(), getHeight());
}
return shape.contains(x, y);
}
*/
// Test routine.
public static void main(String[] args) {
// Create a button with the label "Jackpot".
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.green);
// Create a frame in which to show the button.
JFrame frame = new JFrame();
//frame.getContentPane().setBackground(Color.magenta);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(150, 150);
frame.setVisible(true);
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。