从JComboBox控件重绘

时间:2012-03-05 22:52:17

标签: java swing graphics plot jcombobox

当用户选择JComboBox中的各种选项时,我试图让绘制的图形刷新/重新填充。我在网上找到的例子都使用JLabel,这可能适用于图像文件,但不适用于paintComponent生成的自定义图形。

我尝试在下面的~60行代码中推出自己的解决方案。我正在使用一个简单的矩形示例来重新调整大小。如果编译并运行下面的代码,当用户从JComboBox中选择不同的选项时,您将看到它不会重新绘制。另外,我故意还没有对displayConstraints做任何事情,因为如果有人有更好的方法,我不想强​​加解决方案。我的目标是让JComboBox显示在顶部的自己的行中,并且绘制的图形将在第一行下面的更大的第二行中完成。第二行将吸收所有调整大小的更改,而第一行在调整JFrame大小时将保持大致相同的大小。通过从JComboBox中选择不同的选项,用户将能够使绘制的矩形相对于JFrame的当前大小变小或变大。

任何人都可以告诉我如何修复下面的代码,以便完成上述目标吗?

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ComboBox extends JFrame implements ItemListener {
final String[] sizes = { "10%", "20%", "33%" };
JComboBox combobox = new JComboBox(sizes);
int selectedIndex;

public ComboBox() {
    setLayout(new GridBagLayout());
    combobox.setSelectedIndex(-1);
    combobox.addItemListener(this);
    GridBagConstraints comboBoxConstraints = new GridBagConstraints();
    comboBoxConstraints.gridx = 0;
    comboBoxConstraints.gridy = 0;
    comboBoxConstraints.gridwidth = 1;
    comboBoxConstraints.gridheight = 1;
    comboBoxConstraints.fill = GridBagConstraints.NONE;
    add(combobox,comboBoxConstraints);//This should be placed at top, in middle.

    GridBagConstraints displayConstraints = new GridBagConstraints();
    displayConstraints.gridx = 0;
    displayConstraints.gridy = 1;
    displayConstraints.gridwidth = 1;
    displayConstraints.gridheight = 1;
    displayConstraints.fill = GridBagConstraints.BOTH;
    //I am aware that nothing is done with displayConstraints.
    //I just want to indicate that the rectangle should go below the combobox,
    //and that the rectangle should resize while the combobox should not.
    //Other suggested approaches are welcome.

    setSize(300, 300);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] args) {new ComboBox();}

public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        JComboBox combo = (JComboBox) e.getSource();
        selectedIndex = combo.getSelectedIndex();
        System.out.println("selectedIndex is: "+selectedIndex);
        repaint();
    }
}
protected void paintComponent(Graphics g){
    int scaleFactor = 1;
    if(selectedIndex==0){scaleFactor = 10;}
    if(selectedIndex==1){scaleFactor = 5;}
    if(selectedIndex==2){scaleFactor = 3;}
    if(selectedIndex!=-1){
        int xStart = (getWidth()/2)-(getWidth()/scaleFactor);
        int yStart = (getHeight()/2)-(getHeight()/scaleFactor);
        g.drawRect(xStart, yStart, (getWidth()/scaleFactor), (getHeight()/scaleFactor));
    }
}
}

2 个答案:

答案 0 :(得分:3)

你不应该直接在JFrame中绘制,即使你这样做,JFrame也没有paintComponent方法。使用@Override注释自己查看。相反,在JPanel或JComponent中绘制,在paintComponent中进行绘制,并确保使用覆盖注释正确覆盖该方法。

例如:

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

public class ComboBoxTest extends JPanel implements ItemListener {
   private static final int PREF_W = 300;
   private static final int PREF_H = PREF_W;
   final String[] sizes = { "10%", "20%", "33%" };
   JComboBox combobox = new JComboBox(sizes);
   int selectedIndex;
   private double scaleFactor = 1;

   public ComboBoxTest() {
      setLayout(new GridBagLayout());
      combobox.setSelectedIndex(-1);
      combobox.addItemListener(this);
      GridBagConstraints comboBoxConstraints = new GridBagConstraints();
      comboBoxConstraints.gridx = 0;
      comboBoxConstraints.gridy = 0;
      comboBoxConstraints.gridwidth = 1;
      comboBoxConstraints.gridheight = 1;
      comboBoxConstraints.fill = GridBagConstraints.NONE;
      add(combobox, comboBoxConstraints);// This should be placed at top, in
                                         // middle.

      GridBagConstraints displayConstraints = new GridBagConstraints();
      displayConstraints.gridx = 0;
      displayConstraints.gridy = 1;
      displayConstraints.gridwidth = 1;
      displayConstraints.gridheight = 1;
      displayConstraints.fill = GridBagConstraints.BOTH;
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public void itemStateChanged(ItemEvent e) {
      if (e.getStateChange() == ItemEvent.SELECTED) {
         JComboBox combo = (JComboBox) e.getSource();
         selectedIndex = combo.getSelectedIndex();
         System.out.println("selectedIndex is: " + selectedIndex);
         if (selectedIndex == -1) {
            return;
         }
         String selectedItem = combo.getSelectedItem().toString().trim();
         selectedItem = selectedItem.replace("%", "");
         scaleFactor = Double.parseDouble(selectedItem) / 100.0;
         repaint();
      }
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
         int xStart = (getWidth() / 2) - (int)(getWidth() * scaleFactor);
         int yStart = (getHeight() / 2) - (int)(getHeight() * scaleFactor);
         g.drawRect(xStart, yStart, (int)(getWidth() * scaleFactor),
               (int)(getHeight() * scaleFactor));
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("ComboBoxTest");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ComboBoxTest());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:0)

离开我的头顶,没有测试这是我想出的。创建一个子类JPanel,覆盖paintComponent你做图形的地方(就像Hovercraft Full Of Eels所说的那样)。添加到JFrame。现在,在您的组合框中添加ActionListener(而不是ItemListener):

public void actionPerformed(ActionEvent evt) {
    //do other stuff...
    yourGraphicsPanel.repaint();
}

yourGraphicsPanel是JPanel子类的一个实例。

希望它有所帮助! :)