java 定位输出,JFRAME上的JAVA定位标签

在创建水平柱状图时遇到标签显示不全的问题,原因为默认使用了BorderLayout。解决方案是切换到GridLayout布局管理器,以便正确地显示所有标签。通过设置GridLayout并添加JLabels,成功解决了标签显示的问题。
摘要由CSDN通过智能技术生成

I need to draw a horizontal histogram, and i am setting up the labels of the histogram as follows,

CODE

public static void drawVertical(){

JFrame frame = new JFrame("Horizontal Histogram");

frame.setSize(300, 300);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label_01=new JLabel("0-29");

label_01.setAlignmentX(-290);

label_01.setAlignmentY(290);

JLabel label_02=new JLabel("30-39");

label_02.setAlignmentX(-290);

label_02.setAlignmentY(270);

JLabel label_03=new JLabel("40-69");

label_03.setAlignmentX(-290);

label_03.setAlignmentY(250);

JLabel label_04=new JLabel("70-100");

label_04.setAlignmentX(-290);

label_04.setAlignmentY(230);

frame.add(label_01);

frame.add(label_02)

frame.add(label_03);

frame.add(label_04);

}

But this is the output i get :(

OUTPUT

uPbU0.png

And this is my expected output (Edited with MS paint),

Expected Output

h0AnL.png

Can anyone figure out whats wrong here?

Why arent the other labels being displayed?

解决方案

The answer to this question is that you should not use a BorderLayout (which the JFrame uses by default), but instead use a GridLayout. This will allow you to just add the JLabels to your JFrame. An example looks like this:

EventQueue.invokeLater(() -> {

JFrame frame = new JFrame("Stackoverflow | Question");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame.setSize(300, 300);

frame.setLocationRelativeTo(null);

// This is the important line. This will Change the layout to a GridLayout.

frame.setLayout(new GridLayout(4, 1));

frame.add(new JLabel("0-29"));

frame.add(new JLabel("30-39"));

frame.add(new JLabel("40-69"));

frame.add(new JLabel("70-100"));

frame.setVisible(true);

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值