JComboBox に問題があります。JComboBox を持つ上部の JPanel と、いくつかの楕円形を描画できる BufferedImage を持つ別の JPanel が下にあります (マウス クリックのリスナーがあります)。ビデオでわかるように、問題は、JComboBox のオプションを変更しても、その変更が表示されないことがあることです。
たとえば、「ALL」を選択してから「L4」を選択すると、「L4」に変更されずに「ALL」が表示されたままになりますが、JComboBox をクリックすると、別の描画 JPanel またはデスクトップの別のウィンドウで、「L4」に変更されます(以前に変更されているはずです)。問題は、JPanel が JComboBox の下のマウス クリックをリッスンすることに関連していると思いますが、よくわかりません。
どうすれば修正できますか?
問題を示すために作成したビデオへのリンクは次のとおりです。 http://youtu.be/8Gg2Uq3SCYw
また、QuickTime Player でビデオを録画すると、正常に動作していなくても、すべて正常に動作して記録されるということも重要です (つまり、ビデオの内容はわかりますが (リンク)、QuickTime はそれを記録します)。正しく実行されていますが、非常に奇妙です...)
ここに、私が作成したEclipseプロジェクトの例へのリンクがあります。さまざまなオプションを何度も選択してみてください。私の言うことがわかります。 、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);
}
}
}
関連すると思われるすべての情報を投稿しました。さらに情報が必要な場合は、それを求めてください。