jmenu

Change background and text color of JMenuBar and JMenu objects inside it

雨燕双飞 提交于 2019-11-27 04:44:27
问题 How can I set custom background color for JMenuBar and JMenu objects inside it? I tried .setBackgroundColor and it does not work! 回答1: Create a new class that extends JMenuBar : 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); }

JMenu ActionListener

穿精又带淫゛_ 提交于 2019-11-26 09:57:02
问题 I was wondering if can you test to see if a JMenu (not JMenuItem) has been clicked. I tried adding an ActionListener to it but it doesn\'t seem to recognize it. I just need it to preform an action when the JMenu button is pressed so that I can change the JMenuItems for that menu befor it opens. All work arrounds to get this result are welcome too! Thanks 回答1: for JMenu use MenuListener code import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public