0

我正在尝试设置 quiz_7 的文本字段,但是,它不像剩余的六个那样显示,我尝试将参数设置为 360,没有文本字段。将其更改为 340,但我只看到半个文本字段。

import java.awt.Container;

import javax.swing.*;

public class QuizEntry {

    JPanel textPanel,panelForTextFields;
    JLabel Entry_for_Quizes, quiz1, quiz2, quiz3, quiz4, quiz5, quiz6;
    JTextField quiz_1, quiz_2, quiz_3, quiz_4, quiz_5, quiz_6;
    JTextField quiz_7;
    private JLabel quiz7; 

    public JPanel createContentPane() {    
        // We create a bottom JPanel to place everything on.
        JPanel totalGUI1 = new JPanel();
        totalGUI1.setLayout(null);          
        Entry_for_Quizes = new JLabel("Quiz Entry ");
        Entry_for_Quizes.setLocation(0, 0);
        Entry_for_Quizes.setSize(400, 400);
        Entry_for_Quizes.setHorizontalAlignment(4);
        totalGUI1.add(Entry_for_Quizes);            
        // Creation of a Panel to contain the JLabels
                textPanel = new JPanel();
                textPanel.setLayout(null);
                textPanel.setLocation(10, 35);
                textPanel.setSize(100, 600);
                totalGUI1.add(textPanel);

                // Username Label
                quiz1 = new JLabel("Quiz 1");
                quiz1.setLocation(-20, 0);
                quiz1.setSize(70, 40);
                quiz1.setHorizontalAlignment(4);
                textPanel.add(quiz1);

                // Login Label
                quiz2 = new JLabel("Quiz 2");
                quiz2.setLocation(-20, 60);
                quiz2.setSize(70, 40);
                quiz2.setHorizontalAlignment(4);
                textPanel.add(quiz2);

                // Username Label
                quiz3 = new JLabel("Quiz 3");
                quiz3.setLocation(-20, 120);
                quiz3.setSize(70, 40);
                quiz3.setHorizontalAlignment(4);
                textPanel.add(quiz3);

                // Login Label
                quiz4 = new JLabel("Quiz 4");
                quiz4.setLocation(-20, 180);
                quiz4.setSize(70, 40);
                quiz4.setHorizontalAlignment(4);
                textPanel.add(quiz4);

                // Username Label
                quiz5 = new JLabel("Quiz 5");
                quiz5.setLocation(-20, 240);
                quiz5.setSize(70, 40);
                quiz5.setHorizontalAlignment(4);
                textPanel.add(quiz5);

                // L
                quiz6 = new JLabel("Quiz 6");
                quiz6.setLocation(-20, 300);
                quiz6.setSize(70, 40);
                quiz6.setHorizontalAlignment(4);
                textPanel.add(quiz6);

                quiz7 = new JLabel("Quiz 7");
                quiz7.setLocation(-20, 350);
                quiz7.setSize(70, 40);
                quiz7.setHorizontalAlignment(4);
                textPanel.add(quiz7);

                //////////////////////////////////////////////////////////////
                panelForTextFields = new JPanel();
                panelForTextFields.setLayout(null);
                panelForTextFields.setLocation(110, 40);
                panelForTextFields.setSize(100, 350);
                totalGUI1.add(panelForTextFields);

                // quiz Textfield
                quiz_1 = new JTextField(8);
                quiz_1.setLocation(0, 0);
                quiz_1.setSize(100, 30);
                panelForTextFields.add(quiz_1);

                quiz_2 = new JTextField(8);
                quiz_2.setLocation(0, 60);
                quiz_2.setSize(100, 30);
                panelForTextFields.add(quiz_2);

                quiz_3 = new JTextField(8);
                quiz_3.setLocation(0, 120);
                quiz_3.setSize(100, 30);
                panelForTextFields.add(quiz_3);

                quiz_4 = new JTextField(8);
                quiz_4.setLocation(0, 180);
                quiz_4.setSize(100, 30);
                panelForTextFields.add(quiz_4);

                quiz_5 = new JTextField(8);
                quiz_5.setLocation(0, 240);
                quiz_5.setSize(100, 30);
                panelForTextFields.add(quiz_5);

                quiz_6 = new JTextField(8);
                quiz_6.setLocation(0, 300);
                quiz_6.setSize(100, 30);
                panelForTextFields.add(quiz_6);

                quiz_7 = new JTextField(8);
                quiz_7.setLocation(0, 340);
                quiz_7.setSize(100, 30);
                panelForTextFields.add(quiz_7);
        return totalGUI1;

             }
               private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Quiz Entry");    
        QuizEntry demo = new QuizEntry();
        frame.setContentPane(demo.createContentPane());    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 700);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
4

1 回答 1

1

基本上改变大小:对于 panelForTextFields. 例如从 350 到 500。

为了使代码有点通用,我会写如下内容:

panelForTextFields.setSize(100, 6*80);

6s的计数在哪里JTextField

如果我们走得更远,我会创建JTextField如下列表:

List<JTextField> list = new ArrayList<JTextField>();
list.add(quiz_1);
list.add(quiz_2);
list.add(quiz_3);
list.add(quiz_4);
list.add(quiz_5);
list.add(quiz_6);

之后会写:

 final int GAP = 40;
 panelForTextFields.setSize(100, 6*( quiz_1.getHeight()  + GAP));

由于您使用相同的逻辑来配置JTextfields,您现在可以使用list如下:

    quiz_1 = new JTextField(8);     
    panelForTextFields.add(quiz_1);


    quiz_2 = new JTextField(8);
    panelForTextFields.add(quiz_2);

    quiz_3 = new JTextField(8);
    panelForTextFields.add(quiz_3);

    quiz_4 = new JTextField(8);
    panelForTextFields.add(quiz_4);

    quiz_5 = new JTextField(8);
    panelForTextFields.add(quiz_5);

    quiz_6 = new JTextField(8);
    panelForTextFields.add(quiz_6);

    quiz_7 = new JTextField(8);
    panelForTextFields.add(quiz_7);

    list.add(quiz_1);
    list.add(quiz_2);
    list.add(quiz_3);
    list.add(quiz_4);
    list.add(quiz_5);
    list.add(quiz_6);
    list.add(quiz_7);

    for(int k = 0; k<list.size(); k++){         
        JTextField f  = list.get(k);            
        f.setLocation(0, k*60);
        f.setSize(100, 30);         
    }       

    final int GAP = 40;
    panelForTextFields.setSize(100, 6*( quiz_1.getHeight()  + GAP));
于 2013-11-02T20:34:14.573 回答