我应该把我的主要方法放在这个应用程序的什么地方?
Where would I place my main method in this application?
我目前正在完成一项任务,我必须创建一个应用程序,该应用程序使用动态数据来填充树和列表。我曾尝试将 main 方法放在这个 class 之前,并且唯一一次看起来我可以编译的是如果我将所有变量都更改为静态。我可以将我的主要方法放在这个 class 中吗(我可以在哪里?)还是我需要另一个 class?谢谢。
import javax.swing.DefaultListModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import Accounts.Client;
import Accounts.ClientLoader;
import Accounts.Account;
import Accounts.Bootstrap;
import Accounts.Portfolio;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JTextArea;
public class FormWindow {
protected JFrame frame;
private DefaultListModel < String > clientDetail = new DefaultListModel < String > ();
private Client person;
private JTextField firstName;
private JTextField lastName;
private JTextField phoneNumber;
public FormWindow(Client person) {
this.person = person;
initialize();
}
private void initialize() {
frame = new JFrame("Client Manager");
frame.setBounds(100, 100, 903, 719);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 11, 619, 192);
frame.getContentPane().add(scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
firstName = new JTextField(person.getFirstName());
firstName.setBounds(730, 11, 144, 26);
frame.getContentPane().add(firstName);
firstName.setColumns(10);
lastName = new JTextField(person.getLastName());
lastName.setBounds(730, 66, 144, 26);
frame.getContentPane().add(lastName);
lastName.setColumns(10);
phoneNumber = new JTextField(person.getPhoneNumber());
phoneNumber.setBounds(730, 123, 144, 26);
frame.getContentPane().add(phoneNumber);
phoneNumber.setColumns(10);
JButton btnNewButton = new JButton("Update Interface");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
person.setFirstName(firstName.getText());
person.setLastName(lastName.getText());
person.setPhoneNumber(phoneNumber.getText());
repaintForm();
}
});
btnNewButton.setBounds(639, 168, 235, 35);
frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("First Name");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel.setBounds(649, 11, 71, 20);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Last Name");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1.setBounds(649, 70, 71, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel(" Phone");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_2.setBounds(634, 127, 71, 14);
frame.getContentPane().add(lblNewLabel_2);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setBounds(10, 214, 425, 436);
frame.getContentPane().add(scrollPane_1);
//needs work: TREE
DefaultMutableTreeNode portfolios = new DefaultMutableTreeNode("Portfolios");
JTree tree = new JTree(portfolios);
scrollPane_1.setViewportView(tree);
JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_2.setBounds(445, 214, 429, 436);
frame.getContentPane().add(scrollPane_2);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Client");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("Load Client");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("Save Client");
mnNewMenu.add(mntmNewMenuItem_1);
JMenu mnNewMenu_1 = new JMenu("Portfolios");
menuBar.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("Add Portfolio");
mnNewMenu_1.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("Remove Portfolio");
mnNewMenu_1.add(mntmNewMenuItem_3);
JMenu mnNewMenu_2 = new JMenu("Accounts");
menuBar.add(mnNewMenu_2);
JMenuItem mntmNewMenuItem_4 = new JMenuItem("Add Account");
mnNewMenu_2.add(mntmNewMenuItem_4);
JMenuItem mntmNewMenuItem_5 = new JMenuItem("Remove Account");
mnNewMenu_2.add(mntmNewMenuItem_5);
}
void repaintForm() {
clientDetail.clear();
clientDetail.addElement("First Name: " + person.getFirstName());
clientDetail.addElement("Last Name: " + person.getLastName());
clientDetail.addElement("Phone Number: " + person.getPhoneNumber());
clientDetail.addElement("Total Value: " + person.getClientPortfolioValue());
clientDetail.addElement("Portfolio Count: " + person.getPortfolioCount());
clientDetail.addElement("Portfolio Names: " + person.getClientPortfolioNames());
}
}
您需要另一个 class 并调用 FormWindow 的新实例 class 如果变量是静态的,那么您需要将 return 值分配给该特定静态变量的方法
考虑在 main 方法中创建一个 FormWindow 的实例。
将 main 放入 FormWindow class
在 main 方法中通过“new”运算符创建实例
class FormWindow {
public static void main(String[] args) {
FormWindow form = new FormWindow(...);
}
...
}
在一个静态方法中你只能调用其他静态方法。这就是编译器将您的方法更改为静态的原因。您想要的是使用 main 方法创建另一个 class,然后创建 FormWindow class 的对象。由于您的构造函数需要一个 Client 对象,因此您需要先创建该对象。
FormWindow fw = new FormWindow(someClientObject)
我认为你必须为此创建一个默认构造函数 class 要么提供一些默认值:
public class FormWindow {
public FormWindow() {
Client personWithDefaultValues = new Client();
//Fill personWithDefaultValues with some data here
}
public static void main(String[] args) {
FormWindow h = new FormWindow();
h.initialize();
}
}
我目前正在完成一项任务,我必须创建一个应用程序,该应用程序使用动态数据来填充树和列表。我曾尝试将 main 方法放在这个 class 之前,并且唯一一次看起来我可以编译的是如果我将所有变量都更改为静态。我可以将我的主要方法放在这个 class 中吗(我可以在哪里?)还是我需要另一个 class?谢谢。
import javax.swing.DefaultListModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import Accounts.Client;
import Accounts.ClientLoader;
import Accounts.Account;
import Accounts.Bootstrap;
import Accounts.Portfolio;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JTextArea;
public class FormWindow {
protected JFrame frame;
private DefaultListModel < String > clientDetail = new DefaultListModel < String > ();
private Client person;
private JTextField firstName;
private JTextField lastName;
private JTextField phoneNumber;
public FormWindow(Client person) {
this.person = person;
initialize();
}
private void initialize() {
frame = new JFrame("Client Manager");
frame.setBounds(100, 100, 903, 719);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 11, 619, 192);
frame.getContentPane().add(scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
firstName = new JTextField(person.getFirstName());
firstName.setBounds(730, 11, 144, 26);
frame.getContentPane().add(firstName);
firstName.setColumns(10);
lastName = new JTextField(person.getLastName());
lastName.setBounds(730, 66, 144, 26);
frame.getContentPane().add(lastName);
lastName.setColumns(10);
phoneNumber = new JTextField(person.getPhoneNumber());
phoneNumber.setBounds(730, 123, 144, 26);
frame.getContentPane().add(phoneNumber);
phoneNumber.setColumns(10);
JButton btnNewButton = new JButton("Update Interface");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
person.setFirstName(firstName.getText());
person.setLastName(lastName.getText());
person.setPhoneNumber(phoneNumber.getText());
repaintForm();
}
});
btnNewButton.setBounds(639, 168, 235, 35);
frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("First Name");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel.setBounds(649, 11, 71, 20);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Last Name");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1.setBounds(649, 70, 71, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel(" Phone");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_2.setBounds(634, 127, 71, 14);
frame.getContentPane().add(lblNewLabel_2);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setBounds(10, 214, 425, 436);
frame.getContentPane().add(scrollPane_1);
//needs work: TREE
DefaultMutableTreeNode portfolios = new DefaultMutableTreeNode("Portfolios");
JTree tree = new JTree(portfolios);
scrollPane_1.setViewportView(tree);
JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_2.setBounds(445, 214, 429, 436);
frame.getContentPane().add(scrollPane_2);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Client");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("Load Client");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("Save Client");
mnNewMenu.add(mntmNewMenuItem_1);
JMenu mnNewMenu_1 = new JMenu("Portfolios");
menuBar.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("Add Portfolio");
mnNewMenu_1.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("Remove Portfolio");
mnNewMenu_1.add(mntmNewMenuItem_3);
JMenu mnNewMenu_2 = new JMenu("Accounts");
menuBar.add(mnNewMenu_2);
JMenuItem mntmNewMenuItem_4 = new JMenuItem("Add Account");
mnNewMenu_2.add(mntmNewMenuItem_4);
JMenuItem mntmNewMenuItem_5 = new JMenuItem("Remove Account");
mnNewMenu_2.add(mntmNewMenuItem_5);
}
void repaintForm() {
clientDetail.clear();
clientDetail.addElement("First Name: " + person.getFirstName());
clientDetail.addElement("Last Name: " + person.getLastName());
clientDetail.addElement("Phone Number: " + person.getPhoneNumber());
clientDetail.addElement("Total Value: " + person.getClientPortfolioValue());
clientDetail.addElement("Portfolio Count: " + person.getPortfolioCount());
clientDetail.addElement("Portfolio Names: " + person.getClientPortfolioNames());
}
}
您需要另一个 class 并调用 FormWindow 的新实例 class 如果变量是静态的,那么您需要将 return 值分配给该特定静态变量的方法
考虑在 main 方法中创建一个 FormWindow 的实例。
将 main 放入 FormWindow class
在 main 方法中通过“new”运算符创建实例
class FormWindow { public static void main(String[] args) { FormWindow form = new FormWindow(...); } ... }
在一个静态方法中你只能调用其他静态方法。这就是编译器将您的方法更改为静态的原因。您想要的是使用 main 方法创建另一个 class,然后创建 FormWindow class 的对象。由于您的构造函数需要一个 Client 对象,因此您需要先创建该对象。
FormWindow fw = new FormWindow(someClientObject)
我认为你必须为此创建一个默认构造函数 class 要么提供一些默认值:
public class FormWindow {
public FormWindow() {
Client personWithDefaultValues = new Client();
//Fill personWithDefaultValues with some data here
}
public static void main(String[] args) {
FormWindow h = new FormWindow();
h.initialize();
}
}