[英]Looking to add functionality to more than one button in the same panel in JFrame
[英]JFrame does not show more than one panel
我试图在Java中为大型项目创建一个简单的颜色选择器面板。 我有一个框架,该框架应该包括用于RGB滑块的面板,并且三个文本字段显示其值。 我能够毫无问题地添加滑块面板,但是当我尝试添加文本字段面板时,整个过程变得混乱了,所有面板都没有显示。 我唯一的问题是如何解决此问题的面板。 谢谢。
这是我的代码:
//importing necessary libraries
import java.awt.*;
import javax.swing.*;
//Object extends JFrame
public class FrameObject extends JFrame
{
//declaring the panels, one for the color sliders and the other for the text fields
private JPanel color_panel;
private JPanel textFileds;
//arrays to hold the J components for further efficiency
private JSlider[] RGB = new JSlider[3];
private JTextField[] RGBFileds = new JTextField[3];
public FrameObject()
{
//Preparing the frame
super("Color panel");
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//A grid layout to give desired orientation
color_panel = new JPanel(new GridLayout(3, 1));
textFileds = new JPanel(new GridLayout(3, 1));
//initializing the individual components through a loop in the arrays
for(int c=0; c<RGB.length; c++)
{
RGB[c] = new JSlider(SwingConstants.HORIZONTAL,0,255,100);
RGBFileds[c] = new JTextField(12);
//Adding each component to its specific panel
color_panel.add(RGB[c]);
textFileds.add(RGBFileds[c]);
}
//adding the sub panels to the main panel.
add(color_panel,BorderLayout.CENTER);
add(textFileds,BorderLayout.EAST);
}
}
public class FrameTest
{
public static void main(String[] args)
{
FrameObject f = new FrameObject();
}
}
在构造函数FrameObject
,您需要添加以下行。
this.pack();
打包方法调整框架的大小,以便其所有内容均等于或大于其首选大小。
您需要收拾行李架。
//importing necessary libraries
import java.awt.*;
import javax.swing.*;
//Object extends JFrame
public class FrameObject extends JFrame
{
//declaring the panels, one for the color sliders and the other for the text fields
private JPanel color_panel;
private JPanel textFileds;
//arrays to hold the J components for further efficiency
private JSlider[] RGB = new JSlider[3];
private JTextField[] RGBFileds = new JTextField[3];
public FrameObject()
{
//Preparing the frame
super("Color panel");
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//A grid layout to give desired orientation
color_panel = new JPanel(new GridLayout(3, 1));
textFileds = new JPanel(new GridLayout(3, 1));
//initializing the individual components through a loop in the arrays
for(int c=0; c<RGB.length; c++)
{
RGB[c] = new JSlider(SwingConstants.HORIZONTAL,0,255,100);
RGBFileds[c] = new JTextField(12);
//Adding each component to its specific panel
color_panel.add(RGB[c]);
textFileds.add(RGBFileds[c]);
}
//adding the sub panels to the main panel.
add(color_panel,BorderLayout.CENTER);
add(textFileds,BorderLayout.EAST);
pack();
}
public static void main(String[] args)
{
FrameObject f = new FrameObject();
}
}
采取setVisible(true);
并在建立用户界面后将其作为最后调用的内容
public FrameObject() {
//Preparing the frame
super("Color panel");
//setVisible(true);
//setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//...
//adding the sub panels to the main panel.
add(color_panel, BorderLayout.CENTER);
add(textFileds, BorderLayout.EAST);
pack();
setVisible(true);
}
在更新UI时,Swing很懒惰,它使您可以在“批处理”中添加和删除许多组件,而无需更新UI或执行新的布局遍历,这可能会很昂贵。
如果您需要动态更新UI,请记住在要更新UI时先调用revalidate
然后进行repaint
另外,与setSize
,首选pack
,因为pack
将考虑框架装饰以及字体规格的差异以及其他可能在平台和系统之间变化的因素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.