【问题标题】:How can I put color in combo box JFrame switch case array?如何将颜色放在组合框 JFrame 开关盒数组中?
【发布时间】:2015-10-09 15:22:16
【问题描述】:

//这是其中颜色为dropbox类型的数组......

  private final String[] Options = new String[] {"Select Color", "RED" , "YELLOW", "BLUE" , "GREEN"};
       private final JComboBox cmb1 = new JComboBox (Options);

//这是switch case..如果我选择红色,整个框架都是红色的。 enter image description here

    private class ButtonHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

        // Get the selected value on the JCOmboBox
        String SelectedValue = cmb1.getSelectedItem().toString();

        // Display
        switch (SelectedValue){

//如果我选择红色,文本将显示在下面的框架中。我怎样才能让“红色”的框架变成红色??

        case "RED" :
            txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon"); break;

        case "YELLOW" :
            txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); break;

        case "BLUE" :
            txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire"); break;

        case "GREEN" :
            txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); break;

        default:
            txt1.setText("");

            }
        }
    }

这就是我所做的……想要插入颜色……


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ComboBoxColor extends JFrame {
    // Declare Combo Box with Values from Array
    private final String[] Options = new String[] { "Select Color", "RED",
            "YELLOW", "BLUE", "GREEN" };
    private final JComboBox cmb1 = new JComboBox(Options);

    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        itemStateChanged(evt);
    }

    // Declare JTextArea and wrap it in a JScrollPane
    private final JTextArea txt1 = new JTextArea(2, 20);
    private final JScrollPane scr = new JScrollPane(txt1);
    // ActionListener
    private final ButtonHandler BH = new ButtonHandler();
    public JComboBox combo;

    // Constructor
    public ComboBoxColor() {
        // Add ActionListener
        cmb1.addActionListener(BH);
        // Set Layout
        Container pane = this.getContentPane();
        pane.setLayout(new GridLayout(2, 4));
        // Add Components
        pane.add(cmb1);
        pane.add(scr);
        // Set JFrame Properties
        this.setTitle("COMBO BOX COLOR");
        this.setSize(500, 150);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    // ActionListener Sub-Class
    private class ButtonHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Get the selected value on the JCOmboBox
            String SelectedValue = cmb1.getSelectedItem().toString();
            // Display
            switch (SelectedValue) {
            case "RED":
                txt1.setText("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
                break;
            case "YELLOW":
                txt1.setText("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron");
                break;
            case "BLUE":
                txt1.setText("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
                break;
            case "GREEN":
                txt1.setText("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive");
                break;
            default:
                txt1.setText("");
            }
        }
    }

    // Main Method
    public static void main(String[] args) {
        ComboBoxColor CC = new ComboBoxColor();
    }
}

