我有一个简单的应用程序,它包含一个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并从用户那里获得前景色和背景色
答案 0 :(得分:4)
我认为您不能使用JOptionPane
,因为按下按钮后对话框会立即消失。
您可以在中心创建自己的JDialog
JColorChooser
,在底部设置Set Foreground
和Set Background
等按钮;这样您就不必调用2 JColorChooser
s:
<强>代码强>:
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();
}
}
}
}