Графический интерфейс - Java (240901)
Формулировка задачи:
Решил сделать форму при массива графических элементов. Нужно было обращаться к ним. Сделал такой метод в родителе
Полный код родителя
Полный код потомка
Потомок
После добавления данного метода во второго родителя, начали появляться ошибки
Почему так? Ведь когда добавил один метод, то все хорошо. А после второго ошибки
Я предполагаю что у меня рекурсия (как это исправить я не знаю)
Запустилась но теперь при нажатии на кнопку в ModeFrame выходит следующая ошибка
public void exittheMode(JButton exit, JFrame MF) { exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); MF.setVisible(true); MF.setSize(400, 310); } }); }
package classPackage; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import mainPackage.*; public class classAdminFrame extends JFrame { protected ModeFrame MF = new ModeFrame(); protected JButton[] button = new JButton [2]; protected String[] textBtn = {"Расcчитать и отправить стоимость тура", "Выйти из режима"}; protected Integer[] btncoordxy = {0, 270, 200, 200}; protected Integer[] btnscale = {255, 140, 30, 30}; protected Integer[] btnsizefont = {13, 13}; JLabel[] label = new JLabel[7]; protected String[] textlbl = {"Расчёт стоимости тура", "Заявка есть? -", "", "Стоимость проживания одного туриста за одну ночь -", "Стоимость перевозки туда и обратно -", "Стоимость питания -", "Маржинальный доход (в процентах) -"}; protected Integer[] lblcoordxy = {110, 0, 120, 0, 0, 0, 0, 0, 30, 30, 60, 90, 120, 150}; protected Integer[] lblscale = {285, 120, 20, 340, 240, 130, 235, 30, 30, 30, 30, 30, 30, 30}; protected Integer[] lblsizefont = {20, 14, 16, 14, 14, 14, 14}; JTextField[] textField = new JTextField[4]; protected Integer[] textFieldcoordxy = {340, 250, 135, 240, 60, 90, 120, 150}; protected int widthtextField = 50; protected int heighttextField = 30; protected int textFieldsizefont = 14; public void setJButton(String textBtn[], Integer btncoordxy[], Integer btnscale[], Integer btnsizefont[], JPanel panel) { for (int i = 0; i < 2; i++) { button[i] = new JButton(); button[i].setText(textBtn[i]); button[i].setBounds(btncoordxy[i], btncoordxy[i + 2], btnscale[i], btnscale[i + 2]); button[i].setFont(new Font("Segoe UI Light", Font.PLAIN, btnsizefont[i])); panel.add(button[i]); } } protected void setJLabel(String textlbl[], Integer lblcoordxy[], Integer lblscale[], Integer lblsizefont[], JPanel panel) { for (int i = 0; i < 7; i++) { label[i] = new JLabel(); label[i].setText(textlbl[i]); label[i].setBounds(lblcoordxy[i], lblcoordxy[i + 7], lblscale[i], lblscale[i + 7]); label[i].setFont(new Font("Segoe UI Light", Font.PLAIN, lblsizefont[i])); panel.add(label[i]); } } protected void setJTextField(Integer textFieldcoordxy[], int widthtextField, int heighttextField, int textFieldsizefont, JPanel panel) { for (int i = 0; i < 4; i++) { textField[i] = new JTextField(); textField[i].setBounds(textFieldcoordxy[i], textFieldcoordxy[i + 4], widthtextField, heighttextField); textField[i].setFont(new Font("Segoe UI Light", Font.PLAIN, textFieldsizefont)); panel.add(textField[i]); } } public JPanel setPanel (JPanel panel, int x, int y, int width, int height) { panel.setBounds(x, y, width, height); panel.setLayout(null); return panel; } public void exittheMode(JButton exit, JFrame MF) { exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); MF.setVisible(true); MF.setSize(400, 310); } }); } }
package mainPackage; import java.awt.*; import javax.swing.*; import classPackage.classAdminFrame; public class AdminFrame extends classAdminFrame { private JPanel panel = new JPanel(); public AdminFrame() { getContentPane().setLayout(null); setPanel(panel, 10, 11, 415, 320); getContentPane().add(panel); setJButton(textBtn, btncoordxy, btnscale, btnsizefont, panel); setJLabel(textlbl, lblcoordxy, lblscale, lblsizefont, panel); setJTextField(textFieldcoordxy, widthtextField, heighttextField, textFieldsizefont, panel); exittheMode(button[1], MF); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { AdminFrame frame = new AdminFrame(); frame.setVisible(true); frame.setSize(450, 310); } catch (Exception e) { e.printStackTrace(); } } }); } }
Решил сделать тоже самое и для других
Родитель
package classPackage; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import mainPackage.*; public class classModeFrame extends JFrame { protected AdminFrame AF = new AdminFrame(); protected JButton[] button = new JButton[2]; protected String[] textBtn = {"Турагент", "Турист"}; protected Integer[] btncoordxy = {80, 80, 60, 120}; protected Integer[] btnscale = {200, 200, 30, 30}; protected int btnsizefont = 14; public JPanel setPanel (JPanel panel, int x, int y, int width, int height) { panel.setBounds(x, y, width, height); panel.setLayout(null); return panel; } public JLabel setLabel(String namelabel, String labeltext, int x, int y, int width, int height, int size, JPanel panel) { JLabel label = new JLabel(); label.setName(namelabel); label.setText(labeltext); label.setBounds(x, y, width, height); label.setFont(new Font("Segoe UI Light", Font.PLAIN, size)); panel.add(label); return label; } protected void setJButton(String textBtn[], Integer btncoordxy[], Integer btnscale[], int btnsizefont, JPanel panel) { for (int i = 0; i < 2; i++) { button[i] = new JButton(); button[i].setText(textBtn[i]); button[i].setBounds(btncoordxy[i], btncoordxy[i + 2], btnscale[i], btnscale[i + 2]); button[i].setFont(new Font("Segoe UI Light", Font.PLAIN, btnsizefont)); panel.add(button[i]); } } public void gotoAdminFrame(JButton btnTourAgent, JFrame AF) { btnTourAgent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); AF.setVisible(true); AF.setSize(400, 310); } }); } }
package mainPackage; import classPackage.classModeFrame; import java.awt.*; import javax.swing.*; public class ModeFrame extends classModeFrame { private JPanel panel = new JPanel(); public ModeFrame() { getContentPane().setLayout(null); setPanel(panel, 10, 11, 415, 240); getContentPane().add(panel); setLabel("chooseMode", "Select mode", 145, 0, 80, 30, 14, panel); setJButton(textBtn, btncoordxy, btnscale, btnsizefont, panel); gotoAdminFrame(button[0], AF); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ModeFrame frame = new ModeFrame(); frame.setVisible(true); frame.setSize(400, 310); } catch (Exception e) { e.printStackTrace(); } } }); } }
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at sun.awt.Win32GraphicsConfig.getBounds(Native Method) at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source) at java.awt.Window.init(Unknown Source) at java.awt.Window.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at javax.swing.JFrame.<init>(Unknown Source) at classPackage.classAdminFrame.<init>(classAdminFrame.java:10) at mainPackage.AdminFrame.<init>(AdminFrame.java:13) at classPackage.classModeFrame.<init>(classModeFrame.java:12) at mainPackage.ModeFrame.<init>(ModeFrame.java:13) at classPackage.classAdminFrame.<init>(classAdminFrame.java:12) at mainPackage.AdminFrame.<init>(AdminFrame.java:13) at classPackage.classModeFrame.<init>(classModeFrame.java:12) at mainPackage.ModeFrame.<init>(ModeFrame.java:13) at classPackage.classAdminFrame.<init>(classAdminFrame.java:12) at mainPackage.AdminFrame.<init>(AdminFrame.java:13) ........
Как и предполагал у меня была рекурсия
Решение:
В родителе
public void gotoAdminFrame(JButton btnTourAgent, [B]AdminFrame admin[/B]) { btnTourAgent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { [B]AF = admin;[/B] dispose(); AF.setVisible(true); AF.setSize(400, 310); } }); }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at classPackage.classAdminFrame$1.actionPerformed(classAdminFrame.java:76) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Решение задачи: «Графический интерфейс»
textual
Листинг программы
public void exittheMode (JButton button, ModeFrame frame){ button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); frame.setVisible(true); frame.setSize(445, 310); } }); }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д