
正文
java代码设置按钮样式 java代码设置按钮样式有哪些
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 怎么改变button的形状
1,首先明确button是安卓的一个控件,是用java语言写的。
2,设置大小的方法:btn.setbounds(x,y,width,height);//设置大小并定位
或者btn.setsize(width,height);//设置大小btn.setlocation(x,y);//定位
3,也可以在布局文件上直接给定大小
比如:
这个button控件高度和宽带都是100px
相关问答
Q1: JAVA中怎么自定义按钮的形状?
貌似这个是不可以自定义java代码设置按钮样式的
JAVA挎平台
可以去别java代码设置按钮样式的操作系统运行
按纽就改变java代码设置按钮样式了
Q2: Java 改变按钮外观、形状,最主要的是如何在上面加图片
/**下面中因为初始时就调用了 paintComponent方法 ,所以你一运行就会看到图片.
你会重绘时才看到,就自己编码设置下..
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class ChangeButton extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeButton frame = new ChangeButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChangeButton() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
BPanel panel = new BPanel();
contentPane.add(panel, BorderLayout.CENTER);
}
}
class BPanel extends JPanel{
JButton button = new JButton("New button");
public BPanel(){
this.setLayout(null);
button.setBounds(131, 80, 160, 160);
this.add(button);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
button.setIcon(new ImageIcon("json.jpg"));
//下面几种还有其它的设置效果请你参照 JDK_API文档吧...
// button.setIconTextGap(3);设置文字与图标之间的间隔
//button.setBorder(border);//设置按键的边框
//button.setBackground(bg);//
//button.setForeground(fg);//
//button.setAlignmentY(alignmentY);//
}
}
Q3: Java 改变按钮外观
通常swing自定义组件继承javax.swing.JComponent并重写protected void paintComponent(Graphics g)方法实现自定义绘制。 重写paintComponent方法时通常要先去掉super.paintComponent(g),因为父方法调用会绘制背景色。不妨先看一下源代码中的调用过程。
在JComponent.java中paintComponent(Graphics g)方法定义如下:
protected void paintComponent(Graphics g) {
if (ui != null) {
Graphics scratchGraphics = (g == null) ? null : g.create();
try {
ui.update(scratchGraphics, this);
}
finally {
scratchGraphics.dispose();
}
}
}
其中ui的声明如下
protected transient ComponentUI ui;
然后转向ComponentUI的update(Graphics g, JComponent c)方法:
public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}
可见如果发现组件是非透明的,就绘制背景,可以看出swing组件的setBackground方法如何绘制背景的。
一般简单的自定义组件,你可以只通过重写paintComponent方法来实现绘制,对于一般的组件这已经足够。对于自定义按钮一般的原则是准备4张背景图对应上述4种状态,这4种状态都可通过鼠标监听来感知,当状态改变时,调用repaint()使Button重绘。除了背景,按钮文本、图标等的改变一样也必须调用repaint()来刷新。
然后重要的一点是你必须重写public Dimension getPreferredSize()来获得按钮的最佳尺寸。getPreferredSize方法对于布局管理器来说至关重要,布局管理器会通过getPreferredSize的判断组件的最佳大小,并进行布局。而对于本范例而言,getPreferredSize的大小只和背景图片大小有关。
对于业务,尽量做到前台界面与后来业务分离。你可以自定义按钮动作监听器来实现,本例是沿用swing的Action实现,当鼠标抬起时,构造一个ActionEvent对象,然后交给Action成员的actionPerformed(ActionEvent e)处理。
Q4: java怎么做个简单按钮
你写的按钮计算吧,这个类是一个Applet,其中有一个按钮,这个类本身也是按钮的动作监听器,所以实现了ActionListener 接口用来给按钮调用(也就是 actionPerformed方法),其中的参数e是事件参数,当点击按钮时会发送给按钮使用。e.getSource() == b 就是如果点击是b这个按钮,当监听器给一个按钮使用时没有必要加此判断,e.getSource就是获取发生事件的源对象,比如
c = new JButton("点我有次数哦");
f.getContentPane().add(c);
c.setVisible(true);
c.addActionListener(this);
此时又增加了一个按钮,就可以用e.getSource() 判断点击的是哪一个按钮。
建议你把面向对象搞懂在学swing编程吧,很容易看懂的
Q5: java中如何设置按钮文字的大小、颜色和字体?
submit=newJButton("登陆");\x0d\x0a\x0d\x0asubmit.setFont(newFont("宋体",Font.PLAIN,16));\x0d\x0a三个参数分别表示:字体,样式(粗体,斜体等),字号\x0d\x0a\x0d\x0asubmit.setForeground(Color.RED);\x0d\x0a这个表示给组件上的文字设置颜色Color.RED表示红色\x0d\x0a当然你也可以自己给RGB的值比如submit.setForeground(newColor(215,215,200));\x0d\x0a\x0d\x0aJLabel组件支持HTML标记代码\x0d\x0ainfoLab=newJLabel("用户登陆系统",JLabel.CENTER);\x0d\x0a\x0d\x0a*注意:地址要单引号引起来。这个表示给用户登录系统几个字增加超链接\x0d\x0ainfoLab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));\x0d\x0a\x0d\x0a这个表示给这个文字添加鼠标样式,当鼠标移动到文字上,鼠标变成手型
java代码设置按钮样式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码设置按钮样式有哪些、java代码设置按钮样式的信息别忘了在本站进行查找喔。







