2014-04-06 35 views
0

我写了这段代码,但是我在使它从右到左对齐时遇到了问题。 我尝试了几件事情,但没有一件能奏效。 我知道这可以通过玩坐标来完成,但我想要一种方法,我不需要为每个单词都这样做。我该如何正确地做到这一点?如何在JFrame/java中从右到左对齐?

代码:

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); 

} 

}

+0

您正在'frame1.getContentPane()'中添加多个组件。这是绞刑。使用'JPanel'并添加其中的所有组件。并在帧的内容窗格中添加'JPanel'。 – Braj

+0

我是新来的...新代码需要怎么样? – user3504367

+0

请看我的答案。 – Braj

回答

0

尝试

pane = new JPanel(); 

代替

pane = frame1.getContentPane(); 

,并删除此行

pane.setLayout(null); 

最后加paneJFrame

frame1.getContentPane().add(pane); 
+0

这是可怕的代码,因为我已经在我的答案中提到过。仅仅因为OP发布结构不良的代码并不意味着你应该这样做。这就是为什么我提供了Swing教程的链接,它可以正确执行,因此OP也可以学习如何正确执行。 – camickr

+0

它对齐它的中心... – user3504367

+0

对不起,我不能帮你。你必须先学习。试试@camickr的建议。 – Braj

2

试着这么做:

JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
panel.add(...); 
panel.add(...); 
... 
frame.add(panel, BorderLayout.NORTH); 

和组件将调整到帧的乘坐侧。

另外,摆脱所有的静态变量。这不是编写课程的方式。

有关更多信息和示例,请参阅How to Use Flow Layout的Swing教程中的部分。如果能告诉你一个更好的方法来构建你的班级,而不需要所有的静态内容。