gpt4 book ai didi

java - JScrollPane 内容在滚动时不刷新

转载 作者:行者123 更新时间:2023-11-30 03:09:36 25 4
gpt4 key购买 nike

我正在创建一个简单的 GUI 选项,其中包含 JScrollPanel。

问题是当我滚动 JScrollPanel 内 JPanel 的所有内容时,它不会像这样刷新: Bad Refresh Scrolling

另一个问题是,并非所有文本字段都绘制得很好,并且组合框字段位于第一个文本字段后面。

这是我的代码:

  • 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);

内容传播得很好(至少在这个例子中)。

谢谢你

朱利安

最佳答案

基本问题是,您使用的是java.awt.TextField,它是轻量级容器内的重量级组件。这只是自找麻烦,他们往往不能很好地配合。

相反,使用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();

关于java - JScrollPane 内容在滚动时不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33899843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com