[英]How to alignment right to left in JFrame/java?
I wrote this code, but I'm having a problem with making it align from right to left. 我编写了这段代码,但我遇到了从右到左对齐的问题。 I tried several things, but none of them works. 我尝试了几件事,但都没有。 I know it can be done by playing with the coordinates, but I want a way that I wouldn't need to do that for every word. 我知道它可以通过使用坐标来完成,但我想要一种我不需要为每个单词做的方式。 How am I supposed to do it the right way? 我怎么能以正确的方式做到这一点?
code: 码:
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class GUI {
//messages
public static final String ConnectC_MSG = "התחבר" ;
public static final String DisconnectC_MSG = "התנתק" ;
public static final String ServerLBL_MSG = "שרת יעד:" ;
public static final String UsernameLBL_MSG = ":שם משתמש";
public static final String PasswordLBL_MSG = ":סיסמא" ;
public static final String PortLBL_MSG = ":פתחה" ;
//sizes
public static final int SreverTxtfield_Width = 10;
public static final int UsernameTxtfield_width = 10;
public static final int PasswordTxtfield_width = 10;
public static final int PortTxtfield_width = 5 ;
public static final int WINDOW_WIDTH = 800;
public static final int WINDOW_HEIGHT = 200;
static JFrame frame1;
static Container pane;
static JButton btnConnect = new JButton(ConnectC_MSG),
btiDiscinnect = new JButton(DisconnectC_MSG);
static JLabel lblServer = new JLabel(ServerLBL_MSG),
lblUsername = new JLabel(UsernameLBL_MSG),
lblPassword = new JLabel(PasswordLBL_MSG),
lblPort = new JLabel(PortLBL_MSG);
static JTextField txtServer = new JTextField(SreverTxtfield_Width),
txtUsername = new JTextField(UsernameTxtfield_width),
txtPort = new JTextField(PortTxtfield_width);
static JPasswordField txtPassword = new JPasswordField(PasswordTxtfield_width);
static Insets insets;
public static void main(String[] args) {
frame1 = new JFrame ("הדגמה");
frame1.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
frame1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
pane = frame1.getContentPane();
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
insets = pane.getInsets();
pane.setLayout(null);
pane.add(lblServer);
pane.add(lblUsername);
pane.add(lblPassword);
pane.add(lblPort);
pane.add(txtServer);
pane.add(txtUsername);
pane.add(txtPassword);
pane.add(txtPort);
lblServer.setBounds((int)(insets.right),insets.top,
lblServer.getPreferredSize().width, lblServer.getPreferredSize().height);
txtServer.setBounds(lblServer.getX()+lblServer.getWidth(), lblServer.getY()+lblServer.getHeight(),
txtServer.getPreferredSize().width, txtServer.getPreferredSize().height);
frame1.setVisible(true);
}
} }
Try something like: 尝试类似的东西:
JPanel panel = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
panel.add(...);
panel.add(...);
...
frame.add(panel, BorderLayout.NORTH);
and the components will align to the ride side of the frame. 并且组件将对准框架的骑行侧。
Also, get rid of all the static variables. 另外,摆脱所有静态变量。 That is NOT the way to write a class. 那不是写课的方法。
Read the section from the Swing tutorial on How to Use Flow Layout for more information and examples. 有关更多信息和示例,请阅读有关如何使用流布局的Swing教程中的部分。 If will show you a better way to structure your class without all the static stuff. 如果没有所有静态的东西,你会告诉你一个更好的方法来构建你的类。
Try 尝试
pane = new JPanel();
in place of 代替
pane = frame1.getContentPane();
and remove this line 并删除此行
pane.setLayout(null);
and finally add pane
to JFrame
最后将pane
添加到JFrame
frame1.getContentPane().add(pane);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.