java中 t无法对齐,我無法在JAVA中重新排列文本

basically we've been taught the basics of Java but we weren't taught about the GUI in java. So for the past 2 days I've been trying to make my text appear from top to bottom without success. Can anyone help rearrange? I want the "registration number" to appear below "username". This is the code:

基本上我們已經學過Java的基礎知識但我們沒有在java中學過GUI。因此,在過去的兩天里,我一直試圖讓我的文字從上到下顯示而沒有成功。任何人都可以幫忙重新排列我希望“注冊號”顯示在“用戶名”下方。這是代碼:

package management;

import javax.swing.*;

import java.awt.*;

public class Library extends JFrame {

JFrame frame = new JFrame();

JPanel panel = new JPanel();

JLabel label1 = new JLabel();

JLabel label3 = new JLabel();

JLabel label2 = new JLabel();

Library() {

super("WELCOME TO KCA LIBRARY");

setLayout(new FlowLayout());

setBounds(500,500,500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container con = this.getContentPane();

con.add(panel);

panel.setLayout(null);

label1=new JLabel("USERNAME");

label1.setToolTipText("Enter Your Username");

label2=new JLabel("REGISTRATION NUMBER");

label2.setToolTipText("Enter Your Registration no");

label2.setVerticalTextPosition(JLabel.BOTTOM);

label3 = new JLabel("PASSWORD");

label3.setToolTipText("Enter Your Password");

add(label1);

add(label2);

add(label3);

setVisible(true);

}

public static void main(String[]args) {

new Library();

}

}

2 个解决方案

#1

1

If you're using Java Swing which I presume you are since you've imported it, you can go to the "Design" tab and rearange it there.

如果您使用Java Swing,我認為您已導入它,您可以轉到“設計”選項卡並在那里重新排列。

Alternate way is to use AWT - Which is an older and worse technology than Swing, and in that case, you can't use FlowLayout - it puts all the components into one row. Use BoxLayout, GridBagLayout, GridLayout, GroupLayout or even SpringLayout. More on layouts here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

替代方法是使用AWT - 這是比Swing更老和更糟的技術,在這種情況下,您不能使用FlowLayout - 它將所有組件放入一行。使用BoxLayout,GridBagLayout,GridLayout,GroupLayout甚至SpringLayout。有關布局的更多信息,請訪問:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

If you have any questions, ask. :)

如果您有任何疑問,請詢問。 :)

#2

2

Revise your layouts and components: you have added a panel with no layout to your content pane with flow layout and then adding your labels to the content pane again.

修改布局和組件:您已使用流布局向內容窗格添加了沒有布局的面板,然后再次將標簽添加到內容窗格。 FlowLayout將組件排列在一起。 SpringLayout或GridBagLayout最符合您的需求。這是一個使用GridLayout的簡單示例:

package management;

import javax.swing.*;

import java.awt.*;

public class Library extends JFrame {

JFrame frame = new JFrame();

JPanel panel = new JPanel();

JLabel label1 = new JLabel();

JLabel label3 = new JLabel();

JLabel label2 = new JLabel();

Library(){

super("WELCOME TO KCA LIBRARY");

setBounds(500, 500, 500, 500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container con = this.getContentPane();

con.add(panel);

panel.setLayout(new GridLayout(3, 1));

label1 = new JLabel("USERNAME");

label1.setToolTipText("Enter Your Username");

label2 = new JLabel("REGISTRATION NUMBER");

label2.setToolTipText("Enter Your Registration no");

label3 = new JLabel("PASSWORD");

label3.setToolTipText("Enter Your Password");

panel.add(label1);

panel.add(label2);

panel.add(label3);

setVisible(true);

}

public static void main(String[]args){

new Library();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值