Java设计UI

时间:2014-09-26 13:18:38

标签: java user-interface jframe

我正在努力在java中编写特定的UI(以编程方式),但我无法实现我的目标UI。我需要帮助来实现我的目标UI。我甚至尝试过SWT Designer,但我还是没有成功。在此先感谢。

我的代码:

public class Main{

    /**
     * @param args
     */

    static JTextArea  lblOne = new JTextArea ("Enter Nb Of Rows");
    static JButton btn1 = new JButton("Generate Table");
    final static TextField tf1 = new TextField();

    static JFrame AFrame;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

           AFrame = new JFrame("Pascal's Table Generator");

          AFrame.setLayout(new BorderLayout());

          // Add a window listner for close button
          AFrame.addWindowListener(new WindowAdapter() {

                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });


          tf1.setText("");



          //AFrame.add(lblOne);
          //lblOne.setText("                        \n                     ");

            //JTextArea textArea = new JTextArea(5, 5);
            JScrollPane scrollableTextArea = new JScrollPane(lblOne);

            scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);





            AFrame.getContentPane().add(scrollableTextArea);

         // AFrame.add(new JScrollPane(lblOne));
          AFrame.add(btn1);
          AFrame.add(tf1);
          AFrame.setSize(800, 800);


          btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                lblOne.setText("");

                genPyrN(Integer.parseInt(tf1.getText().toString()));
            }
        });


          //set the layout of the frame to FlowLayout
          //and align the components to the center
          AFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
         AFrame.setVisible(true);

当前用户界面: enter image description here

目标用户界面: enter image description here

1 个答案:

答案 0 :(得分:1)

首先,您应该阅读有关Java中的LayoutManagers的更多信息,我喜欢这个:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

在这种具体情况下,BorderLayout是个好主意。 Frame可以有一个BorderLayout,JScrollPane可以进入CENTER部分,按钮可以进入NORTH。

但是在代码中设置BorderLayout后,这没有任何意义:AFrame.setLayout(new FlowLayout(FlowLayout.CENTER));。我建议创建一个JPanel,添加按钮,然后将此面板放入BorderLayouts NORTH部分。

相关问题