Я пытаюсь настроить мой основной графический интерфейс и настроить навигацию, прежде чем пытаться кодировать функциональность.В основном у меня есть форма входа в систему, которая работает и открывает страницу моего меню, когда пользователь правильно вводит данные для входа.У меня есть страница AddProperty
, которую я хочу открыть при нажатии кнопки J (эта новая страница должна открыться, и страница меню закрылась).Я использовал книгу Head First Java , чтобы попытаться выполнить эту задачу.
Когда я нажимаю на кнопку J, ничего не происходит.Это может быть связано со структурой моего кода, так как я новичок и просто пытаюсь ознакомиться с кодом.Спасибо за просмотр!
Вот страница меню (NavigationMenu.java
):
/*
* NavigationMenu.java
*
* Created on 18 May 2011, 12:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Login;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Graeme Pearson
*/
public class NavigationMenu {
/** Creates a new instance of NavigationMenu */
public void NavigationMenu()
{
JFrame menu = new JFrame("menuframe");
menu.setVisible(true);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
menu.setSize(180,240);
menu.add(panel);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton AddProperty = new JButton("Add A Property");
panel.add(AddProperty);
//AddProperty gui = new AddProperty();
//gui.AddProperty();
JButton CreateNewAccount = new JButton("Create New Account");
panel.add(CreateNewAccount);
JButton SearchProperty = new JButton("Search Property");
panel.add(SearchProperty);
JButton ViewPropertyDetails = new JButton("View Property");
panel.add(ViewPropertyDetails);
JButton Logout = new JButton("Logout");
panel.add(Logout);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Add A Property"))
{
AddProperty gui = new AddProperty();
gui.AddProperty();
}
//the user pressed "Add A Property"; do something
}
}
Вот содержимое AddProperty.java
:
/**
*
* @author Graeme
*/
package Login;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
public class AddProperty
{
public void AddProperty()
{
JFrame frame = new JFrame("AddPropertyFrame");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// having to set sizes of components is rare, and often a sign
// of problems with layouts.
//frame.setSize(800,600);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
// make it big like the original
panel.setBorder(new EmptyBorder(100,20,100,20));
frame.add(panel);
//panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel HouseNumber = new JLabel("A");
panel.add(HouseNumber);
JTextField HouseNumber1 = new JTextField(10);
panel.add(HouseNumber1);
JLabel HousePrice = new JLabel("B");
panel.add(HousePrice);
JTextField HousePrice1 = new JTextField(10);
panel.add(HousePrice1);
JLabel HouseType = new JLabel("C");
panel.add(HouseType);
JTextField HouseType1 = new JTextField(5);
panel.add(HouseType1);
JButton submit = new JButton("Submit");
panel.add(submit);
submit.addActionListener(new Action());
// tell the GUI to assume its natural (minimum) size.
frame.pack();
}
static class Action implements ActionListener{
@Override
public void actionPerformed (ActionEvent e)
{
// this should probably be a modal JDialog or JOptionPane.
JFrame frame2 = new JFrame("Submitted");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("You Have Submitted a New Property");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
}