我只想让花瓣变色。我已经得到它与文本字段一起工作,但我已经决定组合框会更好(所以我的讲师知道他可以选择哪种颜色)。 我看了几个例子,他们会在组合框中选择出现在文本区域,但我不知道如何用我的花做这件事。
简而言之,我如何让组合框改变花瓣的颜色,阵列会更好(以及为什么/不)?
图片 - http://s903.photobucket.com/user/Nicole_Pretorius/media/Flowers_zps936f6f40.jpg.html
框架代码:
package Versie4;
public class FrameFlowers extends javax.swing.JFrame {
public FrameFlowers() {
initComponents();
setSize(900, 600);
setContentPane(new PanelFlowers());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrameFlowers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameFlowers().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
专家组代码:
package Versie4;
import java.awt.Color;
import java.awt.Graphics;
public class PanelFlowers extends javax.swing.JPanel {
private int amount;
private String color = "";
public PanelFlowers() {
initComponents();
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int teller;
g.setColor(Color.RED); //flowerpot
g.fillRect(300, 350, 500, 100);
int x = 1;
for (teller=1; teller <= amount ;teller++) {
//Flower 1
g.setColor(Color.GREEN); //stem
g.fillRect(320 + x, 250, 10, 100);
switch (color) { //Colours of petals
case "red":
g.setColor(Color.red);
break;
case "blue":
g.setColor(Color.blue);
break;
case "yellow":
g.setColor(Color.yellow);
break;
case "orange":
g.setColor(Color.orange);
break;
case "pink":
g.setColor(Color.PINK);
break;
case "purple":
g.setColor(new Color(102, 0, 204));
break;
}
g.fillOval(304 + x, 190, 40, 40); //petals
g.fillOval(330 + x, 210, 40, 40);
g.fillOval(320 + x, 240, 40, 40);
g.fillOval(290 + x, 240, 40, 40);
g.fillOval(280 + x, 210, 40, 40);
g.setColor(Color.YELLOW); //pistil
g.fillOval(312 + x, 225, 25, 25);
x = teller * 80;
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lblamount = new javax.swing.JLabel();
txtamount = new javax.swing.JTextField();
lblcolor = new javax.swing.JLabel();
txtcolor = new javax.swing.JTextField();
btngrow = new javax.swing.JButton();
btnreset = new javax.swing.JButton();
colorCombo = new javax.swing.JComboBox();
lblamount.setText("Amount: ");
txtamount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtamountActionPerformed(evt);
}
});
lblcolor.setText("Color: ");
txtcolor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtcolorActionPerformed(evt);
}
});
btngrow.setText("Grow!");
btnreset.setText("Reset Size");
colorCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "red", "blue", "yellow", "orange", "pink", "purple" }));
colorCombo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
colorComboActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblamount)
.addGap(18, 18, 18)
.addComponent(txtamount, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(41, 41, 41)
.addComponent(lblcolor)
.addGap(18, 18, 18)
.addComponent(txtcolor, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)
.addComponent(btngrow)
.addGap(18, 18, 18)
.addComponent(btnreset))
.addGroup(layout.createSequentialGroup()
.addGap(211, 211, 211)
.addComponent(colorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(230, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblamount)
.addComponent(txtamount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblcolor)
.addComponent(txtcolor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btngrow)
.addComponent(btnreset))
.addGap(42, 42, 42)
.addComponent(colorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(350, Short.MAX_VALUE))
);
}// </editor-fold>
private void txtamountActionPerformed(java.awt.event.ActionEvent evt) {
amount = Integer.parseInt(txtamount.getText());
color = this.txtcolor.getText();
repaint();
}
private void txtcolorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void colorComboActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton btngrow;
private javax.swing.JButton btnreset;
private javax.swing.JComboBox colorCombo;
private javax.swing.JLabel lblamount;
private javax.swing.JLabel lblcolor;
private javax.swing.JTextField txtamount;
private javax.swing.JTextField txtcolor;
// End of variables declaration
}
答案 0 :(得分:1)
简而言之,我如何让组合框改变我的颜色 花瓣和阵列会更好(以及为什么/不)?
是的,很明显。 JComboBox
是用户输入控制组件。它允许用户在相同类型但不同品味的各种其他项目中选择项目。数组不是组件。它只是一个内部数据结构,我们可以使用它来维护(添加,保存,删除)项目,仅此而已。 JComboBox的模型称为DefaultComboBoxModel
,它在内部包含这样的数据结构并包含元素并为我们管理它们。
但是,您使用JComboBox
的方式会让您遇到麻烦,因为如果您从该组项目中选择一项,则需要使用if-else
或switch-case
检查寻找特定元素。显然你现在在做什么。作为一个简单(但相当垃圾)的解决方案:
public class PanelFlowers extends javax.swing.JPanel {
private int amount;
// private String color = "";
Color chosenColor;
public PanelFlowers() {
initComponents();
// repaint(); // <--- no need to call repaint in constructor
}
private void initComponents() {
// your other code------------
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new Color(152, 52, 152));
model.addElement(new Color(152, 12, 52));
model.addElement(new Color(152, 142, 120));
colorCombo.setModel(model);
colorCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
JComboBox comb = (JComboBox)e.getSource();
chosenColor = (Color) comb.getSelectedItem();
repaint();
}
});
我已经将上述解决方案称为junky。如果您通过采用上述方法运行程序,您实际上可以直接使用chosenColor
实例来绘制paintComponent
函数内部。每当您从colorCombo
中选择颜色时,您将看到现在程序以所选颜色项目进行响应。但该项目将在comboBox中显示为:java.awt.Color[r=xxx,g=xxx,b=xxx]
这可能是您不喜欢的。
如何了解Color Chooser并使用它。