1. 在java中如何設置按鈕背景顏色
Icon i=new ImageIcon("a.jpg");
JButton b=new JButton(i);
這里需要注意的是a.jpg的位置要放到你的項目文件夾下才行。
2. java中單擊按鈕背景變成藍色
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BackGroundChangeTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
BackFrame frame=new BackFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class BackFrame extends JFrame
{
private JPanel btnPanel;
public BackFrame(){
setTitle("backgrond");
setSize(600,400);
JButton btn=new JButton("changeblue");
/**
* 單擊是是背景為藍色
*/
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
btnPanel.setBackground(Color.blue);
}
});
btnPanel=new JPanel();
btnPanel.add(btn);
add(btnPanel);
}
}
3. java JButton 點擊按鈕 背景變色
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
publicclassStudyextendsJFrame{
JButtonbtn;
publicStudy(){
this.setTitle("111");
this.setSize(500,500);
setLayout(newFlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
finalContainerjp=this.getContentPane();
btn=newJButton("abc");
jp.add(btn);
btn.setBounds(100,100,100,100);
btn.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource().equals(btn)){
jp.setBackground(Color.gray);
}
}
});
}
publicstaticvoidmain(String[]args){
newStudy();
}
}
4. JAVA中JBUTTON的背景色問題
這個應該就是你要的效果....
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Exec1 extends JFrame {
public Exec1() {
init();
}
/**
* 初始化窗體
*/
private void init() {
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (Exception e) {
e.printStackTrace();
}
setLayout(null);
final JButton btnTest = new JButton("測試");
btnTest.setBounds(20, 30, 100, 25);
Border b = new LineBorder(Color.YELLOW, 1);
btnTest.setBorder(b);
add(btnTest);
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Exec1().setVisible(true);
}
}
=============
如果你想要那種的,就不能設lookandfeel了
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
因為你還沒了解這部分的機制。
當你改變lookAndFeel之後,java UI的控制項發生不少變化。就是說畫控制項的代碼都不是一套的了。你以後了解這部分只是就知道了。
==============
先給你個思路,看看你就明白了。
=========
你用上面的方法設置,你可以到getSystemLookAndFeelClassName函數里看看
當你的系統是windows那它會返回
com.sun.java.swing.plaf.windows.WindowsLookAndFeel,然而用他之後。你所有控制項的UI都是使用sun\java\swing\plaf\windows這下面的控制項,如:
WindowsButtonUI.java
WindowsButtonListener.java
WindowsCheckBoxMenuItemUI.java
WindowsCheckBoxUI.java 等等。
而你沒設lookandfeel之前使用swing默認的風格顯示的。它默認是:
使用javax.swing.plaf.basic.BasicLookAndFeel這個類。而控制項的UI就是用下面這些:
BasicButtonUI.java
BasicButtonListener.java
BasicCheckBoxMenuItemUI.java
BasicCheckBoxUI.java等等。其實你看看jdk源碼就知道了。
控制項UI就是顯示控制項樣子的
5. JAVA 中JButton的背景色問題
這個應該就是你要的效果.... 這里我一起答復了.....
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Exec1 extends JFrame {
public Exec1() {
init();
}
/**
* 初始化窗體
*/
private void init() {
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (Exception e) {
e.printStackTrace();
}
setLayout(null);
final JButton btnTest = new JButton("測試");
btnTest.setBounds(20, 30, 100, 25);
Border b = new LineBorder(Color.YELLOW, 1);
btnTest.setBorder(b);
add(btnTest);
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Exec1().setVisible(true);
}
}
=============
如果你想要那種的,就不能設lookandfeel了
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
因為你還沒了解這部分的機制。
當你改變lookAndFeel之後,java UI的控制項發生不少變化。就是說畫控制項的代碼都不是一套的了。你以後了解這部分只是就知道了。
==============
先給你個思路,看看你就明白了。
=========
你用上面的方法設置,你可以到getSystemLookAndFeelClassName函數里看看
當你的系統是windows那它會返回
com.sun.java.swing.plaf.windows.WindowsLookAndFeel,然而用他之後。你所有控制項的UI都是使用sun\java\swing\plaf\windows這下面的控制項,如:
WindowsButtonUI.java
WindowsButtonListener.java
WindowsCheckBoxMenuItemUI.java
WindowsCheckBoxUI.java 等等。
而你沒設lookandfeel之前使用swing默認的風格顯示的。它默認是:
使用javax.swing.plaf.basic.BasicLookAndFeel這個類。而控制項的UI就是用下面這些:
BasicButtonUI.java
BasicButtonListener.java
BasicCheckBoxMenuItemUI.java
BasicCheckBoxUI.java等等。其實你看看jdk源碼就知道了。
控制項UI就是顯示控制項樣子的
6. java點擊按鈕換背景顏色
改了一下:
packagemethod2;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.FlowLayout;
importjava.awt.Frame;
importjava.awt.Panel;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
{
Framef=newFrame();//f應聲明為屬性,才能在mousePressed()中可見
PanelP1=newPanel();//P1應聲明為屬性,才能在mousePressed()中可見
PanelP2=newPanel();//P2應聲明為屬性,才能在mousePressed()中可見
PanelP3=newPanel();//P3應聲明為屬性,才能在mousePressed()中可見
Buttonb1=newButton("紅色");//b1應聲明為屬性,才能在mousePressed()中可見
Buttonb2=newButton("藍色");//b2應聲明為屬性,才能在mousePressed()中可見
Buttonb3=newButton("黃色");//b3應聲明為屬性,才能在mousePressed()中可見
publicstaticvoidmain(Stringargs[]){
Changecolorct=newChangecolor();
ct.init();
}
publicvoidinit(){
//Framef=newFrame();
f.setLayout(newFlowLayout());
//PanelP1=newPanel();
//PanelP2=newPanel();
//PanelP3=newPanel();
//Buttonb1=newButton("紅色");
//Buttonb2=newButton("藍色");
//Buttonb3=newButton("黃色");
f.add(b1);
f.add(b2);
f.add(b3);
P1.setBackground(Color.red);
P2.setBackground(Color.blue);
P3.setBackground(Color.yellow);
P1.setVisible(true);
P2.setVisible(true);
P3.setVisible(true);
b1.addMouseListener(this);
b2.addMouseListener(this);
b3.addMouseListener(this);
f.setSize(300,300);
f.setVisible(true);
}//缺了個括弧
publicvoidmousePressed(MouseEvente2){
if(e2.getSource()==b1){//b1應聲明為屬性才可見
f.setBackground(Color.red);//f應聲明為屬性才可見。源代碼f.add(P1);給f添加一個背景色為紅色的Panel;根據原意,應設置f的背景色為紅色
}
if(e2.getSource()==b2){//b2應聲明為屬性才可見
f.add(P2);//f應聲明為屬性才可見
f.doLayout();//要讓添加的組件起作用,應該再調用doLayout()方法,因為f的layout變了
}
if(e2.getSource()==b3){//b3應聲明為屬性才可見
f.add(P3);
f.doLayout();//要讓添加的組件起作用,應該再調用doLayout()方法,因為f的layout變了
}
}
}
7. java swing中按鈕的顏色要怎麼設置
publicclassDemo{
publicstaticvoidmain(Stringargs[])throwsIOException{
ButtonDemodemo=newButtonDemo();
demo.view();
}
}
classButtonDemoextendsJFrame{
=1L;
publicvoidview(){
JPanelpanel=newJPanel();
Buttonbutton=newButton("ok");
button.setBackground(Color.RED);//設置顏色
panel.add(button);
add(panel);
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
8. 用java代碼改變xml文件中的button按鈕的背景顏色
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
classFrame1extendsJFrame//設計一個窗體類
{
JButtonjb1,jb2,jb3;//按鈕
JLabeljl;//標簽
Frame1()//構造方法
{
jb1=newJButton("紅色");//創建按鈕對象
jb2=newJButton("綠色");
jb3=newJButton("藍色");
//給jb1添加監聽事件
jb1.addActionListener(newActionListener(){
//覆寫actionPerformed方法
@Override
publicvoidactionPerformed(ActionEvente){
jl.setForeground(Color.RED);
}
});
//給jb2添加監聽事件
jb2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
jl.setForeground(Color.GREEN);
}
});
//給jb3添加監聽事件
jb3.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
jl.setForeground(Color.BLUE);
}
});
jl=newJLabel("設定標簽顏色");//創建標簽對象
jl.setFont(newFont("隸書",Font.PLAIN,32));//標簽字體
JPaneljp1=newJPanel();//面板1
JPaneljp2=newJPanel();//面板2
jp1.add(jl);//標簽放入面板1
jp2.add(jb1);//三個按鍵放入面板2
jp2.add(jb2);
jp2.add(jb3);
setLayout(newBorderLayout());//窗體設定為邊界布局
this.add(jp1,BorderLayout.NORTH);//標簽面板放在窗體上端
this.add(jp2,BorderLayout.CENTER);//按鈕面板放在窗體中間
setBounds(400,300,300,140);//設定窗體大小和位置
setTitle("【實驗10-1】座號25,王濤 ");//設定窗體標題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);//設定窗體可見
}
}
publicclassExperiment10_1{
publicstaticvoidmain(String[]args){
newFrame1();
}
}
9. java 單擊按鈕改變背景顏色
public void actionPerformed(ActionEvent evt) { if(evt.getActionCommand().equals("紅色"){((Button)evt.getSource()).setBackground(Color.red);} else if(evt.getActionCommand().equals("藍色"){((Button)evt.getSource()).setBackground(Color.blue);} else if(evt.getActionCommand().equals("黃色"){((Button)evt.getSource()).setBackground(Color.yellow);} }
記得採納啊
10. java如何改變按鈕的顏色,不是背景的顏色
setForeground() 設置前景/字體顏色
setBackground() 設置背景顏色
具體實現:(假設按鈕名稱為:button)
設置紅字:
button.setForeground(Color.red);
設置黑色背影:
button.setBackground(Color.black);