我正在尝试使用我的GUI编码,所以我只是为了创建几个不同的接口,我在课堂上给出的例子。
我试图在这里创建一个GUI,顶部有标签,后跟各自的文本字段供用户输入,一个在另一个之上。然后我希望这后面跟着一系列两个按钮,最后是一个不可编辑的结果字段(后面会介绍一些结果字段)和不透明。我得到了一系列的错误,所以想知道我是否只是做了一些愚蠢的错误。
面板的代码在这里:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GradePanel extends JPanel {
private JButton addEntry, calculate;
private JLabel name, grade;
private JTextField nameField, gradeField, resultField;
public GradePanel() {
// Button to add entry to list
addEntry = new JButton("Add entry to list");
// addEntry.addActionListener(new tempListener());
// Button to print all entries in correct format
calculate = new JButton("Print all user grades");
// calculate.addActionListener(new tempListener());
//Create Labels
name = new JLabel("Enter student name: ");
nameField = new JTextField(10);
// nameField.addActionListener(new tempListener());
grade = new JLabel("Enter students mark: ");
gradeField = new JTextField(5);
// gradeField.addActionListener(new tempListener());
//Bottom segment for result
resultField = new JTextField();
resultField.setOpaque(false);
resultField.setEditable(false);
setLayout(new BorderLayout());
//Bottom Panel
JPanel GradePanel = new JPanel();
GradePanel.setBorder(BorderFactory.createTitledBorder("Students/Results"));
GradePanel.setOpaque(false);
GradePanel.add(resultField);
//Button Panel
JPanel ButtonPane = new JPanel();
ButtonPane.add(addEntry, BoxLayout.LINE_AXIS);
ButtonPane.add(calculate, BoxLayout.LINE_AXIS);
//Label Panel
JPanel labelPane = new JPanel();
labelPane.add(name);
labelPane.add(Box.createRigidArea(new Dimension (5,0)));
labelPane.add(nameField);
labelPane.add(Box.createRigidArea(new Dimension (0,2)));
labelPane.add(grade);
labelPane.add(Box.createRigidArea(new Dimension (5,0)));
labelPane.add(gradeField);
//Add all panels to the main panel
add(labelPane, BorderLayout.NORTH);
add(ButtonPane, BorderLayout.CENTER);
add(GradePanel, BorderLayout.SOUTH);
setBackground(Color.WHITE);
setPreferredSize(new Dimension(400, 300));
}
提前致谢!
编辑:忘记错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Container.java:1087)
at java.awt.Container.add(Container.java:467)
at GradePanel.<init>(GradePanel.java:55)
at Grade$1.run(Grade.java:26)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
编辑2:
public class GradePanel2 extends JPanel {
private JButton addEntry, calculate;
private JLabel name, grade;
private JTextField nameField, gradeField, resultField;
public GradePanel2() {
// Button to add entry to list
addEntry = new JButton("Add entry to list");
// addEntry.addActionListener(new tempListener());
// Button to print all entries in correct format
calculate = new JButton("Print all user grades");
// calculate.addActionListener(new tempListener());
// Create Labels
name = new JLabel("Enter student name: ");
nameField = new JTextField(10);
// nameField.addActionListener(new tempListener());
grade = new JLabel("Enter students mark: ");
gradeField = new JTextField(5);
// gradeField.addActionListener(new tempListener());
// Bottom segment for result
resultField = new JTextField();
resultField.setOpaque(false);
resultField.setEditable(false);
setLayout(new BorderLayout());
// Bottom Panel
JPanel GradePanel = new JPanel();
GradePanel.setBorder(BorderFactory.createTitledBorder("Students/Results"));
GradePanel.setOpaque(false);
GradePanel.add(resultField);
// Button Panel
JPanel ButtonPane = new JPanel();
ButtonPane.setLayout(new BoxLayout(ButtonPane, BoxLayout.PAGE_AXIS));
ButtonPane.add(addEntry, BoxLayout.LINE_AXIS);
ButtonPane.add(calculate, BoxLayout.LINE_AXIS);
// Label Panel
JPanel labelPane = new JPanel();
labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.PAGE_AXIS));
labelPane.add(name);
labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
labelPane.add(nameField);
labelPane.add(Box.createRigidArea(new Dimension(0, 2)));
labelPane.add(grade);
labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
labelPane.add(gradeField);
// Add all panels to the main panel
add(labelPane, BorderLayout.NORTH);
add(ButtonPane, BorderLayout.CENTER);
add(GradePanel, BorderLayout.SOUTH);
setBackground(Color.WHITE);
setPreferredSize(new Dimension(400, 300));
}
}
答案 0 :(得分:1)
您已将BoxLayout
个约束应用于默认使用JPanel
的{{1}} ...
FlowLayout
导致
JPanel ButtonPane = new JPanel();
ButtonPane.add(addEntry, BoxLayout.LINE_AXIS);
ButtonPane.add(calculate, BoxLayout.LINE_AXIS);
也许你应该首先看看How to Use BoxLayout