I am developing a Java Desktop Application. In it I have 4 JButtons on a JPanel. Now I want that whenever a button is clicked its background color changes to some other color (say orange) to represent that it has been clicked and the background color of all other 3 buttons reset to their default color (in case any of them had Orange background color).

我正在開發一個Java桌面應用程序。在一個JPanel上有4個JButtons。現在我想要的是,每當一個按鈕被點擊時,它的背景顏色就會改變為其他顏色(比如橙色)來表示它已經被點擊了,而其他3個按鈕的背景顏色都將重置為默認顏色(如果其中任何一個按鈕的背景顏色都是橙色的話)。

So, at one time only one button can have the orange color.

所以,一度只有一個按鈕可以有橙色。

The current approach that I have applied is that I have implemented the following code in the xxxActionPerformed() method of JButton button1

我應用的當前方法是在JButton button1的xxxActionPerformed()方法中實現了以下代碼

button1.setBackground(Color.Orange);button2.setBackground(Color.Gray);button3.setBackground(Color.Gray);button4.setBackground(Color.Gray);

and similarly for the rest three buttons.

其他三個按鈕也是如此。

Now in actual, I don't want the backgroud color as Gray (for unclicked button). Instead, I want the default background color so that the backgroud color will adjust itself to the look-and-feel of the GUI according to the end-user's platform's look-and-feel.

實際上,我不希望backgroud顏色為灰色(用於unclick按鈕)。相反,我希望使用默認的背景色,以便backgroud顏色能夠根據最終用戶平台的外觀調整到GUI的外觀。

Q1. How can I get the default background color?

Q1。如何獲得默認的背景色?

Q2. Is this the correct approach to do this or Is there any other mechanism through which I can group all the four buttons in a button group so that only one can have the specified property at one time (like radio buttons)?

Q2。這是正確的做法嗎?還是有其他機制可以將按鈕組中的所有四個按鈕分組,以便一次只能擁有指定的屬性(如單選按鈕)?

6 个解决方案

#1

9

just use null to use the default color:

使用null就可以使用默認的顏色:button1.setBackground(Color.ORANGE);button2.setBackground(null);...

consider using JToggleButtons with a ButtonGroup, set the Icon and PressedIcon of the buttons. No need to change the background color.

考慮使用帶有按鈕組的JToggleButtons,設置按鈕的圖標和PressedIcon。不需要改變背景顏色。button1 = new JToggleButton(new ImageIcon("0.jpg"));button1.setSelectedIcon(new ImageIcon("1.jpg"));button2 = new JToggleButton(new ImageIcon("0.jpg"));button2.setSelectedIcon(new ImageIcon("2.jpg"));...ButtonGroup group = new ButtonGroup();group.add(button1);group.add(button2);...

#2

8

You can get the standard background color for buttons from the UIManager:

你可以從UIManager上獲得按鈕的標准背景色:

button1.setBackground(UIManager.getColor("Button.background"));

As far as I know, the keys could change in different look & feels. Here is a nice webstart app that shows all available keys:

據我所知,鑰匙可以在不同的外觀和感覺上改變。這是一個很好的webstart應用程序,它顯示了所有可用的密鑰:

http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/

#3

1

Here's a cross-section of "Button.background" based on this example:

這是“按鈕”的橫截面。背景"基於這個例子:

*** Metal javax.swing.plaf.metal.MetalLookAndFeel 636 entriesButton.background: javax.swing.plaf.ColorUIResource[r=238,g=238,b=238]*** Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel 1052 entriesButton.background: com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel$NimbusProperty@60961dff*** CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel 550 entriesButton.background: javax.swing.plaf.ColorUIResource[r=174,g=178,b=195]*** Mac OS X com.apple.laf.AquaLookAndFeel 705 entriesButton.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]

#4

1

Q1.: To get the GUI color of the button, just do this

Q1。:要獲得按鈕的GUI顏色,只需執行以下操作

button1.setBackground(Color.Orange);button2.setBackground(java.awt.SystemColor.control);button3.setBackground(java.awt.SystemColor.control);button4.setBackground(java.awt.SystemColor.control);

With this class (java.awt.SystemColor.*), you can get the color of all elements of your user interface.

通過這個類(java.awt.SystemColor.*),您可以獲得用戶界面中所有元素的顏色。

Q2.: I've never heard about grouping buttons. Then, I can´t answer you this one.

Q2。我從未聽說過分組按鈕。然后,我可以´t回答你這個。

Hope it helps.

希望它可以幫助。

#5

0

If you wish you can redesign your whole button UI

如果你想重新設計你的整個按鈕UI

public class buttonUI extends javax.swing.plaf.basic.BasicButtonUI{buttonUI(JButton b){ b.setContentAreaFilled(false);}@Override public void paint(Graphics g,JComponent c){ Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); AbstractButton b = (AbstractButton) c; g2d.setFont(font.deriveFont(11f)); Dimension d = b.getSize(); FontMetrics fm = g2d.getFontMetrics(); g2d.setColor(new Color(100,100,100)); String caption = b.getText(); int x = (d.width - fm.stringWidth(caption)) / 2; int y = (d.height + fm.getAscent()) / 2 - 2; g2d.drawString(caption, x, y); } }

and in your main piece of code use like

在你的主要代碼中使用like。

jButton1.setUI(new buttonUI(jButton1));

This how I use it .. you can have your own way too

我就是這樣使用它的。你也可以有自己的方式

#6

-1

have you looked into the decorator pattern in java you pass a button into a method which decorates it depending on whether the button is the current active one, for example if it hovered over.

您是否查看過java中的decorator模式,您將一個按鈕傳遞到一個方法中,該方法根據按鈕是否是當前活動的按鈕而進行修飾,例如,如果它在上方停留。

public Jbutton decorateButton(JButton b, boolean isHoveredOver){ if(isHoveredOver) b.setBackground(getContentPane().getBackground().GREEN); return b;}

you call this method from the MouseEvent or ActionEvent methods and just issue a repaint() after. put all your buttons into an array or vector and loop through it passing each one in to the decorateButton method and give the initail boolean value of false then on event set it to true. This way the initial value is default and the button is then decorated upon action the buttons will appear to be acting as part of a group

您可以從MouseEvent或ActionEvent方法中調用這個方法,然后在后面發出repaint()。將所有的按鈕放入一個數組或向量中,並循環其中,將每個按鈕傳遞給decorateButton方法,並將initail布爾值設為false,然后將其設置為true。這樣,初始值是默認的,按鈕在操作時被修飾,按鈕將顯示為組的一部分