Java Swing中带有颜色的菜单[关闭]

问题描述 投票:-1回答:1
单击按钮时,我需要在按钮下方显示一个菜单,并且该菜单应包含下图所示的颜色。我尝试了多种方法来实现它,但是我的尝试是徒劳的。您可以为我提供建议或任何类似的解决方案吗?

enter image description here

java swing event-handling awt
1个回答
0
投票
在此,我尽力为您的问题设计示例设计。它很快,但我希望它能对您有所帮助。主要定义很明显,您可以从https://docs.oracle.com/javase/8/docs/api/index.html?javax/swing/package-summary.html中阅读更多内容

//Here the definition of frame. frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.getContentPane().setLayout(null); //Here the button for display or hide colors menu. JButton btnNewButton = new JButton("\uD83D\uDD8A \uD83E\uDC47"); btnNewButton.setBackground(SystemColor.control); btnNewButton.setBounds(326, 13, 70, 32); frame.getContentPane().add(btnNewButton); //Scrol Pane defined for multiple color buttons. JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(188, 45, 208, 140); frame.getContentPane().add(scrollPane); //Here the panel with Grid Layout defined for 3 columns. JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 3, 5, 5)); scrollPane.setViewportView(panel); //Here the buttons defined with related colors. new Color(R,G,B) >> in RGB format. JButton color1But = new JButton(""); color1But.setBackground(new Color(255,255,255)); JButton color2But = new JButton(""); color2But.setBackground(new Color(122,111,215)); JButton color3But = new JButton(""); color3But.setBackground(new Color(1,23,123)); JButton color4But = new JButton(""); color4But.setBackground(new Color(5,25,255)); JButton color5But = new JButton(""); color5But.setBackground(new Color(55,176,12)); JButton color6But = new JButton(""); color6But.setBackground(new Color(5,123,212)); JButton color7But = new JButton(""); color7But.setBackground(new Color(61,142,123)); JButton color8But = new JButton(""); color8But.setBackground(new Color(221,51,41)); JButton color9But = new JButton(""); color9But.setBackground(new Color(2,2,2)); //Here we add buttons to to our panel panel.add(color1But); panel.add(color2But); panel.add(color3But); panel.add(color4But); panel.add(color5But); panel.add(color6But); panel.add(color7But); panel.add(color8But); panel.add(color9But); //And here define the hide and show listener to our button. btnNewButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { //İf its visible set invisible else set invisible. if(scrollPane.isVisible()) scrollPane.setVisible(false); else scrollPane.setVisible(true); } });

© www.soinside.com 2019 - 2024. All rights reserved.