使用JSplitPane在java中向一个框架添加3个面板?
尝试使用JSplitPane将我创建的3个面板添加到Java框架中。我试过用2个面板,效果很好,但用3个面板仍然不能达到我想要的效果 我读过一些关于制作两个JSplitPane的文章,并将其中一个放在另一个中,但这并不是我想要它做的事情 我的代码显示有3个面板,但大小都不对。。应该填好 我的代码:使用JSplitPane在java中向一个框架添加3个面板?,java,swing,jframe,jpanel,jsplitpane,Java,Swing,Jframe,Jpanel,Jsplitpane,尝试使用JSplitPane将我创建的3个面板添加到Java框架中。我试过用2个面板,效果很好,但用3个面板仍然不能达到我想要的效果 我读过一些关于制作两个JSplitPane的文章,并将其中一个放在另一个中,但这并不是我想要它做的事情 我的代码显示有3个面板,但大小都不对。。应该填好 我的代码: frame = new JFrame(); // Create a new frame frame.setVisible(true); // Makes it visible
frame = new JFrame(); // Create a new frame
frame.setVisible(true); // Makes it visible
frame.setSize(900, 500); // Sets size
frame.setTitle(""); // Sets title
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Sets the window on the center of the screen
temp_panel = new JPanel(); // Creates new JPanel
water_panel = new JPanel(); // Creates new JPanel
power_panel = new JPanel(); // Creates new JPanel
temp_panel.setBackground(Color.decode("#2ecc71")); // Sets color
water_panel.setBackground(Color.decode("#3498db")); // Sets color
power_panel.setBackground(Color.decode("#f1c40f")); // Sets color
temp_label = new JLabel("This is Temperature");
water_label = new JLabel("This is Water consumption");
power_label = new JLabel("This is Power consumption");
// Add labels on panel
temp_panel.add(temp_label);
water_panel.add(water_label);
power_panel.add(power_label);
JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JSplitPane splitPaneRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneLeft.setLeftComponent( temp_panel );
splitPaneLeft.setRightComponent( water_panel );
splitPaneRight.setLeftComponent( splitPaneLeft );
splitPaneRight.setRightComponent( power_panel );
splitPaneLeft.setEnabled(false);
splitPaneLeft.setDividerSize(0);
splitPaneRight.setEnabled(false);
splitPaneRight.setDividerSize(0);
// put splitPaneRight onto a single panel
JPanel panelSplit = new JPanel();
panelSplit.add( splitPaneRight );
frame.add(panelSplit, BorderLayout.CENTER);
它应该是这样的,但是只有3个面板,3种不同的颜色,而不是2种
希望有人能提供帮助您可以使其中一个面板成为另一个JSplitPane,不幸的是,没有其他解决方案。如果您不需要在运行时更改组件的相对大小,请不要使用JSplitPane。相反,创建一个使用GridLayout的容器JPanel,比如说
newGridLayout(1,0)
对于1行和可变列数,使用JPanel将您的三个彩色JPanel添加到GridLayout,然后将其添加到JFrame中。为什么使用JSplitPane?为什么不将3个JPanel放到另一个使用GridLayout的JPanel中呢?你需要使用JSplitPane有什么特别的原因吗?我不知道你可以这么做。。。我马上就去试试。Thx你所说的相对尺寸是什么意思。。。我的程序将要更改,但它只会更改标签,标签会随着时间的推移而更改。它现在可以工作了。。我制作了另一个面板并使用了该面板上的GridLayout,现在我得到了3行:)thx man