JComboBox陷入困境

时间:2013-04-09 22:24:43

标签: java swing click jpanel jcombobox

我遇到了JComboBox的问题。我有一个带有JComboBox的上部JPanel和另一个带有BufferedImage的JPanel,我可以在其中绘制一些椭圆形(它有一个鼠标点击的监听器)。正如您将在视频中看到的那样,有时当我更改JComboBox中的选项时,它不会显示更改。

例如,如果我选择'ALL'然后我选择'L4','ALL'仍会显示而不是更改为'L4',但是当我点击JComboBox时,在另一个绘图JPanel中或在另一个窗口上在桌面上,它会变为'L4'(之前应该已更改)。我认为这个问题与JPAnel监听JComboBox下面的鼠标点击有关,但我不确定。

任何人都可以告诉我如何解决这个问题?

以下是我为展示问题所做的视频的链接:http://youtu.be/8Gg2Uq3SCYw

同样重要的是,当我用QuickTime播放器录制视频时,即使它没有正常工作,所有录制的也都正常工作(我的意思是我看到视频中的内容(链接)但是QuickTime记录它正确运行,非常奇怪......)

以下是我为您制作的示例eclipse项目的链接,尝试多次选择不同的选项,您会看到我说的话:https://app.sugarsync.com/iris/wf/D6421069_60887018_228038 看看CircuitTracePlotView.java的LapPanel,有JComboBox。

我在MacBookPro 10.8(Mountain Lion)上运行它

这是视图的代码(它也在示例的链接上):

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.image.BufferedImage;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;




public class CircuitTracePlotView extends JFrame {
    private static final long serialVersionUID = 5125304890914235274L;
    private int numberOfLaps;

    private CircuitTracePlot plot;
    private LapPanel lapPanel;

    public CircuitTracePlotView() {

        this.setVisible(false);

        this.plot = new CircuitTracePlot();
        this.lapPanel = new LapPanel();


        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(this.plot, BorderLayout.CENTER);
        this.getContentPane().add(this.lapPanel, BorderLayout.NORTH);
        this.pack();
        this.setLocationRelativeTo(null);       
    }

    public void init(CircuitTracePlotViewController circuitTracePlotViewController, int numberOfLaps) {
        this.setController(circuitTracePlotViewController);
        this.setNumberOfLaps(numberOfLaps);

        this.lapPanel.setLapSelection();
    }

    private void setController(CircuitTracePlotViewController circuitTracePlotViewController) {
        this.plot.init(circuitTracePlotViewController);
    }

    private int getNumberOfLaps() {
        return this.numberOfLaps;
    }

    public void setNumberOfLaps(int laps) {
        this.numberOfLaps = laps;
        System.out.println("NumberOfLaps" + this.numberOfLaps);
    }

    public void drawPoint(int x, int y) {
        this.plot.drawPoint(x, y);
        this.repaint();
    }

    public void drawLine(int x1, int y1, int x2, int y2) {
        this.plot.drawLine(x1, y1, x2, y2);
    }

    public Dimension getPlotDimension() {
        return this.plot.getPreferredSize();
    }

    //Drawing panel
    private class CircuitTracePlot extends JPanel {

        private static final long serialVersionUID = -7915054480476755069L;

        private final static short LINE = 0;
        private final static short OVAL = 1;


        private final static int OVALHEIGHT = 10;
        private final static int OVALWIDTH = 10;

        BufferedImage plot;
        Graphics2D plotGraphics;

        private int x1;
        private int x2;
        private int y1;
        private int y2;

        private int paintType;

        private CircuitTracePlot() {
            this.setBackground(Color.WHITE);

        }

        private void init(CircuitTracePlotViewController circuitTracePlotViewController) {
            this.addListeners(circuitTracePlotViewController);
            this.plot = (BufferedImage)this.createImage(this.getPreferredSize().width, this.getPreferredSize().height);
            this.plotGraphics = this.plot.createGraphics();
        }

        private void drawLine(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.paintType = LINE;
            this.repaint();
        }

        private void drawPoint(int x1, int y1) {
            this.x1 = x1;
            this.y1 = y1;
            this.paintType = OVAL;
            this.repaint();
        }


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            switch (this.paintType) {
            case LINE:
                plotGraphics.drawLine(x1, y1, x2, y2);
                g.drawImage(this.plot,0,0, this);
                break;
            case OVAL:
                plotGraphics.fillOval(x1 - OVALWIDTH/2, y1 - OVALHEIGHT/2, OVALWIDTH, OVALHEIGHT);          
                g.drawImage(this.plot,0,0, this);
                break;
            }

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(700, 500);
        }

        private void addListeners(CircuitTracePlotViewController circuitTracePlotViewController) {
            this.addMouseListener(circuitTracePlotViewController);
        }

    }

    private class LapPanel extends JPanel{

        private static final long serialVersionUID = -287427935273603789L;

        //private LinkedList<JIDButton> list_LapButtons;
        private JComboBox<String> JCB_Laps;

        private LapPanel() {

            this.setBorder(BorderFactory.createTitledBorder("Lap Selection"));
            //this.list_LapButtons = new LinkedList<JIDButton>();

            //JIDButton auxButton = new JIDButton(0, "<html><u>A</u>LL<html>");
            //this.list_LapButtons.add(auxButton);
            //this.add(auxButton);

            System.out.println("NumberOfLaps" + CircuitTracePlotView.this.numberOfLaps);


        }

        private void setLapSelection() {

            String[] auxLaps = new String[CircuitTracePlotView.this.getNumberOfLaps() + 1];

            auxLaps[0] = "<html>" + "<u>" + "A" + "</u>" +  "LL"  + "<html>";
            for (int i = 1; i <= CircuitTracePlotView.this.getNumberOfLaps(); i++) {
                auxLaps[i] = "<html>" + "L" + "<u>" +  i + "</u>" + "<html>";

            }
            this.JCB_Laps = new JComboBox<String>(auxLaps);


            this.add(JCB_Laps);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(700, 65);
        }

    }

}

如果有人需要更多信息,我已经公布了我认为相关的所有信息。

0 个答案:

没有答案