java - 如何使用 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();
}
});
}
}
解决方案
推荐阅读
- gremlin - Gremlin,其中查询不会使用标记顶点中的值
- c# - 在我的计算机上将 VM 客户端连接到服务器 C#
- c# - ComboBox.SelectedValue 返回 ToString() 而不是 Name 属性
- python - 如何在python中绘制具有不同X索引的多条线
- ios - Swift:如何从导航控制器过渡到标签栏控制器
- javascript - 如何使用 axios 和文件 URL 读取文件内容?
- python - 在python中使用步长计算滑动窗口
- jmeter - 在 JSR223 断言中定义一个值并从不同线程中的用户定义变量访问它
- r - 为什么会报错说找不到对象?
- reactjs - 如何从文件外部导入?