Java 如何更改JMenuBar和JToolBar的背景色?
我想更改JMenuBar和JToolBar的背景色。为此,我试过了,但没有成功。我遵循了一些网站给出的解决方案。但是,这些也不能正常工作 这是我的密码:Java 如何更改JMenuBar和JToolBar的背景色?,java,swing,background-color,jmenubar,jtoolbar,Java,Swing,Background Color,Jmenubar,Jtoolbar,我想更改JMenuBar和JToolBar的背景色。为此,我试过了,但没有成功。我遵循了一些网站给出的解决方案。但是,这些也不能正常工作 这是我的密码: import java.awt.Color; public class JFrameDemo extends javax.swing.JFrame { public JFrameDemo() { Color b=new Color(0,150,255); initComponents(); menuBar.setFore
import java.awt.Color;
public class JFrameDemo extends javax.swing.JFrame {
public JFrameDemo() {
Color b=new Color(0,150,255);
initComponents();
menuBar.setForeground(Color.GREEN);
toolbar.setBackground(b);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
toolbar = new javax.swing.JToolBar();
close = new javax.swing.JButton();
open = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
jMenu3 = new javax.swing.JMenu();
jMenu4 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
toolbar.setRollover(true);
close.setText("close");
close.setFocusable(false);
close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
close.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
toolbar.add(close);
open.setText("open");
open.setFocusable(false);
open.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
open.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
toolbar.add(open);
menuBar.setBackground(new java.awt.Color(51, 51, 255));
jMenu3.setText("File");
menuBar.add(jMenu3);
jMenu4.setText("Edit");
menuBar.add(jMenu4);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(toolbar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 306, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// ImageIcon img = new ImageIcon("C:\\Icons\book-edit-icon.png");
JFrameDemo fdemo=new JFrameDemo();
fdemo.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton close;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar menuBar;
private javax.swing.JButton open;
private javax.swing.JToolBar toolbar;
// End of variables declaration
}
导入java.awt.Color;
公共类JFrameDemo扩展了javax.swing.JFrame{
公共JFrameDemo(){
颜色b=新颜色(0150255);
初始化组件();
菜单栏。设置前景(颜色。绿色);
(b)挫折背景;
}
@抑制警告(“未选中”)
//
私有组件(){
toolbar=newjavax.swing.JToolBar();
close=newjavax.swing.JButton();
open=newjavax.swing.JButton();
menuBar=newjavax.swing.JMenuBar();
jMenu3=newjavax.swing.JMenu();
jMenu4=newjavax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
toolbar.setRollover(true);
close.setText(“close”);
close.setFocusable(假);
close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
close.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
工具栏。添加(关闭);
open.setText(“open”);
open.setFocusable(假);
open.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
open.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
工具栏。添加(打开);
setBackground(新java.awt.Color(51,51255));
setText(“文件”);
菜单栏添加(jMenu3);
jMenu4.setText(“编辑”);
菜单栏添加(jMenu4);
setJMenuBar(菜单栏);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(工具栏,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT\u SIZE,400,Short.MAX\u值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(工具栏,javax.swing.GroupLayout.PREFERRED\u大小,25,javax.swing.GroupLayout.PREFERRED\u大小)
.addGap(0,306,短。最大值))
);
包装();
}//
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
@凌驾
公开募捐{
//ImageIcon img=新的ImageIcon(“C:\\Icons\book edit icon.png”);
JFrameDemo fdemo=新的JFrameDemo();
fdemo.setVisible(真);
}
});
}
//变量声明-不修改
私有javax.swing.JButton close;
私有javax.swing.JMenu jMenu3;
私有javax.swing.JMenu jMenu4;
私有javax.swing.JMenuBar菜单栏;
私有javax.swing.JButton打开;
私有javax.swing.JToolBar工具栏;
//变量结束声明
}
我想你的意思是它没有设置颜色。
我不知道他是否是一个挥杆高手,但我总是被告知要这样做:
public class BackgroundMenuBar extends JMenuBar
{
Color bgColor=Color.WHITE;
public void setColor(Color color)
{
bgColor=color;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(bgColor);
g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
然后使用JMenuBar的instend新类,并使用setColor设置颜色
例如:我想你的意思是它没有设置颜色。 我不知道他是否是一个挥杆高手,但我总是被告知要这样做:
public class BackgroundMenuBar extends JMenuBar
{
Color bgColor=Color.WHITE;
public void setColor(Color color)
{
bgColor=color;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(bgColor);
g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
然后使用JMenuBar的instend新类,并使用setColor设置颜色
示例取自:您是否尝试在JMenuBar上设置不透明(true)
您也可以这样做:
menuBar.setUI ( new BasicMenuBarUI (){
public void paint ( Graphics g, JComponent c ){
g.setColor ( YOUR_COLOR_HERE );
g.fillRect ( 0, 0, c.getWidth (), c.getHeight () );
}
} );
或一般而言:
UIManager.put ( "MenuBarUI", YOUR_SPECIFIC_UI_HERE );
您是否尝试在JMenuBar上设置不透明(true)
您也可以这样做:
menuBar.setUI ( new BasicMenuBarUI (){
public void paint ( Graphics g, JComponent c ){
g.setColor ( YOUR_COLOR_HERE );
g.fillRect ( 0, 0, c.getWidth (), c.getHeight () );
}
} );
或一般而言:
UIManager.put ( "MenuBarUI", YOUR_SPECIFIC_UI_HERE );
“工作不正常”是什么意思?设置一个nice。提示:添加@robzillaDev(或任何人,
@
很重要)以通知此人新的评论。什么是“工作不正常”?设置一个nice。提示:添加@robzillaDev(或任何人,@
很重要)通知此人一条新评论。我是通过使用NetBeans IDE创建JMenuBar和JToolBar的,因为我的项目不是通过IDE手动开发的。因此,我不知道如何使用上述类。请帮助我。在UI包中创建一个类(我知道你有这样的类),然后在其中使用menuBar=new javax.swing.JMenuBar();相反,menuBar=newbackgroundmenubar();然后选择menuBar.setColor(Color.YOUR\u Color\u CHOICH);诸如此类on@user3556256看上面好的,我知道了。但是,我用NetBeans IDE开发了这个程序。所以,IDE生成的源代码是不可编辑的。MenuBar也是IDE生成的代码。我如何修改它。谢谢你的回复…帮助me@user3556256我希望这不是真的-我自己不使用NetBeans,但是如果你不能编辑U