Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 盒子布局和图形_Java_Boxlayout_Data Entry - Fatal编程技术网

Java 盒子布局和图形

Java 盒子布局和图形,java,boxlayout,data-entry,Java,Boxlayout,Data Entry,我希望得到一个工作示例,说明如何根据下面提供的代码实现box布局。目前我只熟悉边界布局,但这个项目我不知道如何添加5个输入字段,这些字段可以在输入数字后应用函数。目前,图形方面的代码非常混乱,但主要是占位符,尽管格式是我想要的 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.JFrame; import

我希望得到一个工作示例,说明如何根据下面提供的代码实现box布局。目前我只熟悉边界布局,但这个项目我不知道如何添加5个输入字段,这些字段可以在输入数字后应用函数。目前,图形方面的代码非常混乱,但主要是占位符,尽管格式是我想要的

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GradeFrame {

    public static void main(String[] args) {

    JFrame GradeFrame = new JFrame("Check Your Grades!");
    GradeFrame.setLocation(450, 150);
    GradeFrame.setPreferredSize(new Dimension(1000, 625));
    GradeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    Font font1 = new Font("SansSerif", Font.BOLD, 32);

    JPanel section1 = new JPanel();
    JTextArea entry1 = new JTextArea
    ("Place holder for user's name and class");
    entry1.setPreferredSize(new Dimension(350, 110));
    entry1.setBackground(Color.lightGray);
    entry1.setLineWrap(true);
    entry1.setEditable(false);
    entry1.setFont(font1);
    section1.add(entry1);

    JPanel section2 = new JPanel();
    JTextField entry2 = new JTextField("Current Grade: ");
    entry2.setPreferredSize(new Dimension(350, 110));
    entry2.setHorizontalAlignment(JTextField.CENTER);
    entry2.setEditable(false);
    entry2.setFont(font1);
    section2.add(entry2);

    JPanel section3 = new JPanel();
    JTextField entry3 = new JTextField("Grades entered: ");
    entry3.setPreferredSize(new Dimension(350, 110));
    entry3.setHorizontalAlignment(JTextField.CENTER);
    entry3.setEditable(false);
    entry3.setFont(font1);
    section3.add(entry3);

    JPanel section4 = new JPanel();
    JTextField entry4 = new JTextField
    ("graphic place holder");
    entry4.setPreferredSize(new Dimension(350, 110));
    entry4.setHorizontalAlignment(JTextField.CENTER);
    entry4.setEditable(false);
    entry4.setFont(font1);
    section4.add(entry4);

    JPanel section5 = new JPanel();
    JTextField entry5 = new JTextField("Data entry area");
    entry5.setPreferredSize(new Dimension(600, 500));
    entry5.setHorizontalAlignment(JTextField.CENTER);
    entry5.setEditable(false);
    entry5.setFont(font1);
    section5.add(entry5);

    JPanel section6 = new JPanel();
    section6.add(section1);
    section6.add(section2);
    section6.add(section3);
    section6.add(section4);

    GradeFrame.add(section5, BorderLayout.WEST);
    GradeFrame.add(section6);

    GradeFrame.pack();

    GradeFrame.setVisible(true);
    GradeFrame.setLayout(new BorderLayout());
    }
}
我试图实现一些具有第二部分代码功能的代码。这里的一些代码是由我上一个问题中的另一个用户提供的

import java.text.DecimalFormat;
import java.util.Scanner;

public class GradeData {

    public static void main(String[] args) {

int count = 0;
double grade = 0;
String user;
boolean enter = true;
String name;
String enterAgain;
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter your name: ");
name = userInput.nextLine();
System.out.println("Please enter a grade: ");
grade = userInput.nextDouble();
userInput.nextLine(); //add this to your program

System.out.println("Would you like to continue entering grades? (y/n)");
enterAgain = userInput.nextLine();

while(enter){
    if(enterAgain.equalsIgnoreCase("y")){
        System.out.println("Please enter another grade: ");
        grade = userInput.nextDouble();
    }
    else if(enterAgain.equalsIgnoreCase("n")){
        System.out.println("Ok. Thank you for entering grades.");
        break; 
    }
    else{
        System.out.println("Sorry, you can only enter (y/n)");
        break; 
    }
}
}
}
目前,我只熟悉边框布局,然后您可以从Swing教程中的部分开始学习工作示例。您可以下载演示并使用它们。不要忘记,可以使用不同的布局管理器嵌套每个面板。GradeFrame.setLayoutnew BorderLayout;-框架的默认布局管理器是BorderLayout,因此不需要该语句。此外,在将构件添加到配电盘后,也不会设置布局。在开始添加组件之前设置布局。此外,您不需要为每个文本字段创建面板。可以将每个文本字段直接添加到section6面板。