我是编码的新手,想知道为什么我的JMenuBar不会出现? 这是代码,谢谢! :)
package vp.hendrasetiawan.pkg20413017.week4no2;
public class VPHendraSetiawan20413017Week4No2 {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
}
}
MainFrame.java
package vp.hendrasetiawan.pkg20413017.week4no2;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
public MainFrame() {
this.setContentPane(new PeriodFormPanel());
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
PeriodFormPanel
package vp.hendrasetiawan.pkg20413017.week4no2;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PeriodFormPanel extends JPanel {
private JLabel titleLabel, fieldLabel;
private JTextArea expertise;
private JTextField idField,
nameField,
dob1Field, dob2Field, dob3Field,
address1Field, address2Field,
phoneField,
emailField, image;
private ButtonGroup group = new ButtonGroup();
private JRadioButton genderFbutton, genderMbutton;
private JButton backButton,
saveButton,
cancelButton;
private JMenu login, student, kelas, teacher, presence;
private JMenuItem logout, change,
newStudent, listStudent, joinClass, payment,
newpayment, history,
course,
newCourse, listCourse,
activeClass,
newActiveClass, listActiveClass,
newTeacher, listTeacher,
spresence, tpresence;
private JButton btnImage;
public PeriodFormPanel() {
this.setPreferredSize(new Dimension(450,750));
buildLayout();
}
public void buildLayout() {
titleLabel = new JLabel("Teacher Form");
titleLabel.setFont(new Font("Times New Roman",Font.BOLD, 20));
idField = new JTextField();
nameField = new JTextField();
genderFbutton = new JRadioButton("Female");
genderMbutton = new JRadioButton("Male", true);
group.add(genderFbutton);
group.add(genderMbutton);
dob1Field = new JTextField();
dob2Field = new JTextField();
dob3Field = new JTextField();
address1Field = new JTextField();
address2Field = new JTextField();
phoneField = new JTextField();
emailField = new JTextField();
expertise = new JTextArea(
"Piano \n"
+ "Guitar \n"
+ "Vocal \n"
+ "Drum \n"
+ "Violin \n \n \n");
backButton = new JButton("<< Back");
saveButton = new JButton("Save");
cancelButton = new JButton("Cancel");
//create a menu bar
final JMenuBar menuBar = new JMenuBar();
//create menus
login = new JMenu("Login");
student = new JMenu("Student");
kelas = new JMenu("Class");
teacher = new JMenu("Teacher");
presence = new JMenu("Presence");
//create menus
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
final JMenu aboutMenu = new JMenu("About");
final JMenu linkMenu = new JMenu("Links");
//create menu items
//Login
logout = new JMenuItem("Logout");
change = new JMenuItem("Change Password");
//Student
newStudent = new JMenuItem("New Student");
listStudent = new JMenuItem("List of Student");
joinClass = new JMenuItem("Join Class");
payment = new JMenuItem("Payment");
//Payment SubMenu
payment.add(new JMenuItem("New Payment"));
payment.add(new JMenuItem("History"));
//Class
course = new JMenuItem("Course");
//Course SubMenu
course.add(new JMenuItem("New Course"));
course.add(new JMenuItem("List of Course"));
activeClass = new JMenuItem("Active Class");
//ActiveClass SubMenu
activeClass.add(new JMenuItem("New Active Class"));
activeClass.add(new JMenuItem("List of Active Class"));
//Teacher
newTeacher = new JMenuItem("New Teacher");
listTeacher = new JMenuItem("List of Teacher");
//Presence
spresence = new JMenuItem("Student's Presence");
tpresence = new JMenuItem("Teacher's Presence");
login.add(logout);
login.add(change);
student.add(newStudent);
student.add(listStudent);
student.add(joinClass);
student.add(payment);
kelas.add(course);
kelas.add(activeClass);
teacher.add(newTeacher);
teacher.add(listTeacher);
presence.add(spresence);
presence.add(tpresence);
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
String colSizes = "20px, pref, 25px, pref, 20px, 30px, 20px, 30px, 20px, 20px, 20px, 70px, 20px";
String rowSizes = "20px, pref, 20px, pref, 20px, pref, 20px, pref, 20px, pref, 20px, pref, 20px, pref, 20px, pref, 10px, 120px, 20px, pref, 20px, 200px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
this.setLayout(layout);
CellConstraints cc = new CellConstraints();
//Student Form
this.add(titleLabel, cc.xywh(2, 2, 3, 1));
//ID
fieldLabel = new JLabel("ID");
this.add(fieldLabel, cc.xywh(2, 4, 1, 1));
this.add(idField, cc.xywh(4, 4, 2, 1));
idField.setEditable(false);
//Name
fieldLabel = new JLabel("Name");
this.add(fieldLabel, cc.xywh(2, 6, 1, 1));
this.add(nameField, cc.xywh(4, 6, 9, 1));
//Gender
fieldLabel = new JLabel("Gender");
this.add(fieldLabel, cc.xywh(2, 8, 1, 1));
this.add(genderFbutton, cc.xywh(4, 8, 1, 1));
this.add(genderMbutton, cc.xywh(6, 8, 3, 1));
//DOB
fieldLabel = new JLabel("D.O.B");
this.add(fieldLabel, cc.xywh(2, 10, 1, 1));
this.add(dob1Field, cc.xywh(4, 10, 1, 1));
this.add(dob2Field, cc.xywh(6, 10, 3, 1));
this.add(dob3Field, cc.xywh(10, 10, 3, 1));
//Address
fieldLabel = new JLabel("Address");
this.add(fieldLabel, cc.xywh(2, 12, 1, 1));
this.add(address1Field, cc.xywh(4, 12, 7, 1));
this.add(address2Field, cc.xywh(12, 12, 1, 1));
//Phone
fieldLabel = new JLabel("Phone");
this.add(fieldLabel, cc.xywh(2, 14, 1, 1));
this.add(phoneField, cc.xywh(4, 14, 3, 1));
//Email
fieldLabel = new JLabel("Email");
this.add(fieldLabel, cc.xywh(2, 16, 1, 1));
this.add(emailField, cc.xywh(4, 16, 3, 1));
//Expertise
fieldLabel = new JLabel("Expertise");
this.add(fieldLabel, cc.xywh(2, 17, 1, 2));
this.add(expertise, cc.xywh(4, 18, 3, 1));
//Image
BufferedImage image = null;
try
{
image = ImageIO.read(new File("Box.jpg"));
}
catch(IOException ex)
{
System.out.println("Gambar Tidak Ditemukan");
}
JLabel picLabel = new JLabel(new ImageIcon(image));
this.add((picLabel), cc.xywh(8, 14, 5, 5));
//Back
backButton.setPreferredSize(new Dimension(50,25));
backButton.setFont(new Font("Times New Roman",Font.BOLD, 10)); //Font - Bold - Size
this.add(backButton, cc.xywh(2, 20, 2, 1));
//Save
saveButton.setPreferredSize(new Dimension(50,25));
saveButton.setFont(new Font("Times New Roman",Font.BOLD, 10));
this.add(saveButton, cc.xywh(8, 20, 3, 1));
//Cancel
cancelButton.setPreferredSize(new Dimension(50,25));
cancelButton.setFont(new Font("Times New Roman",Font.BOLD, 10));
this.add(cancelButton, cc.xywh(12, 20, 1, 1));
//Button Image
BufferedImage image1 = null;
try
{
image = ImageIO.read(new File("pixel.png"));
}
catch(IOException ex)
{
System.out.println("Gambar Tidak Ditemukan");
}
btnImage = new JButton(new ImageIcon(image));
this.add((btnImage), cc.xywh(8, 22, 5, 1));
}
}
答案 0 :(得分:1)
1)向PeriodFormPanel
添加一个字段 private JMenuBar menuBar;
2)将getter添加到PeriodFormPanel
public JMenuBar getMenuBar() {
return menuBar;
}
3)在PeriodFormPanel
中的buildLayout函数末尾执行此代码 menuBar.add(login);
menuBar.add(student);
menuBar.add(kelas);
menuBar.add(teacher);
menuBar.add(presence);
this.menuBar = menuBar;
4)更改您的Mainframe Constructor
public MainFrame() {
PeriodFormPanel periodFormPanel = new PeriodFormPanel();
this.setJMenuBar(periodFormPanel.getMenuBar());
this.setContentPane(periodFormPanel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}