gpt4 book ai didi

java - Swing Ui 倍增面板重影

转载 作者:行者123 更新时间:2023-11-29 06:55:55 25 4
gpt4 key购买 nike

最近我的 swing ui 有问题。一切正常,直到我从 JButton 触发工具提示。之后将鼠标移到 UI 的其余部分会导致奇怪的伪像和故障。

窃听:

enter image description here

我无法显示整个代码,因为它太多了,但我在这里初始化按钮:

    GridBagConstraints bottompane_gbc = new GridBagConstraints();
toggleTorConnectionButton = new JButton();
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
toggleTorConnectionButton.setIcon(new ImageIcon(ResourceHandler.Menueicon3_1));
toggleTorConnectionButton.setMinimumSize(new Dimension(removeFinishedDownloads.getMinimumSize().width, toggleTorConnectionButton.getIcon().getIconHeight()+5));

toggleTorConnectionButton.addActionListener(); // unimportant
bottompane_gbc.gridy = 1;
bottompane_gbc.fill = GridBagConstraints.BOTH;
bottompane_gbc.insets = new Insets(0,15,10,5);
bottompane.add(ToggleTorConnectionButton,bottompane_gbc);


this.add(bottompane,BorderLayout.PAGE_END);

如果有人需要更多信息来帮助我,请随时询问。我有点绝望。 XD

编辑:经过一些修补我猜测问题与 swing 和我对它的使用有关。目前我使用了很多 Eventlisteners(这很糟糕吗?),这可能会减慢 awt 线程?以下是 HPROF 的简短摘录: http://www.pastebucket.com/96444

编辑 2:我能够在一个方便且简单的示例中重新创建错误。当您移到按钮上时,等待工具提示,然后移到 ui 上。您会看到重影 :(。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class Main_frame {

public static void main(String[] args) {
new Main_frame();
}

public Main_frame() {
JFrame frame = new JFrame("LOL");
frame.setFocusable(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(new Dimension(400, 500));
frame.setLocationRelativeTo(null);

Download_window download_window = new Download_window();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Download", null, download_window, "Main Download Window.");

for (int i = 0; i < 5; i++) {
JPanel pane = new JPanel();
Dimension dim = new Dimension(370, 60);
pane.setPreferredSize(dim);
pane.setMaximumSize(dim);
pane.setBackground(Color.blue);
pane.setMinimumSize(dim);
download_window.jobpanel.add(pane);
}
download_window.jobpanel.repaint();
download_window.jobpanel.revalidate();

frame.add(tabbedPane);
frame.setVisible(true);
}

public class Download_window extends JPanel {

JPanel jobpanel;

public Download_window() {
this.setLayout(new BorderLayout());

jobpanel = new JPanel();
jobpanel.setLayout(new BoxLayout(jobpanel, BoxLayout.Y_AXIS));

JPanel bottompane = new JPanel();
bottompane.setPreferredSize(new Dimension(385, 40));

JButton toggleTorConnectionButton = new JButton();
toggleTorConnectionButton.setPreferredSize(new Dimension(100, 50));
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");

bottompane.add(toggleTorConnectionButton);

this.add(bottompane, BorderLayout.PAGE_END);
JScrollPane jobScrollPane = new JScrollPane(jobpanel);
jobScrollPane.getVerticalScrollBar().setUnitIncrement(16);
this.add(jobScrollPane, BorderLayout.CENTER);

}
}
}

编辑 3:关于 trashgods 的想法,我使用了 EventDispatchThread,我修改了 setter 以覆盖 getter 的大小,我通过使用 trashgods 代码消除了不兼容性并且它工作正常......那么实际的区别在哪里?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class Main_frame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Main_frame();
}
});
}

public Main_frame() {
JFrame frame = new JFrame("LOL");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 500));

Download_window download_window = new Download_window();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Download", null, download_window, "Main Download Window.");
frame.add(tabbedPane);

frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public class Download_window extends JPanel {

JPanel jobpanel;

public Download_window() {
this.setLayout(new BorderLayout());

jobpanel = new JPanel();
jobpanel.setLayout(new BoxLayout(jobpanel, BoxLayout.Y_AXIS));
for (int i = 0; i < 5; i++) {
JPanel pane = new JPanel(){
@Override
public Dimension getPreferredSize() {
return new Dimension(370, 60);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(370, 60);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(370, 60);
}
};
pane.setBackground(Color.blue);
jobpanel.add(pane);
}

JPanel bottompane = new JPanel(){
@Override
public Dimension getPreferredSize() {
return new Dimension(385, 40);
}
};

JButton toggleTorConnectionButton = new JButton("Button"){
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 30);
}
};
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
bottompane.add(toggleTorConnectionButton);
this.add(bottompane, BorderLayout.PAGE_END);


JScrollPane jobScrollPane = new JScrollPane(jobpanel);
jobScrollPane.getVerticalScrollBar().setUnitIncrement(16);
this.add(jobScrollPane, BorderLayout.CENTER);

}
}
}

谁能亲自验证一下这种奇怪的行为?您只需要在 Edit3 中复制并粘贴上面的代码即可。

最佳答案

在我的平台上运行时,您的代码没有出现上述任何故障。

  • 确认您没有绘画问题,例如忽略 super.paintComponent()正如所讨论的here .

  • 确认您没有驱动程序不兼容问题,如讨论的那样here .

  • 构建和修改 event dispatch thread 上的所有 GUI 对象.

  • 不要使用 set[Preferred|Maximum|Minimum]Size()当你真的想覆盖 get[Preferred|Maximum|Minimum]Size() 时,正如所讨论的here .下面的示例覆盖了 getPreferredSize()在滚动 Pane 上,但您可以实现 Scrollable ,正如所讨论的here .

image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

/** @see https://stackoverflow.com/a/34319260/230513 */
public class MainFrame {

private static final int H = 64;

public static void main(String[] args) {
EventQueue.invokeLater(() -> new MainFrame());
}

public MainFrame() {
JFrame frame = new JFrame("LOL");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
for (int i = 0; i < 8; i++) {
panel.add(new DownloadPanel());
}
JScrollPane jsp = new JScrollPane(panel) {
@Override
public Dimension getPreferredSize() {
return new Dimension(6 * H, 4 * H);
}
};
tabbedPane.addTab("Download", null, jsp, "Main Download Window.");
tabbedPane.addTab("Options", null, null, "Options");
frame.add(tabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private static class DownloadPanel extends JPanel {

JPanel jobPanel = new JPanel();

public DownloadPanel() {
this.setLayout(new BorderLayout());
this.setBackground(Color.lightGray);
JProgressBar jpb = new JProgressBar();
jpb.setIndeterminate(true);
this.add(jpb);
JPanel buttonPane = new JPanel();
JButton toggleTorConnectionButton = new JButton("Button");
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
buttonPane.add(toggleTorConnectionButton);
this.add(buttonPane, BorderLayout.WEST);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(4 * H, H);
}
}
}

关于java - Swing Ui 倍增面板重影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316000/

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