【问题讨论】:

  • 设置组件的背景色 'setBackground(Color)' 注意 setOpaque(true)
  • 谢谢...但是在哪里插入呢?
  • 对于example
  • 发布minimal reproducible example 这样我们就可以准确地看到什么都没发生,然后帮助您找出原因。
  • 当我运行程序时,没有出现颜色` - 你还没有发布MCVE,所以我们无法提供更多帮助。

标签: java swing combobox switch-statement


【解决方案1】:

您没有将文本字段组件添加到窗格。 这就是它没有改变的原因。

您必须先将组件添加到面板中,然后才能对其进行更改,使用..

pane.add(txt1);

这是工作源代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ComboBoxColor extends JFrame {
   // Declare Combo Box with Values from Array
   private final String[] Options = new String[] {"Select Color", "RED" ,"YELLOW", "BLUE" , "GREEN"};
   private final JComboBox<String>  cmb1 = new JComboBox<String>(Options);
   // Declare JTextArea and wrap it in a JScrollPane
   private final JTextArea txt1 = new JTextArea (2,20);
   private final JTextArea txt2 = new JTextArea (2,20);
   private final JScrollPane scr = new JScrollPane(txt1);
   // ActionListener
   private final ButtonHandler BH = new ButtonHandler();

   // Constructor
   public ComboBoxColor() {

  // Add ActionListener
  cmb1.addActionListener(BH);

  // Set Layout
  Container pane = this.getContentPane();
  pane.setLayout(new GridLayout (2,1));

  // Add Components
  pane.add(cmb1); pane.add(scr);

  //Here you didn't add textField Component to the pane
  pane.add(txt1);
  pane.add(txt2);


  // Set JFrame Properties
  this.setTitle("COMBO BOX COLOR");
  this.setSize(500,150);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);

  }    

  // ActionListener Sub-Class
  private class ButtonHandler implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
      // Get the selected value on the JCOmboBox
      String SelectedValue = cmb1.getSelectedItem().toString();
      // Display
      switch (SelectedValue){
          case "RED" :
              txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
              txt2.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
              txt1.setBackground(Color.red);
              txt2.setForeground(Color.red);
              break;

          case "YELLOW" :
              txt1.setBackground(Color.yellow);
              txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); 
              txt2.setForeground(Color.yellow);
              txt2.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); 
              break;
          case "BLUE" :
              txt1.setBackground(Color.blue);
              txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
              txt2.setForeground(Color.blue);
              txt2.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
              break;

          case "GREEN" :
              txt1.setBackground(Color.green);
              txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); 
              txt2.setForeground(Color.green);
              txt2.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); 
              break;

          default:
              txt1.setText("");
          }
      }
  }
 // Main Method
 public static void main (String[]args){
      ComboBoxColor CC = new ComboBoxColor();
 } 
}

希望问题现在得到解决。

【讨论】:

  • 为什么 ItemListener 应该比 ActionListener 工作得更好?
  • @Hovercraft ,这是stackoverflow.com/a/3499275/5364318问题的stackoverflow答案
  • @Hovercraft,您是否对答案投了反对票。但答案确实解决了上述问题。我在使用 actionPerformed 事件之前检查过它,但它不起作用,后来我使用了 itemStateChanged 事件,它也起作用了。
  • @HovercraftFullOfEels ,以及您在评论中提出的问题。因为我不太确定两者之间的区别。我在stackoverflow中检查了它。我得到了答案。你应该看看它。并运行代码并检查上述问题是否已解决。
  • @HovercraftFullOfEels ,因为您已经投了反对票。让我知道你做错了什么。
【解决方案2】:

这是我做的,我要插入颜色:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ComboBoxColor extends JFrame { 
    
    // Declare Combo Box with Values from Array
    private final String[] Options = new String[] {"Select Color", "RED" ,      "YELLOW", "BLUE" , "GREEN"};
        private final JComboBox cmb1 = new JComboBox (Options);
    
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            itemStateChanged(evt);
        }
    
    // Declare JTextArea and wrap it in a JScrollPane
    private final JTextArea txt1 = new JTextArea (2,20);
    private final JScrollPane scr = new JScrollPane(txt1);
    
    // ActionListener
    private final ButtonHandler BH = new ButtonHandler();
    public JComboBox combo;
    
    
    // Constructor
    public ComboBoxColor () {
        
        // Add ActionListener
        cmb1.addActionListener(BH);
        
        // Set Layout
        Container pane = this.getContentPane();
        pane.setLayout(new GridLayout (2,4));
        
        // Add Components
        pane.add(cmb1); pane.add(scr);
        
        
        // Set JFrame Properties
        this.setTitle("COMBO BOX COLOR");
        this.setSize(500,150);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        
    }
    
    // ActionListener Sub-Class
    private class ButtonHandler implements ActionListener {
        
        @Override
        public void actionPerformed(ActionEvent e) {
            
            // Get the selected value on the JCOmboBox
            String SelectedValue = cmb1.getSelectedItem().toString();
            
            // Display
            switch (SelectedValue){
            
            case "RED" :
                txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon"); break;
                            
            case "YELLOW" :
                txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); break;
                        
            case "BLUE" :
                txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire"); break;
            
            case "GREEN" :
                txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); break;
            
            default:
                txt1.setText("");
                
                }
            }

        
        }
        
    
    // Main Method
    public static void main (String[]args){
        ComboBoxColor CC = new ComboBoxColor ();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2012-12-24
    • 1970-01-01
    • 2022-01-10
    • 2011-10-13
    • 2017-02-02
    • 2020-05-22
    相关资源
    最近更新 更多