「java按钮设置边框颜色」java设置按钮背景颜色

博主:adminadmin 2022-12-06 19:24:07 97

本篇文章给大家谈谈java按钮设置边框颜色,以及java设置按钮背景颜色对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何改变按钮的颜色,不是背景的颜色

setForeground() 设置前景/字体颜色

setBackground() 设置背景颜色

具体实现:(假设按钮名称为:button)

设置红字:

button.setForeground(Color.red);

设置黑色背影:

button.setBackground(Color.black);

java怎么样改变窗口边框颜色?

边框跟所用的操作系统有关,你没办法改变。比如你改变系统主题的时候边框也会变的。

不过还是有办法实现你的要求的。你可以把窗口的边框隐藏了,然后自己创建三个按钮放到右上角就可以啦。分别用于__口X。

还有一种是使用JWindow代替JFrame。这个本来就没有标题栏。

示例如下:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JWindow;

public class Win extends JWindow{

public Win() {

setBounds(200,200,300,300);

setVisible(true);

}

public static void main(String a[]) {

new Win();

}

}

但还有个问题是这两种方法都不能再移动窗口啦,解决办法是写一个鼠标监听事件检测鼠标在按下和移动后所移动的方向和距离,然后相应的修改窗口的位置就可以啦。

java怎么改变窗体边框的颜色?

如果你想给窗口内部加上一个边框,可以在窗口内加一个Panel,设置Panel的边框就行。

如果你想修改操作系统提供的边框颜色,是做不到的,但是可以去掉系统提供的边框,重写paint方法自己模拟一个:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.LineBorder;

public class MyFrame {

public static void main(String[] args) {

JFrame frame1 = new JFrame();

frame1.setBounds(400, 300, 200, 200);

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

panel.setBorder(new LineBorder(Color.red));

frame1.add(panel);

frame1.setVisible(true);

JFrame frame2 = new JFrame() {

public void paint(Graphics g) {

super.paint(g);

Rectangle rect = this.getBounds();

int width = (int) rect.getWidth() - 1;

int height = (int) rect.getHeight() - 1;

g.setColor(Color.red);

g.drawRect(0, 0, width, height);

}

};

frame2.setBounds(650, 300, 200, 200);

frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame2.setUndecorated(true);

frame2.setVisible(true);

}

}

java改变按钮颜色

为yellow、blue、red3个按钮添加actionlistener,当按钮点击时执行setBackground(backgroundColor),同时执行 按钮.setBackground(backgroundColor)即可,比如:

JButton btnYellow = null;

JButton btnBlue = null;

JButton btnRed = null;

btnYellow.setActionCommand("yellow");

btnBlue.setActionCommand("blue");

btnRed.setActionCommand("red");

public void actionPerformed(ActionEvent e) {

String command = event.getActionCommand();

if( "yellow".equals(command) ){

setBackground(Color.yellow);

btnYellow.setBackground(Color.yellow);

}else if( "blue".equals(command) ){

setBackground(Color.blue);

btnBlue.setBackground(Color.blue);

}else if( "red".equals(command) ){

setBackground(Color.red);

btnRed.setBackground(Color.red);

}

}

写出了部分代码

java设置按钮边框

可以这样:

import java.awt.*;

public class Test extends Frame{

public void go(){

Button btn=new Button("带边框的按钮");

setLayout(new FlowLayout()); /*Frame的默认布局为BorderLayout,如果直接添加,则Button就会充满整个Frame,无法显示画的边框*/

add(btn);

setSize(100,100);

setVisible(true);

//必须将窗口显示出来,才能用Graphics画图,否则无效

Graphics g=getGraphics();

g.setColor(Color.RED);

Point p=btn.getLocation();

g.drawRect(p.x,p.y,btn.getSize().width,btn.getSize().height);

}

public static void main(String args[]){

new Test().go();

}

}

java按钮设置边框颜色的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java设置按钮背景颜色、java按钮设置边框颜色的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-06,除非注明,否则均为首码项目网原创文章,转载请注明出处。