JComboBox, неправильная перерисовка после выбора - Java
Формулировка задачи:
Добрый день. Столкнулся вот с такой проблемой: после выбора объекта в JComboBox он неправильно перерисовывается.
Пример:
После выбора элемента и переходе, скажем уже на JSpinner, этот JComboBox рисуется и там.
Использовал стандартный редактор форм в NetBeans.
Собственно почему так происходит? и как решать?
Решение задачи: «JComboBox, неправильная перерисовка после выбора»
textual
Листинг программы
- import java.awt.Color;
- public class JMainFrame extends javax.swing.JFrame {
- public JMainFrame() {
- initComponents();
- // Радио-буттоны в группу
- buttonGroup1.add(jRadioButton_Epicycloid);
- buttonGroup1.add(jRadioButton_Hypocycloid);
- jRadioButton_Hypocycloid.setSelected(true);
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- buttonGroup1 = new javax.swing.ButtonGroup();
- jRadioButton_Epicycloid = new javax.swing.JRadioButton();
- jRadioButton_Hypocycloid = new javax.swing.JRadioButton();
- label1 = new java.awt.Label();
- label2 = new java.awt.Label();
- label3 = new java.awt.Label();
- label4 = new java.awt.Label();
- jSpinner_scale = new javax.swing.JSpinner();
- jSpinner_k = new javax.swing.JSpinner();
- jSpinner_R = new javax.swing.JSpinner();
- jSpinner_r = new javax.swing.JSpinner();
- button_Print = new java.awt.Button();
- jComboBox1 = new javax.swing.JComboBox<>();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setName("MainFrame"); // NOI18N
- setResizable(false);
- setSize(new java.awt.Dimension(468, 148));
- jRadioButton_Epicycloid.setText("Эпициклоида");
- jRadioButton_Hypocycloid.setText("Гипоциклоида");
- label1.setText("Масштаб");
- label2.setText("k");
- label3.setText("R");
- label4.setText("r");
- jSpinner_scale.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.0f), Float.valueOf(1.0f), Float.valueOf(50.0f), Float.valueOf(0.1f)));
- jSpinner_k.setModel(new javax.swing.SpinnerNumberModel(1, 1, 200, 1));
- jSpinner_k.setEnabled(false);
- jSpinner_R.setModel(new javax.swing.SpinnerNumberModel(1, 1, 200, 1));
- jSpinner_r.setModel(new javax.swing.SpinnerNumberModel(1, 1, 200, 1));
- button_Print.setLabel("Нарисовать");
- button_Print.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- button_PrintActionPerformed(evt);
- }
- });
- jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "black", "red", "green", "yellow" }));
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jRadioButton_Hypocycloid)
- .addComponent(jRadioButton_Epicycloid)
- .addGroup(layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(21, 21, 21)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addGap(40, 40, 40)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGap(29, 29, 29)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jSpinner_scale, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jSpinner_k, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jSpinner_R, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jSpinner_r, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(10, 10, 10)
- .addComponent(button_Print, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(16, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addComponent(jRadioButton_Hypocycloid)
- .addGap(7, 7, 7)
- .addComponent(jRadioButton_Epicycloid)
- .addGap(17, 17, 17)
- .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(10, 10, 10)
- .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(10, 10, 10)
- .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(10, 10, 10)
- .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(11, 11, 11)
- .addComponent(jSpinner_scale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(10, 10, 10)
- .addComponent(jSpinner_k, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(11, 11, 11)
- .addComponent(jSpinner_R, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(11, 11, 11)
- .addComponent(jSpinner_r, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(11, 11, 11)
- .addComponent(button_Print, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(17, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void button_PrintActionPerformed(java.awt.event.ActionEvent evt) {
- Color color;
- try {
- java.lang.reflect.Field field = Color.class.getField((String)jComboBox1.getModel().getSelectedItem());
- color = (Color)field.get(null);
- } catch (Exception e) {
- color = null; // Not defined
- }
- JPaintFrame pFrame = new JPaintFrame(
- (int)jSpinner_k.getValue(),
- (int)jSpinner_R.getValue(),
- (int)jSpinner_r.getValue(),
- (float)jSpinner_scale.getValue(),
- color,
- jRadioButton_Epicycloid.isSelected(),
- jRadioButton_Hypocycloid.isSelected()
- );
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see [url]http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html[/url]
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(JMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(JMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(JMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(JMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new JMainFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.ButtonGroup buttonGroup1;
- private java.awt.Button button_Print;
- private javax.swing.JComboBox<String> jComboBox1;
- private javax.swing.JRadioButton jRadioButton_Epicycloid;
- private javax.swing.JRadioButton jRadioButton_Hypocycloid;
- private javax.swing.JSpinner jSpinner_R;
- private javax.swing.JSpinner jSpinner_k;
- private javax.swing.JSpinner jSpinner_r;
- private javax.swing.JSpinner jSpinner_scale;
- private java.awt.Label label1;
- private java.awt.Label label2;
- private java.awt.Label label3;
- private java.awt.Label label4;
- // End of variables declaration
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д