我有一个包含JButton和JColorChooser的简单应用程序。
当我按下按钮2时,将显示JColorChoosers。一个允许我选择背景色,第二个允许我选择前景色。
我将每种颜色保存在单独的变量中,这是我的代码:
public class Slide extends JFrame{
Color bgColor;
JButton colorButton=new JButton();
JColorChooser colorPicker=new JColorChooser();
public Slide(){
JPanel panel=new JPanel();
panel.setLayout(new MigLayout("", "[][][][][]", "[][][][][][]"));
this.setContentPane(panel);
colorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//1st color chooser for background color
bgColor=JColorChooser.showDialog(null, "Background color", null);
//2nd colorchooser appears for foreground
Color fgColor=JColorChooser.showDialog(null, "Foreground color", null);
colorButton.setBackground(bgColor);
colorButton.setForeground(fgColor);
}
});
colorButton.setText("Pick a color");
panel.add(colorButton, "cell 0 5");
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
new Slide();
}
}
现在我的问题是:是否可以最小化JColorChooser的部分,我想只显示一次JColorChooser并从用户那里获得前景色和背景色
最佳答案
我认为您不能使用JOptionPane
,因为在按下包含选项的按钮后对话框将立即消失。
您可以创建自己的JDialog
,在其中央有一个JColorChooser
,在底部有一些按钮,例如Set Foreground
和Set Background
;这样,您不必调用2个JColorChooser
:
码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example {
public Example() {
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
CustomColorChooserDialog dialog = new CustomColorChooserDialog(button);
dialog.setVisible(true);
}
});
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
}
class CustomColorChooserDialog extends JDialog {
JComponent targetComponent;
JColorChooser colorChooser;
JButton backgroundButton;
JButton foregroundButton;
JButton okButton;
public CustomColorChooserDialog(JComponent targetComponent) {
this.targetComponent = targetComponent;
colorChooser = new JColorChooser();
ButtonActionListener listener = new ButtonActionListener();
backgroundButton = new JButton("Set Background");
backgroundButton.addActionListener(listener);
foregroundButton = new JButton("Set Foreground");
foregroundButton.addActionListener(listener);
okButton = new JButton("OK");
okButton.addActionListener(listener);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(backgroundButton);
buttonPanel.add(foregroundButton);
buttonPanel.add(okButton);
getContentPane().add(colorChooser, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
pack();
setModal(true);
setLocationRelativeTo(targetComponent);
}
private class ButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(backgroundButton)) {
targetComponent.setBackground(colorChooser.getColor());
} else if (e.getSource().equals(foregroundButton)) {
targetComponent.setForeground(colorChooser.getColor());
} else if (e.getSource().equals(okButton)) {
dispose();
}
}
}
}
关于java - 使用JColorChooser设置JButton的前景色和背景色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33984266/