JFrame表单创建(多行,列和输入)

时间:2015-03-27 05:33:27

标签: java swing jframe

我想实现一个遵循以下照片流程的JFrame:

enter image description here

我想写一个带有两个介绍文本的框架,每个问题的一行,同一行上有一个输入框。我想要10行,然后在底部有一个按钮来完成对话框。

我试图实现这一点,但我无法超越第一行。有人至少可以指出我正确的方向吗?具有前4行和按钮的代码可以让我尽我所能完成。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

class GUI_Short_Scale extends JFrame
{
/**
     * 
     */
    private static final long serialVersionUID = 1L;
    JDialog d1;
    public int number;
    JButton cont;
    JTextField tf;

    public GUI_Short_Scale()
    {

        createAndShowGUI();
    }

    public int getNumber()
    {
        return this.number;
    }

    private void createAndShowGUI()
    {

        // Must be called before creating JDialog for
        // the desired effect
        JDialog.setDefaultLookAndFeelDecorated(true);

        // A perfect constructor, mostly used.
        // A dialog with current frame as parent
        // a given title, and modal
        d1 = new JDialog(this,"Short Scale",true);

        // Set size
        d1.setSize(400,400);
        d1.setLocationRelativeTo(null);  // *** this will center your app ***

        d1.setLayout(new FlowLayout());



        cont = new JButton("Continue");
        cont.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) 
            { 
                if(isNumber(tf.getText()))
                {
                    // everything worked out just fine
                    number = Integer.valueOf(tf.getText());
                    d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
                }
                else
                {                    
                    // create a jframe
                    JFrame frame = new JFrame("Error");

                    // show a joptionpane dialog using showMessageDialog
                    JOptionPane.showMessageDialog(frame,"The input you gave does not look like a number. Please try again.");
                }
            } 

        }); 

        tf = new JTextField(20);
        d1.add(new JLabel("Tetris Intro"));
        d1.add(tf);
        d1.add(cont);
        d1.setVisible(true);
    }

    private boolean isNumber(String s)
    {
        try
        {
            Integer.valueOf(s);
        }
        catch(NumberFormatException ne)
        {
            return false;
        }
        // is a number
        return true;
    }
}

1 个答案:

答案 0 :(得分:2)

也许像......

Layout 01

public class TestPane extends JPanel {

    public TestPane() {

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JTextArea intro = new JTextArea(4, 20);
        JTextArea moreText = new JTextArea(4, 20);

        JLabel question = new JLabel("Question 1");
        JTextField answer = new JTextField(20);
        JButton btn = new JButton("Done");

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(2, 2, 2, 2);

        add(new JScrollPane(intro), gbc);
        gbc.gridy++;
        add(new JScrollPane(moreText), gbc);

        gbc.gridy++;
        gbc.insets = new Insets(20, 2, 2, 2);
        gbc.anchor = GridBagConstraints.WEST;

        add(question, gbc);
        gbc.gridx++;
        add(answer, gbc);

        gbc.insets = new Insets(80, 2, 2, 2);
        gbc.gridy++;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(btn, gbc);

    }

}

或者

Layout02

public class TestPane extends JPanel {

    public TestPane() {

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JTextArea intro = new JTextArea(4, 20);
        JTextArea moreText = new JTextArea(4, 20);

        JLabel question = new JLabel("Question 1");
        JTextField answer = new JTextField(20);
        JButton btn = new JButton("Done");

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(2, 2, 2, 2);

        add(new JScrollPane(intro), gbc);
        gbc.gridy++;
        add(new JScrollPane(moreText), gbc);

        gbc.gridwidth = 1;
        gbc.gridy++;
        gbc.insets = new Insets(20, 2, 2, 2);
        gbc.anchor = GridBagConstraints.WEST;

        add(question, gbc);
        gbc.gridx++;
        add(answer, gbc);

        gbc.insets = new Insets(80, 2, 2, 2);
        gbc.gridy++;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(btn, gbc);

    }

}

请查看Laying Out Components Within a ContainerHow to Use GridBagLayout了解详情

  

但是你可以放入有两个问题行的代码吗?

public class TestPane extends JPanel {

    public TestPane() {

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JTextArea intro = new JTextArea(4, 20);
        JTextArea moreText = new JTextArea(4, 20);

        JLabel question1 = new JLabel("Question 1");
        JTextField answer1 = new JTextField(20);
        JLabel question2 = new JLabel("Question 2");
        JTextField answer2 = new JTextField(20);
        JButton btn = new JButton("Done");

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(2, 2, 2, 2);

        add(new JScrollPane(intro), gbc);
        gbc.gridy++;
        add(new JScrollPane(moreText), gbc);

        gbc.gridy++;
        gbc.insets = new Insets(20, 2, 2, 2);
        gbc.anchor = GridBagConstraints.WEST;

        add(question1, gbc);
        gbc.gridx++;
        add(answer1, gbc);

        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.gridy++;
        gbc.gridx = 0;
        add(question2, gbc);
        gbc.gridx++;
        add(answer2, gbc);

        gbc.insets = new Insets(80, 2, 2, 2);
        gbc.gridy++;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(btn, gbc);

    }

}