Java:随着 Swing 的变化控制嵌入式面板的宽度
我正在编写一个类来创建一个可以与一些文本一起显示的分数对象。我有一个黑色背景面板和两个顶部垂直方向的白色面板,它们之间有一个 3 像素间隔。每个白色面板都包含一个 JLabel。
这将创建分子、分母和分数条。当分子和分母长度相同(例如 1/2)时,一切正常。但是,一旦分子和分母的大小不同(例如 1/12),那么其中一个面板就会太小,并且两侧会变黑。我试图强制小面板占据背景面板的整个宽度,但似乎无法使其正常工作。这是我的班级。
public class MakeFraction2 {
private JLabel jlabelNum;
private JLabel jlabelDen;
JPanel mainpanel = new JPanel();
JPanel numpanel = new JPanel();
JPanel denpanel = new JPanel();
private Font font3 = new Font("Monospaced", Font.BOLD, 17);
private Font font4 = new Font ("SansSerif", Font.BOLD, 17);
MakeFraction2(){
jlabelNum = new JLabel("Num");
jlabelDen = new JLabel("Den");
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.Y_AXIS));
numpanel.setLayout(new BoxLayout(numpanel, BoxLayout.X_AXIS));
denpanel.setLayout(new BoxLayout(denpanel, BoxLayout.X_AXIS));
mainpanel.setBackground(Color.BLACK);
numpanel.setBackground(Color.WHITE);
denpanel.setBackground(Color.WHITE);
numpanel.add(jlabelNum);
denpanel.add(jlabelDen);
mainpanel.add(numpanel);
mainpanel.add(Box.createRigidArea(new Dimension(1,2)));
mainpanel.add(denpanel);
}
public void setnumden(String num, String den) {
mainpanel.setBackground(Color.BLACK);
jlabelNum.setText(num);
jlabelDen.setText(den);
sizepanels();
}
void sizepanels(){
int width = mainpanel.getWidth();
int h1 = numpanel.getHeight();
int h2 = denpanel.getHeight();
int height = Math.max(h1, h2);
numpanel.setSize(width,height);
denpanel.setSize(width,height);
}
}
I'm writing a class to create a fraction object that I can display along with some text. I have a black background panel and two white panels on top oriented vertically with a 3 pixel spacer between them. Each white panel contains a JLabel.
This creates the numerator, denominator, and fraction bar. Everything works fine when the numerator and denominator are the same length (for example 1/2). But once the numerator and denominator are different sizes (for example 1/12) then one of the panels is too small and gets black along the sides. I'm trying to force the small panel to take up the full width of the background panel but can't seem to get this to work right. Here is my class.
public class MakeFraction2 {
private JLabel jlabelNum;
private JLabel jlabelDen;
JPanel mainpanel = new JPanel();
JPanel numpanel = new JPanel();
JPanel denpanel = new JPanel();
private Font font3 = new Font("Monospaced", Font.BOLD, 17);
private Font font4 = new Font ("SansSerif", Font.BOLD, 17);
MakeFraction2(){
jlabelNum = new JLabel("Num");
jlabelDen = new JLabel("Den");
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.Y_AXIS));
numpanel.setLayout(new BoxLayout(numpanel, BoxLayout.X_AXIS));
denpanel.setLayout(new BoxLayout(denpanel, BoxLayout.X_AXIS));
mainpanel.setBackground(Color.BLACK);
numpanel.setBackground(Color.WHITE);
denpanel.setBackground(Color.WHITE);
numpanel.add(jlabelNum);
denpanel.add(jlabelDen);
mainpanel.add(numpanel);
mainpanel.add(Box.createRigidArea(new Dimension(1,2)));
mainpanel.add(denpanel);
}
public void setnumden(String num, String den) {
mainpanel.setBackground(Color.BLACK);
jlabelNum.setText(num);
jlabelDen.setText(den);
sizepanels();
}
void sizepanels(){
int width = mainpanel.getWidth();
int h1 = numpanel.getHeight();
int h2 = denpanel.getHeight();
int height = Math.max(h1, h2);
numpanel.setSize(width,height);
denpanel.setSize(width,height);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 GridLayout (2 行 1 列)并将标签放在那里。
另请参阅 JEditorPane http://java-sl.com/fraction_view.html 中的分数示例
I would suggest to use GridLayout (2 rows 1 column) and place the labels there.
Also see the fraction example in JEditorPane http://java-sl.com/fraction_view.html