如何使用JButton?

时间:2014-03-27 06:05:16

标签: java swing colors jbutton

您好我正在尝试制作java桌面应用程序,我正在使用3个JButton。我希望当我点击任何JButton时它应该改变该按钮的颜色,如果我点击任何其他JButton,那么之前点击的按钮应该像之前一样,最近点击的按钮改变它的颜色直到另一个JButton被点击

我怎样才能实现这个目标

这是我的按钮代码

b1 = new JButton("Ok");
b1.setBounds(800, 725, 100, 40);
b1.setForeground(Color.BLACK);
c.add(b1);

b2 = new JButton("Print");
b2.setBounds(925, 725, 100, 40);
b2.setForeground(Color.BLACK);
c.add(b2);
b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
        b1.setBackground(Color.BLUE);
        JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
    }
});

b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
        b2.setBackground(Color.BLUE);
        JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
    }
});

2 个答案:

答案 0 :(得分:3)

这实际上比听起来更棘手。在渲染按钮时(例如Windows),某些外观不会使用background颜色属性

可能的解决方案可能是使用ButtonGroupJToggleButton,这样可以确保一次只选择一个按钮,并允许您监视按钮的选定状态。 ..

toggle

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ToggleButton {

    public static void main(String[] args) {
        new ToggleButton();
    }

    public ToggleButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JToggleButton b1 = new JToggleButton("Ok");
                b1.setContentAreaFilled(false);
                b1.setOpaque(true);
                b1.getModel().addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        if (b1.isSelected()) {
                            b1.setBackground(Color.BLACK);
                        } else {
                            b1.setBackground(null);
                        }
                    }
                });
                final JToggleButton b2 = new JToggleButton("Print");
                b2.setContentAreaFilled(false);
                b2.setOpaque(true);
                b2.getModel().addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        if (b2.isSelected()) {
                            b2.setBackground(Color.BLUE);
                        } else {
                            b2.setBackground(null);
                        }
                    }
                });

                ButtonGroup bg = new ButtonGroup();
                bg.add(b1);
                bg.add(b2);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(b1);
                frame.add(b2);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

另一种解决方案可能是使用JRadioButton代替,通常用于指示一组选项中的单个选择。

请参阅How to Use Buttons, Check Boxes, and Radio Buttons

答案 1 :(得分:0)

     b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
       b1.setBackground(Color.BLUE);
       b2.setBackground(Color.BLACK);
JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
}
});

    b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
       b2.setBackground(Color.BLUE);
      b1.setBackground(Color.BLACK);
JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
}
});