当我运行它时,会显示一个空标题栏。我只是希望能够从那里看到组件和工作,但没有显示任何内容。该对话框旨在允许用户通过移动滑块选择颜色,然后返回到主页面的颜色。
import java.awt.*;
import javax.swing.*;
public class ColourDialog extends JDialog
{
String colorNames[] = {"Red: ", "Green: ", "Blue: "};
Label labels[] = new Label[3];
JSlider slider[]= new JSlider[3];
Label lb;
static ColourDialog d;
public void ColourDialog()
{
setModal(true);
Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel sliderPanel = new JPanel();
sliderPanel.setLayout(new GridLayout(0, 1));
for (int i = 0; i < slider.length; i++)
{
labels[i] = new Label(colorNames[i] + 255);
sliderPanel.add(labels[i]);
slider[i] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
slider[i].setMinorTickSpacing(10);
slider[i].setMajorTickSpacing(50);
slider[i].setPaintTicks(true);
slider[i].setPaintLabels(true);
sliderPanel.add(slider[i]);
//slider[i].addChangeListener(this);
}
lb = new Label("Colour");
c.add(sliderPanel, BorderLayout.CENTER);
c.add(lb, BorderLayout.SOUTH);
setSize(500, 450);
setLocation(200,200);
setTitle("Colour Dialog");
}
public static Color showDialog()
{
if (d == null)
d = new ColourDialog();
d.show();
//return new Color(red,green,blue);
return new Color(0,0,0);
}
public static void main(String args[])
{
ColourDialog.showDialog();
}
}
答案 0 :(得分:2)
我认为您已查看JColorChooser,此JComponent
可以返回选定的Color
在那里,我无法为JSlider
找出正确的定义和初始化修改
从使用公共静态Color showDialog()的扩展JDialog结尾开始有很多错误,它返回带有ColourDialog()初始化的空容器拼写错误
import java.awt.*;
import javax.swing.*;
public class ColourDialog {
private JDialog dialog = new JDialog();
private String colorNames[] = {"Red: ", "Green: ", "Blue: "};
private Label labels[] = new Label[3];
private JSlider slider[] = new JSlider[3];
private Label lb;
public ColourDialog() {
JPanel sliderPanel = new JPanel();
sliderPanel.setLayout(new GridLayout(0, 1));
for (int i = 0; i < slider.length; i++) {
labels[i] = new Label(colorNames[i] + 255);
sliderPanel.add(labels[i]);
slider[i] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
slider[i].setMinorTickSpacing(10);
slider[i].setMajorTickSpacing(50);
slider[i].setPaintTicks(true);
slider[i].setPaintLabels(true);
sliderPanel.add(slider[i]);
}
lb = new Label("Colour");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.add(sliderPanel, BorderLayout.CENTER);
dialog.add(lb, BorderLayout.SOUTH);
dialog.pack();
dialog.setLocation(200, 200);
dialog.setTitle("Colour Dialog");
dialog.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ColourDialog colourDialog = new ColourDialog();
}
});
}
}
答案 1 :(得分:1)
我认为这可能是因为你说“public void ColourDialog()”这是一个无效的构造函数。尝试摆脱“虚空”,然后再试一次。
答案 2 :(得分:0)
您永远不会调用ColorDialog()方法。这是一个提到“使用小写字母开始方法”的好地方。要修复代码:
变化:
d = new ColourDialog();
要:
d = new ColourDialog();
d.ColourDialog();