gpt4 book ai didi

java - 如何使用组合框更改花朵的颜色? (java)

转载 作者:行者123 更新时间:2023-12-01 13:48:25 26 4
gpt4 key购买 nike

我只希望我的花朵的花瓣变色。我已经让它与文本字段一起使用,但我认为组合框会更好(所以我的讲师知道他可以选择哪些颜色)。我看到了一些示例,他们会将组合框中的选择显示在文本字段中,但我不确定如何对我的花执行此操作。

简而言之,如何让组合框改变花瓣的颜色以及数组会更好(以及为什么/不)?

图片-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
}

最佳答案

So in short, how do I get the combobox to change the color of my flower petals and would an array be better(and why/not)?

是的,显然。 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();

}
});

我称上述解决方案很垃圾。如果你采用上述方法运行程序,实际上可以直接使用chosenColor实例在paintComponent函数中进行绘制。每当您从colorCombo中选择一种颜色时,您都会看到程序现在以所选颜色项进行响应。但该项目将在组合框中显示为:java.awt.Color[r=xxx,g=xxx,b=xxx],这可能是您不喜欢的。

了解 Color Chooser 怎么样?并利用它。

关于java - 如何使用组合框更改花朵的颜色? (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com