Я работаю с NetBeans и пытаюсь запрограммировать программу с пользовательским интерфейсом.
Мой основной класс - это форма JFrame
, а имя файла - HotelHG.java
.
Я могу написатьвесь код в этом файле без проблем, но код будет слишком сложным, и я никогда не смогу найти код, который ищу внутри.
Поэтому я создал новый класс Java с именем DataBaseConnection
, чтобы положить некоторыекод в.
Здесь возникает проблема: когда я делаю
public class DataBaseConnection extends HotelHG{
//Some Codes...
}
, я получаю:
Исключение в потоке "AWT-EventQueue-0" Java.lang.StackOverflowError в sun.awt.Win32GraphicsConfig.getBounds (собственный метод) в sun.awt.Win32GraphicsConfig.getBounds (Win32GraphicsConfig.java:215)
Поэтому я попытался опубликовать свой класс HotelHG
компонентов и попытался получить к ним доступ напрямую без extends
, но затем DataBaseConnection
не смог найти HotelHG
компоненты.
Короче, у меня есть класс в A.java
, который имеет форму JFrame
и содержит main()
, и я должен получить доступ к его компонентам из другогокласс, в B.java
.
Обновление:
Я проверил, что мои коды не могут ничего найти. Поэтому я добавил свои коды в основной класс, чтобы посмотреть, будет ли проблема решена.Но на этот раз это дает ту же ошибку для класса C ... Я думаю, что я узнаю, что здесь происходит.Я добавлю коды, поэтому, если вы можете посмотреть и найти что-нибудь, пожалуйста, дайте мне знать еще раз спасибо.
Коды основного класса:
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HotelHG extends javax.swing.JFrame{
private Connection Con = null;
private String Url = "jdbc:mysql://localhost:3306/";
private String Db = "hotel_hg";
private String Dri = "com.mysql.jdbc.Driver";
private String Root = "root";
private String Pass = "";
protected ResultSet Res;
Tab1Codes MyCodes1 = new Tab1Codes();
/** Creates new form HotelHG */
//============================================================================== I N S T A L L A T I O N ===========
public HotelHG() {
Statement st;
try{
st = OpenConnection();
Res = st.executeQuery("select * from idpass");
}catch(Exception ex){
JOptionPane.showConfirmDialog(null,"Няма Достъп до Информатции!",
"Опит за Свързване на бази данни...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
}
initComponents();
}
//============================================================================== I N S T A L L A T I O N ===========
//=================================================================================================== OPEN CONNNECTION
private Statement OpenConnection() throws Exception{
try{
Class.forName(Dri).newInstance();
}catch(Exception ex){
JOptionPane.showConfirmDialog(null,"Връзка с Дата Бейс Не е Успешна-Driver!",
"Опит за Свързване на бази данни...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
}
try{
Con = DriverManager.getConnection(Url + Db, Root, Pass);
}catch(Exception ex){
JOptionPane.showConfirmDialog(null,"Връзка с Дата Бейс Не е Успешна-Url!",
"Опит за Свързване на бази данни...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
}
return Con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}
//=================================================================================================== OPEN CONNNECTION
//=================================================================================================== CLOSE CONNECTION
public void CloseConnection(){
try
{
Con.close();
}catch(Exception ex)
{
Logger.getLogger(HotelHG.class.getName()).log(Level.SEVERE,null,ex);
}
}
//=================================================================================================== CLOSE CONNECTION
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
TabMenu = new javax.swing.JTabbedPane();
Tab1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
IDBox1 = new javax.swing.JTextField();
PassBox = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
EnterButton1 = new javax.swing.JButton();
StatusLabel1 = new javax.swing.JLabel();
ExitButton1 = new javax.swing.JButton();
Tab2 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
Tab3 = new javax.swing.JPanel();
Tab4 = new javax.swing.JPanel();
Tab5 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Хотел Heaven Garden");
setMinimumSize(null);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
TabMenu.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
TabMenu.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
TabMenu.setFont(new java.awt.Font("Consolas", 1, 14)); // NOI18N
Tab1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(204, 0, 0), new java.awt.Color(204, 0, 0), null));
jLabel1.setFont(new java.awt.Font("Consolas", 2, 24));
jLabel1.setText("Heaven Garden Главно меню:"); // NOI18N
jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jLabel2.setFont(new java.awt.Font("Consolas", 2, 24));
jLabel2.setText("-Напишете паролата си за да активирате контролите на меню..."); // NOI18N
jLabel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jLabel3.setFont(new java.awt.Font("Consolas", 2, 24));
jLabel3.setText("-Само администратора може да активира всичките менюта!"); // NOI18N
jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jLabel4.setFont(new java.awt.Font("Consolas", 2, 24));
jLabel4.setText("-При три грешени опити програмата ще се заключва!"); // NOI18N
jLabel4.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
IDBox1.setFont(new java.awt.Font("Consolas", 2, 18));
PassBox.setFont(new java.awt.Font("Consolas", 2, 18));
jLabel5.setFont(new java.awt.Font("Consolas", 0, 18));
jLabel5.setText("Потребителско Име:");
jLabel6.setFont(new java.awt.Font("Consolas", 0, 18));
jLabel6.setText("Парола:");
EnterButton1.setFont(new java.awt.Font("Consolas", 1, 18));
EnterButton1.setText("Влизай");
EnterButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EnterButton1ActionPerformed(evt);
}
});
StatusLabel1.setFont(new java.awt.Font("Consolas", 0, 24));
StatusLabel1.setText("Статус:Няма Достъп");
ExitButton1.setFont(new java.awt.Font("Consolas", 1, 18));
ExitButton1.setText("Изход");
ExitButton1.setActionCommand("jButton1");
javax.swing.GroupLayout Tab1Layout = new javax.swing.GroupLayout(Tab1);
Tab1.setLayout(Tab1Layout);
Tab1Layout.setHorizontalGroup(
Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(Tab1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jSeparator1)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, Tab1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6)
.addComponent(IDBox1)
.addComponent(PassBox)
.addComponent(EnterButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(StatusLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ExitButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(15, Short.MAX_VALUE))
);
Tab1Layout.setVerticalGroup(
Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(Tab1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(4, 4, 4)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(IDBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PassBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(EnterButton1)
.addGap(18, 18, 18)
.addComponent(StatusLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ExitButton1)
.addContainerGap(12, Short.MAX_VALUE))
);
TabMenu.addTab("Главно меню", new javax.swing.ImageIcon(getClass().getResource("/Pics/Admin.png")), Tab1); // NOI18N
Tab1.getAccessibleContext().setAccessibleName("Tab1");
Tab2.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
Tab2.setEnabled(false);
jTextField1.setText("jTextField1");
jButton1.setText("jButton1");
javax.swing.GroupLayout Tab2Layout = new javax.swing.GroupLayout(Tab2);
Tab2.setLayout(Tab2Layout);
Tab2Layout.setHorizontalGroup(
Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(Tab2Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(633, Short.MAX_VALUE))
);
Tab2Layout.setVerticalGroup(
Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(Tab2Layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(360, Short.MAX_VALUE))
);
TabMenu.addTab("Рецепция", new javax.swing.ImageIcon(getClass().getResource("/Pics/Reception.png")), Tab2); // NOI18N
Tab2.getAccessibleContext().setAccessibleName("Tab2");
Tab3.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
Tab3.setEnabled(false);
javax.swing.GroupLayout Tab3Layout = new javax.swing.GroupLayout(Tab3);
Tab3.setLayout(Tab3Layout);
Tab3Layout.setHorizontalGroup(
Tab3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 803, Short.MAX_VALUE)
);
Tab3Layout.setVerticalGroup(
Tab3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 431, Short.MAX_VALUE)
);
TabMenu.addTab("Резервация", new javax.swing.ImageIcon(getClass().getResource("/Pics/Reservation.png")), Tab3); // NOI18N
Tab3.getAccessibleContext().setAccessibleName("Tab3");
Tab4.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
Tab4.setEnabled(false);
javax.swing.GroupLayout Tab4Layout = new javax.swing.GroupLayout(Tab4);
Tab4.setLayout(Tab4Layout);
Tab4Layout.setHorizontalGroup(
Tab4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 803, Short.MAX_VALUE)
);
Tab4Layout.setVerticalGroup(
Tab4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 431, Short.MAX_VALUE)
);
TabMenu.addTab("Ресторант", new javax.swing.ImageIcon(getClass().getResource("/Pics/desk.png")), Tab4); // NOI18N
Tab4.getAccessibleContext().setAccessibleName("Tab4");
Tab5.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
Tab5.setEnabled(false);
javax.swing.GroupLayout Tab5Layout = new javax.swing.GroupLayout(Tab5);
Tab5.setLayout(Tab5Layout);
Tab5Layout.setHorizontalGroup(
Tab5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 805, Short.MAX_VALUE)
);
Tab5Layout.setVerticalGroup(
Tab5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 433, Short.MAX_VALUE)
);
TabMenu.addTab("Работници", new javax.swing.ImageIcon(getClass().getResource("/Pics/w1.png")), Tab5); // NOI18N
Tab5.getAccessibleContext().setAccessibleName("Tab5");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TabMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 820, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TabMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)
);
TabMenu.getAccessibleContext().setAccessibleName("TabMenu");
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new HotelHG().setVisible(true);
}
});
}
//============================================================================== FORM OPEN - CLOSE
private void formWindowClosing(WindowEvent evt) {
CloseConnection();
}
private void formWindowOpened(WindowEvent evt) {
if(!MyCodes1.LockAll())
System.exit(1);
}
//============================================================================== FORM OPEN - CLOSE
//============================================================================== TAB1 CODES
private void EnterButton1ActionPerformed(ActionEvent evt) {
MyCodes1.AdministrationControl();
}
//============================================================================== TAB1 CODES
// Variables declaration - do not modify
public javax.swing.JButton EnterButton1;
public javax.swing.JButton ExitButton1;
public javax.swing.JTextField IDBox1;
public javax.swing.JTextField PassBox;
public javax.swing.JLabel StatusLabel1;
public javax.swing.JPanel Tab1;
public javax.swing.JPanel Tab2;
public javax.swing.JPanel Tab3;
public javax.swing.JPanel Tab4;
public javax.swing.JPanel Tab5;
public javax.swing.JTabbedPane TabMenu;
public javax.swing.JButton jButton1;
public javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
public javax.swing.JLabel jLabel3;
public javax.swing.JLabel jLabel4;
public javax.swing.JLabel jLabel5;
public javax.swing.JLabel jLabel6;
public javax.swing.JSeparator jSeparator1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}
И коды класса C:
import javax.swing.JOptionPane;
public class Tab1Codes extends HotelHG{
byte PassTry;
//============================================================================== LOCK ALL TABS
public boolean LockAll(){
try
{
TabMenu.setEnabledAt(1, false);
TabMenu.setEnabledAt(2, false);
TabMenu.setEnabledAt(3, false);
TabMenu.setEnabledAt(4, false);
}catch(Exception e)
{
JOptionPane.showConfirmDialog(null, "Не можем да заключим разделите!",
"Заключване на разделите...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
//============================================================================== LOCK ALL TABS
//============================================================================== ADMINISTRATION
public void AdministrationControl( ){
PassTry=0;
byte No=1;
do{
if(ChechForIDPass(No)){
FindUserType(No);
No=4;
}
else{
No++;
PassTry++;
JOptionPane.showConfirmDialog(null, "Грешно Потребителско име или Парола!",
"П. Име-Парола Контрол...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
}
if(No == 1)
if(No == 2)
if(No == 3)
TabMenu.setEnabledAt(2, false);
PassTry=0;
}while(No<4);
}
//============================================================================== ADMINISTRATION
//============================================================================== CHECK FOR ID PASS
private boolean ChechForIDPass(byte NO){
try {
Res.absolute(NO);
if(IDBox1.getText().equals(Res.getString("ID"))) {
if(PassBox.getText().equals(Res.getString("Pass"))) {
return true;
}
}
}catch(Exception ex){
JOptionPane.showConfirmDialog(null, "Грешка с Връзка на Данни!",
"П. Име-Парола Контрол...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
}
return false;
}
//============================================================================== CHECK FOR ID PASS
//============================================================================== FIND USER TYPE
private void FindUserType(byte NO){
try {
Res.absolute(NO);
if(Res.getString("access").equalsIgnoreCase("Admin")){
PassTry=0;
StatusLabel1.setText("Статус:Админ Достъп");
JOptionPane.showConfirmDialog(null, "Верна Парала! Здравей Администратор!",
"П. Име-Парола Контрол...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.INFORMATION_MESSAGE);
TabMenu.setEnabledAt(2, true);
TabMenu.setEnabledAt(3, true);
TabMenu.setEnabledAt(4, true);
TabMenu.setEnabledAt(5, true);
}
if(Res.getString("access").equalsIgnoreCase("Reception")){
PassTry=0;
StatusLabel1.setText("Статус:Ресепция Достъп");
JOptionPane.showConfirmDialog(null, "Верна Парала! Здравей Потребител!",
"П. Име-Парола Контрол...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.INFORMATION_MESSAGE);
TabMenu.setEnabledAt(2, true);
TabMenu.setEnabledAt(3, true);
}
if(Res.getString("access").equalsIgnoreCase("Worker")){
PassTry=0;
StatusLabel1.setText("Статус:Работник Достъп");
JOptionPane.showConfirmDialog(null, "Верна Парала! Здравей Потребител!",
"П. Име-Парола Контрол...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.INFORMATION_MESSAGE);
TabMenu.setEnabledAt(4, true);
TabMenu.setEnabledAt(5, true);
}
}catch(Exception ex){
JOptionPane.showConfirmDialog(null, "Грешка с Връзка на Данни!",
"П. Име-Парола Контрол...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
}
}
//============================================================================== FIND USER TYPE
}