2015-11-24 2 views
1

Я создаю простой набор опций, содержащий JScrollPanel.Содержимое JScrollPane не обновляется во время прокрутки

Проблема заключается в том, когда я прокрутке все содержание JPanel внутри моей JScrollPanel не обновляется так: Bad Refresh Scrolling

Другая проблемы в том, что не все мои текстовые поля хорошо расписано и поля COMBOBOX находятся за первым текстовым полем.

Вот мой код:

  • OptionsPanel.java источник проблемы

    package scroll; 
    
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.TextEvent; 
    import java.awt.event.TextListener; 
    
    import javax.swing.JPanel; 
    import javax.swing.JScrollPane; 
    
    import scroll.NavigationButtons.Navigation; 
    
    public class OptionsPanel extends JPanel implements ActionListener, TextListener { 
    
        private static final long serialVersionUID = 3800714599366218432L; 
    
        private NavigationButtons navBtn; 
    
        private ChangeOptionCB language; 
    
        private ChangeOptionTF option1; 
    
        private ChangeOptionTF option2; 
    
        private ChangeOptionTF option3; 
    
        private ChangeOptionTF option4; 
    
        private ChangeOptionTF option5; 
    
        private ChangeOptionTF option6; 
    
        private ChangeOptionTF option7; 
    
        private JScrollPane scrollPane; 
    
        public OptionsPanel() { 
         super(); 
    
         GridBagLayout layout = new GridBagLayout(); 
         setLayout(layout); 
    
         GridBagConstraints constraint = new GridBagConstraints(); 
    
         JPanel optionsPanel = new JPanel(); 
         GridBagLayout l = new GridBagLayout(); 
         GridBagConstraints c = new GridBagConstraints(); 
    
         optionsPanel.setLayout(l); 
    
    
    
         String[] langues = {"en", "fr", "es"}; 
         language = new ChangeOptionCB("Languages", langues); 
         language.addTextListener(this); 
         c.gridx = 0; 
         c.gridy = 0; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(language, c); 
    
         option1 = new ChangeOptionTF("option 1"); 
         option1.addTextListener(this); 
         option1.setText("option 1"); 
         c.gridx = 0; 
         c.gridy = 1; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option1, c); 
    
         option2 = new ChangeOptionTF("option 2"); 
         option2.addTextListener(this); 
         option2.setText("option 2"); 
         c.gridx = 0; 
         c.gridy = 2; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option2, c); 
    
         option3 = new ChangeOptionTF("option 3"); 
         option3.addTextListener(this); 
         option3.setText("option 3"); 
         c.gridx = 0; 
         c.gridy = 3; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option3, c); 
    
         option4 = new ChangeOptionTF("option 4"); 
         option4.addTextListener(this); 
         option4.setText("option 4"); 
         c.gridx = 0; 
         c.gridy = 4; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option4, c); 
    
         option5 = new ChangeOptionTF("option 5"); 
         option5.addTextListener(this); 
         option5.setText("option 5"); 
         c.gridx = 0; 
         c.gridy = 5; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option5, c); 
    
         option6 = new ChangeOptionTF("option 6"); 
         option6.addTextListener(this); 
         option6.setText("option 6"); 
         c.gridx = 0; 
         c.gridy = 6; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option6, c); 
    
         option7 = new ChangeOptionTF("option 7"); 
         option7.addTextListener(this); 
         option7.setText("option 7"); 
         c.gridx = 0; 
         c.gridy = 7; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.anchor = GridBagConstraints.PAGE_START; 
         c.weightx = 1; 
         c.weighty = 0.1; 
         optionsPanel.add(option7, c); 
    
         scrollPane = new JScrollPane(optionsPanel); 
    
         constraint.gridx = 0; 
         constraint.gridy = 0; 
         constraint.fill = GridBagConstraints.BOTH; 
         constraint.weightx = 1; 
         constraint.weighty = 1; 
         add(scrollPane, constraint); 
    
         navBtn = new NavigationButtons(NavigationButtons.EXIT); 
         navBtn.addActionListener(this); 
         constraint.gridx = 0; 
         constraint.gridy = 1; 
         constraint.fill = GridBagConstraints.HORIZONTAL; 
         constraint.weightx = 1; 
         constraint.weighty = 0.25; 
         add(navBtn, constraint); 
    
        } 
    
        @Override 
        public void actionPerformed(ActionEvent ae) { 
         if (navBtn == ae.getSource()) { 
          int id = ae.getID(); 
          if (Navigation.EXIT.getId() == id) { 
           System.out.println("Get out !!"); 
          } 
         } 
        } 
    
        @Override 
        public void textValueChanged(TextEvent te) { 
         if (language == te.getSource()) { 
          System.out.println("The option as changed : "+language.getOption()); 
         } 
         if (option1 == te.getSource()) { 
          System.out.println("The option as changed : "+option1.getNewText()); 
         } 
         if (option2 == te.getSource()) { 
          System.out.println("The option as changed : "+option2.getNewText()); 
         } 
         if (option3 == te.getSource()) { 
          System.out.println("The option as changed : "+option3.getNewText()); 
         } 
         if (option4 == te.getSource()) { 
          System.out.println("The option as changed : "+option4.getNewText()); 
         } 
         if (option5 == te.getSource()) { 
          System.out.println("The option as changed : "+option5.getNewText()); 
         } 
         if (option6 == te.getSource()) { 
          System.out.println("The option as changed : "+option6.getNewText()); 
         } 
         if (option7 == te.getSource()) { 
          System.out.println("The option as changed : "+option7.getNewText()); 
         } 
         scrollPane.revalidate(); 
         scrollPane.repaint(); 
        } 
    
    } 
    
  • Главный класс

    package scroll; 
    
    import java.awt.Dimension; 
    import javax.swing.JFrame; 
    
    public class ScrollTest { 
    
        public static void main(String[] args) { 
    
         JFrame frame = new JFrame(); 
    
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.setResizable(false); 
         frame.setContentPane(new OptionsPanel()); 
         frame.setTitle("Scrool Test"); 
         frame.pack(); 
    
         Dimension dimension = new Dimension(691, 263); 
         frame.setSize(dimension); 
         frame.setPreferredSize(dimension); 
         frame.setLocationRelativeTo(null); 
    
         frame.pack(); 
         frame.setVisible(true); 
        } 
    } 
    
  • Различные вещи, которые вам нужно:

    package scroll; 
    
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.TextEvent; 
    import java.awt.event.TextListener; 
    
    import javax.swing.JButton; 
    import javax.swing.JComboBox; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    import javax.swing.SwingConstants; 
    import javax.swing.event.EventListenerList; 
    
    public class ChangeOptionCB extends JPanel implements ActionListener { 
    
        private static final long serialVersionUID = 3355314012553851743L; 
        private JComboBox cb; 
        private JButton saveBtn; 
    
        private EventListenerList listeners; 
    
        public ChangeOptionCB(String label, String[] list) { 
    
         listeners = new EventListenerList(); 
    
         GridBagLayout layout = new GridBagLayout(); 
         GridBagConstraints c = new GridBagConstraints(); 
         setLayout(layout); 
    
         JLabel jLabel = new JLabel(label); 
         jLabel.setHorizontalAlignment(SwingConstants.LEFT); 
         c.fill = GridBagConstraints.LINE_START; 
         c.gridx = 0; 
         c.gridy = 0; 
         c.weightx = 0.1; 
         add(jLabel, c); 
    
         cb = new JComboBox(list); 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.gridx = 1; 
         c.gridy = 0; 
         c.weightx = 0.8; 
         add(cb, c); 
    
    
         saveBtn = new JButton("Save"); 
         saveBtn.addActionListener(this); 
    
         c.gridx = 2; 
         c.gridy = 0; 
         c.weightx = 0; 
         c.anchor = GridBagConstraints.LINE_END; 
         add(saveBtn, c); 
    
        } 
    
        @Override 
        public void actionPerformed(ActionEvent ae) { 
         if (ae.getSource() == saveBtn) { 
          fireTextAsChange(); 
         } 
        } 
    
        public String getOption() { 
         return (String) cb.getSelectedItem(); 
        } 
    
        public void addTextListener(TextListener listener) { 
         listeners.add(TextListener.class, listener); 
        } 
    
        public void removeActionListener(TextListener listener) { 
         listeners.remove(TextListener.class, listener); 
        } 
    
        private void fireTextAsChange(){ 
         TextListener[] listenerList = (TextListener[])listeners.getListeners(TextListener.class); 
    
         for(TextListener listener : listenerList){ 
          listener.textValueChanged(new TextEvent(this, 0)); 
         } 
        } 
    
    } 
    
    package scroll; 
    
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    import java.awt.TextField; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.TextEvent; 
    import java.awt.event.TextListener; 
    
    import javax.swing.JButton; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    import javax.swing.event.EventListenerList; 
    
    public class ChangeOptionTF extends JPanel implements ActionListener { 
    
        private static final long serialVersionUID = 3355314012553851743L; 
        private TextField tf; 
        private JButton saveBtn; 
    
        private EventListenerList listeners; 
    
        public ChangeOptionTF(String label) { 
    
         listeners = new EventListenerList(); 
    
         GridBagLayout layout = new GridBagLayout(); 
         GridBagConstraints c = new GridBagConstraints(); 
         setLayout(layout); 
    
         JLabel jLabel = new JLabel(label); 
         c.fill = GridBagConstraints.BOTH; 
         c.gridx = 0; 
         c.gridy = 0; 
         c.weightx = 1; 
         add(jLabel, c); 
    
         tf = new TextField(); 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.gridx = 0; 
         c.gridy = 1; 
         c.weightx = 0.5; 
         add(tf, c); 
    
    
         saveBtn = new JButton("Save"); 
         saveBtn.addActionListener(this); 
    
         c.gridx = 2; 
         c.gridy = 1; 
         c.weightx = 0; 
         c.anchor = GridBagConstraints.LINE_END; 
         add(saveBtn, c); 
    
        } 
    
        @Override 
        public void actionPerformed(ActionEvent ae) { 
         if (ae.getSource() == saveBtn) { 
          fireTextAsChange(); 
         } 
        } 
    
        public String getNewText() { 
         return tf.getText(); 
        } 
    
        public void addTextListener(TextListener listener) { 
         listeners.add(TextListener.class, listener); 
        } 
    
        public void removeActionListener(TextListener listener) { 
         listeners.remove(TextListener.class, listener); 
        } 
    
        private void fireTextAsChange(){ 
         TextListener[] listenerList = (TextListener[])listeners.getListeners(TextListener.class); 
    
         for(TextListener listener : listenerList){ 
          listener.textValueChanged(new TextEvent(this, 0)); 
         } 
        } 
    
        public void setText(String text) { 
         tf.setText(text); 
        } 
    
    } 
    
    package scroll; 
    
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    
    import javax.swing.JButton; 
    import javax.swing.JPanel; 
    import javax.swing.event.EventListenerList; 
    
    public class NavigationButtons extends JPanel implements ActionListener { 
    
        private static final long serialVersionUID = -4844499317626526067L; 
    
        public enum Navigation { 
         NEXT(1), CANCEL(0), EXIT(-1); 
    
         private int id; 
    
         private Navigation(int id) { 
          this.id = id; 
         } 
    
         public int getId() { 
          return id; 
         } 
        } 
    
        public static int NEXT_CANCEL = 0; 
        public static int CANCEL = 1; 
        public static int EXIT = 2; 
    
        private JButton cancel; 
        private JButton next; 
        private JButton exit; 
    
        private EventListenerList listeners; 
    
        public NavigationButtons(int type) { 
    
         listeners = new EventListenerList(); 
    
         GridBagLayout layout = new GridBagLayout(); 
         GridBagConstraints c = new GridBagConstraints(); 
         setLayout(layout); 
    
         if ((NEXT_CANCEL == type) || (CANCEL == type)) { 
          cancel = new JButton("Cancel"); 
          cancel.addActionListener(this); 
    
          c.gridwidth = 1; 
          c.anchor = GridBagConstraints.LINE_START; 
          c.weightx = 1; 
          add(cancel, c); 
         } 
    
         if (NEXT_CANCEL == type) { 
          next = new JButton("Next"); 
          next.addActionListener(this); 
    
          c.gridwidth = 1; 
          c.anchor = GridBagConstraints.LINE_END; 
          c.weightx = 0; 
          add(next, c); 
         } 
    
         if (EXIT == type) { 
          exit = new JButton("Exit"); 
          exit.addActionListener(this); 
    
          c.gridwidth = 1; 
          c.anchor = GridBagConstraints.LINE_END; 
          c.weightx = 1; 
          add(exit, c); 
         } 
    
        } 
    
        public void setNextEnable(boolean b) { 
         next.setEnabled(b); 
        } 
    
        @Override 
        public void actionPerformed(ActionEvent ae) { 
         if (ae.getSource() == next) { 
          fireActionPerformed(Navigation.NEXT); 
         } 
         if (ae.getSource() == cancel) { 
          fireActionPerformed(Navigation.CANCEL); 
         } 
         if (ae.getSource() == exit) { 
          fireActionPerformed(Navigation.EXIT); 
         } 
        } 
    
        public void addActionListener(ActionListener listener) { 
         listeners.add(ActionListener.class, listener); 
        } 
    
        public void removeActionListener(ActionListener listener) { 
         listeners.remove(ActionListener.class, listener); 
        } 
    
        public void fireActionPerformed(Navigation nav){ 
         ActionListener[] listenerList = (ActionListener[])listeners.getListeners(ActionListener.class); 
    
         for(ActionListener listener : listenerList){ 
          listener.actionPerformed(new ActionEvent(this, nav.getId(), null)); 
         } 
        } 
    } 
    

