如何解决如何使用 InvokeLater 在 JFrame 中显示我的所有组件?
我正在尝试使用 JSplitPane 显示不同的 JPanel。
我使用 SwingUtilities.InvokeLater 来显示所有组件以避免 UI 缓慢。
但是在我调整窗口大小之前,底部的 JPanel 是 PinkPane 不会显示。
我尝试使用多个线程在底部显示 JPanel:
-我使用了 2-3 个线程。一个来初始化类。一种用于重新验证 splitPane
。最后一个是SwingUtilities.InvokeLater(new Runnable...)
(希望能解决问题)
这是我的代码:
package changing;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class splitPaneFrame {
private static JFrame mainFrame = new JFrame();
private static JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
private static JSplitPane middle = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
private int width = 1500;
private int height = 860;
public splitPaneFrame() {
// TODO Auto-generated constructor stub
mainFrame.setSize(width,height);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
initialization();
mainFrame.add(splitPane);
}
private static void initialization() {
JSplitPane mostRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel cyanPane = new JPanel();
cyanPane.setBackground(Color.CYAN);
JPanel orangePane = new JPanel();
orangePane.setBackground(Color.ORANGE);
mostRight.setDividerSize(0);
mostRight.setDividerLocation(400);
mostRight.setLeftComponent(cyanPane);
mostRight.setRightComponent(orangePane);
mostRight.setOneTouchExpandable(false);
JPanel yellowPane = new JPanel();
yellowPane.setBackground(Color.YELLOW);
yellowPane.setSize(new Dimension(600,800));
yellowPane.setMaximumSize(yellowPane.getSize());
yellowPane.setPreferredSize(yellowPane.getSize());
middle.setDividerSize(0);
middle.setDividerLocation(600);
middle.setLeftComponent(yellowPane);
middle.setRightComponent(mostRight);
middle.setOneTouchExpandable(false);
JPanel magentaPane = new JPanel();
magentaPane.setBackground(Color.MAGENTA);
JSplitPane mostLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mostLeft.setDividerLocation(250);
mostLeft.setDividerSize(0);
mostLeft.setLeftComponent(magentaPane);
mostLeft.setRightComponent(middle);
mostLeft.setOneTouchExpandable(false);
// vertically medium panes end here
////////////////////////////////////////////////////////////////////////////////
// Top and Bottom Horizontal Panes start here
// splitPane on the most bottom
JSplitPane bottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JPanel pinkPane = new JPanel();
pinkPane.setBackground(Color.PINK);
pinkPane.setSize(new Dimension(1500,30));
pinkPane.setPreferredSize(new Dimension(1500,30));
pinkPane.setMaximumSize(pinkPane.getPreferredSize());
bottom.setDividerLocation(800);
bottom.setDividerSize(0);
bottom.setTopComponent(mostLeft);
bottom.setBottomComponent(pinkPane);
bottom.setOneTouchExpandable(false);
bottom.setVisible(true);
pinkPane.setVisible(true);
JPanel redPane = new JPanel();
redPane.setBackground(Color.RED);
redPane.setSize(new Dimension(1500,30));
// main splitPane
splitPane.setDividerLocation(30);
splitPane.setDividerSize(0);
splitPane.setTopComponent(redPane);
splitPane.setBottomComponent(bottom);
splitPane.setOneTouchExpandable(false);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
splitPaneFrame frame = new splitPaneFrame();
}
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。