英文:
JLabel cannot be aligned Vertically
问题
以下是翻译好的部分:
我知道这些问题有答案,但对我一点用都没有。
我尝试过:
/*
titleTextLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
titleTextLabel.setVerticalAlignment(SwingConstants.CENTER); 也尝试了 JLabel.CENTER
/*
但完全没有起作用。
如果有人想要检查,这里是完整的代码。
如果您认为问题出在 JFrame 方面,那不是的。没有地方会出问题(基本正常的东西)
此外,我还附加了这段代码的照片,以及应用程序的照片:https://i.stack.imgur.com/LKhku.jpg
package com.practice;
import javax.swing.*;
import java.awt.*;
public class Start extends JFrame {
// 在这里设置 frame 和带有 "start" 字样的初始面板
// J-objects 等
private JPanel titleTextPanel, titleButtonPanel;
private JLabel titleTextLabel;
private JButton titleButton;
Start(){
this.setSize(1000,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.getContentPane().setBackground(Color.darkGray);
setUpStart();
this.setVisible(true);
}
private void setUpStart(){
titleTextPanel=new JPanel();
titleTextPanel.setBounds(200,50,600,150);
titleTextLabel=new JLabel();
titleTextLabel.setFont(new Font("Arial",Font.PLAIN, 30));
titleTextLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setText("ASCENDING DARKNESS");
titleTextPanel.add(titleTextLabel);
this.add(titleTextPanel);
}
}
希望这对您有所帮助。
英文:
I am aware that they are answers to this questions but It doesnt work for me at all.
I tried :
/*
titleTextLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
titleTextLabel.setVerticalAlignment(SwingConstants.CENTER); also JLabel.CENTER
/*
But it didnt worked at all.
Here it is full code if someone wants to check .
If You think problem is on JFrame side it is not.There isnt a place to make failure (basic normal stuff)
Also I am adding photo of this code and also of application :https://i.stack.imgur.com/LKhku.jpg
package com.practice;
import javax.swing.*;
import java.awt.*;
public class Start extends JFrame {
//Tutaj ustawiamy frame i pierwsze panele z napisem start
// J-Obiekty itd
private JPanel titleTextPanel, titleButtonPanel;
private JLabel titleTextLabel;
private JButton titleButton;
Start(){
this.setSize(1000,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.getContentPane().setBackground(Color.darkGray);
setUpStart();
this.setVisible(true);
}
private void setUpStart(){
titleTextPanel=new JPanel();
titleTextPanel.setBounds(200,50,600,150);
titleTextLabel=new JLabel();
titleTextLabel.setFont(new Font("Arial",Font.PLAIN, 30));
titleTextLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
titleTextLabel.setText("ASCENDING DARKNESS");
titleTextPanel.add(titleTextLabel);
this.add(titleTextPanel);
}
}
答案1
得分: 1
你有一些问题,首先为什么要有第二个JPanel?JLabel没有在JPanel中居中显示,因为它使用了流式布局。
你可以不使用第二个JPanel,直接使用JLabel。
private void setUpStart(){
titleTextLabel = new JLabel();
titleTextLabel.setBounds(200, 50, 600, 150);
titleTextLabel.setFont(new Font("Arial", Font.PLAIN, 30));
titleTextLabel.setHorizontalAlignment(JLabel.CENTER);
titleTextLabel.setVerticalAlignment(JLabel.CENTER);
titleTextLabel.setText("ASCENDING DARKNESS");
titleTextLabel.setOpaque(true);
titleTextLabel.setBackground(Color.WHITE);
this.add(titleTextLabel);
}
英文:
You have a couple issues, first why do you have the second JPanel? The JLabel is not getting centered in the JPanel because it has a flow layout.
Instead of using the second JPanel you can just use the JLabel.
private void setUpStart(){
titleTextLabel=new JLabel();
titleTextLabel.setBounds(200,50,600,150);
titleTextLabel.setFont(new Font("Arial",Font.PLAIN, 30));
titleTextLabel.setHorizontalAlignment(JLabel.CENTER);
titleTextLabel.setVerticalAlignment(JLabel.CENTER);
titleTextLabel.setText("ASCENDING DARKNESS");
titleTextLabel.setOpaque(true);
titleTextLabel.setBackground(Color.WHITE);
this.add(titleTextLabel);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论