Что не так в моем коде, что делает это уродливое обновление? Я не понимаю.

Возможно, мне нужно реализовать своего слушателя, который перерисовывает мой кадр при каждом прокрутке?

Более странные вещи, если я заменить

add(scrollPane, constraint); 

по

add(optionsPane, constraint); 

содержание получают хорошо (по крайней мере, в данном примере).

Спасибо ты

Julien

+2

Привет @MonkeyJLuffy, добро пожаловать в StackOverflow! Для быстрой помощи это помогает, если вы можете устранить фрагменты кода, не связанные с проблемой, которую вы испытываете (например, ActionListeners). Правильно ли я говорю, что проблема заключается в том, что после прокрутки часть раскрывающегося списка ComboBox скрывается за первым TextField? –

+0

Не удалось воспроизвести первую проблему, используя предоставленный код. Может воспроизвести вторую проблему. –

+0

@NickRippe Привет, Ник, спасибо, чтобы посмотреть на мою проблему. Я знаю, что мне нужно удалить много вещей, но в этом случае проблема была: /. Merci. Жюльен – MonkeyJLuffy

ответ

2

Основная проблема в том, что вы используете java.awt.TextField, который является тяжелым компонентом веса внутри Lighweight контейнера. Это просто вопрос о проблемах, они, как правило, не играют хорошо вместе.

Вместо этого используйте javax.swing.JTextField

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.TextEvent; 
import java.awt.event.TextListener; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.event.EventListenerList; 

public class ChangeOptionTF extends JPanel implements ActionListener { 

    private static final long serialVersionUID = 3355314012553851743L; 
    private JTextField tf; 
    private JButton saveBtn; 

    private EventListenerList listeners; 

    public ChangeOptionTF(String label) { 

     listeners = new EventListenerList(); 

     GridBagLayout layout = new GridBagLayout(); 
     GridBagConstraints c = new GridBagConstraints(); 
     setLayout(layout); 

     JLabel jLabel = new JLabel(label); 
     c.fill = GridBagConstraints.BOTH; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     add(jLabel, c); 

     tf = new JTextField(); 
+0

ДА! Это !! Большое спасибо. Когда я это сделал, я не обращаю внимания на эту мелочь. Ты помог мне. Хорошего дня :) – MonkeyJLuffy