【发布时间】:2012-03-22 02:08:52
【问题描述】:
当我运行它时,会显示一个空的标题栏。我只想能够看到组件并从那里工作,但没有显示任何内容。该对话框旨在允许用户通过移动滑块来选择颜色,然后返回到主页面的颜色。
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();
}
}
【问题讨论】:
-
d.show()是做什么的? -
ColourDialog 扩展了 JDialog,所以 d.show() 使对话框可见,对吧?
-
JDialog API 中的评论说
.show()已弃用,您应该使用setVisiable(true) -
谢谢。我不认为这是问题的根源,因为我仍然看到一个空对话框,所以它是可见的。