我对 JComboBox 有疑问。我有一个带有 JComboBox 的上部 JPanel 和下面的另一个带有 BufferedImage 的 JPanel,我可以在其中绘制一些椭圆(它有一个鼠标点击的侦听器)。正如您将在视频中看到的那样,问题是有时当我更改 JComboBox 中的选项时,它不会显示该更改。
例如,如果我选择“ALL”,然后选择“L4”,则“ALL”保持显示而不是更改为“L4”,但是当我单击 JComboBox、另一个绘图 JPanel 或桌面的另一个窗口时,它更改为“L4”(之前应该已经更改)。我认为问题与让 JPanel 监听 JComboBox 下方的鼠标点击有关,但我不确定。
谁能告诉我该如何解决?
这是我为显示问题而制作的视频的链接:http: //youtu.be/8Gg2Uq3SCYw
同样重要的是,当我使用 QuickTime Player 录制视频时,即使没有正常工作,所有内容都可以正常录制(我的意思是我看到了视频中的内容(链接),但 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);
}
}
}
我已经发布了所有我认为相关的信息,如果有人需要更多信息,请询问。