Java 引入另一个组件
我有一个家庭作业要做,但没有足够的信息,我现在卡住了。。。问题是,我有三门课: ChoicePanel扩展JPanel并添加按钮,以使用JComboBox选择颜色Java 引入另一个组件,java,swing,jpanel,components,Java,Swing,Jpanel,Components,我有一个家庭作业要做,但没有足够的信息,我现在卡住了。。。问题是,我有三门课: ChoicePanel扩展JPanel并添加按钮,以使用JComboBox选择颜色 public class ChoicePanel extends JPanel{ Draw dessin; public ChoicePanel(Draw df) { dessin = df; ... // couleurs final JComboB
public class ChoicePanel extends JPanel{
Draw dessin;
public ChoicePanel(Draw df) {
dessin = df;
...
// couleurs
final JComboBox<String> couleurs = new JComboBox<>(new String[] {"bleu", "jaune", "rouge"});
add(couleurs);
}
/** Know what color is selected */
private Color determineCouleur(int indice){
switch (indice) {
case 0:
return Color.BLUE;
case 1:
return Color.YELLOW;
case 2:
return Color.RED;
default:
return Color.BLACK;
}
}
}
绘制延伸的JPanel并储存所有轮廓并绘制它们
Main创建包含这些类的框架
我必须将Draw设置为MouseMotionListener,但我无法在ChoixePanel中选择颜色,因为JComobox是在construcor中创建的,我无法将其设置为字段。
那么如何从Draw中检查ChoicePanel的按钮值呢
每个答案都会很有帮助 本练习的目标可能是帮助您学习Java语言。让我们重构所看到的示例以满足您的需求 ChoicePanel需要一种更新绘图面板实例的方法;您可以将引用作为参数传递给面板的构造函数或下面显示的工厂方法
public static JPanel create(Draw draw) {…}
在组合的动作侦听器中,设置绘制时的颜色;如果更改不是背景色等颜色,可以选择调用draw.repaint
Hue h = (Hue) colors.getSelectedItem();
draw.setBackground(h.getColor());
//draw.repaint();
由于Draw可能不包含任何组件,请按如下所示覆盖getPreferredSize
我无法创建私有类
为了方便运行下面的示例,ChoicePanel和Draw包含为私有静态成员。只需将每个类移动到它自己的文件(不包括),就可以从Main获得具有包私有访问权限的独立类
你将抽签传给ChoicePanel,并将其作为一块场地。该字段已经可以访问ChoicePanel的变量。此外,在发布HW问题时,最好告诉您可以在代码中更改哪些内容,不可以更改哪些内容,以及是否有不允许使用的类。例如,我不知道,那么如何从Draw中获取couleurs var?对于第二条评论:我不能在Draw中添加像Color这样的字段。此外,决断者必须保持私密,因为Couluers在ChoicePanel中,你不会从抽签中得到它。也许可以发布Draw的相关部分。谢谢@trahgod,我要试试这个:
JFrame f = new JFrame("Main");
Draw d = new Draw();
f.add(d, BorderLayout.CENTER);
f.add(ChoicePanel.create(d), BorderLayout.SOUTH);
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @see https://stackoverflow.com/a/5663782/230513
*/
public class Main {
private static class ChoicePanel {
public static JPanel create(Draw draw) {
JPanel p = new JPanel();
final JComboBox colors = new JComboBox();
for (Hue h : Hue.values()) {
colors.addItem(h);
}
colors.addActionListener((ActionEvent e) -> {
Hue h = (Hue) colors.getSelectedItem();
draw.setBackground(h.getColor());
});
p.add(colors);
return p;
}
}
private static class Draw extends JPanel {
public Draw() {
this.setBackground(Hue.values()[0].getColor());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
}
public enum Hue {
Cyan(Color.cyan), Magenta(Color.magenta), Yellow(Color.yellow),
Red(Color.red), Green(Color.green), Blue(Color.blue);
private final Color color;
private Hue(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
}
private void display() {
JFrame f = new JFrame("Main");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Draw d = new Draw();
f.add(d, BorderLayout.CENTER);
f.add(ChoicePanel.create(d), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Main().display();
});
}
}