package aufgabe3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MainMenu implements ActionListener, ListSelectionListener {
private static final int MAIN_WIDTH = 800;
private static final int MAIN_HEIGHT = 600;
private DefaultListModel listModel;
public static void main(final String... rArgs) {
new MainMenu().startup();
}
private JPanel buildMainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(buildContentPane(), BorderLayout.CENTER);
mainPanel.add(buildSidebarPane(), BorderLayout.EAST);
return mainPanel;
}
private JComponent buildContentPane() {
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(buildGraphicPanel(), BorderLayout.CENTER);
contentPane.add(buildResultPanel(), BorderLayout.SOUTH);
return contentPane;
}
private JComponent buildSidebarPane() {
JPanel sidebarPane = new JPanel();
sidebarPane.setLayout(new BorderLayout());
sidebarPane.add(buildListPanel(), BorderLayout.NORTH);
sidebarPane.add(buildControlPanel(), BorderLayout.SOUTH);
return sidebarPane;
}
private JPanel buildListPanel() {
JPanel listPanel = new JPanel();
listModel = new DefaultListModel();
JList list = new JList(listModel);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setPreferredSize(new Dimension(240, ((2 * MAIN_HEIGHT / 3) - 10)));
listPanel.add(scrollPane);
listPanel.setPreferredSize(new Dimension(250, (2 * MAIN_HEIGHT / 3)));
return listPanel;
}
/**Eingabe und Steuerfeld
*
* @return ControlPanel
*/
private JPanel buildControlPanel() {
JPanel controlPanel = new JPanel(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
// Erstellen der Buttons und ihrer Funktion
//Load Button
JButton loadButton = new JButton("Load");
loadButton.setToolTipText("Liste Laden");
loadButton.addActionListener(this);
loadButton.setActionCommand("Load");
loadButton.setMnemonic('o');
//Save Button
JButton saveButton = new JButton("Save");
saveButton.setToolTipText("Liste Speichern");
saveButton.addActionListener(this);
saveButton.setActionCommand("Save");
saveButton.setMnemonic('a');
//Eingabe Felder
JTextField realTextField = new JTextField(10);
JLabel label = new JLabel("+ j *");
JTextField imagTextField = new JTextField(10);
//Add Button
JButton addButton = new JButton("Add");
addButton.setToolTipText("Eingegebene Zahl hinzufügen");
addButton.addActionListener(this);
addButton.setActionCommand("Add");
addButton.setMnemonic('d');
//AddRandom Button
JButton addRandomButton = new JButton("AddRandom");
addRandomButton.setToolTipText("Eine Zufällige Zahl hinzufügen");
addRandomButton.addActionListener(this);
addRandomButton.setActionCommand("AddRandom");
addRandomButton.setMnemonic('R');
//Delete Button
JButton deleteButton = new JButton("Delete");
deleteButton.setToolTipText("Delete selected");
deleteButton.addActionListener(this);
deleteButton.setActionCommand("Del");
//Delete All Button
JButton deleteAllButton = new JButton("Delete All");
deleteAllButton.setToolTipText("Clear List");
deleteAllButton.addActionListener(this);
deleteAllButton.setActionCommand("DelAll");
// Textfeld für die Eingabe des Realanteils
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridheight = 1;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
controlPanel.add(realTextField, gridBagConstraints);
// Label für J *
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
controlPanel.add(label, gridBagConstraints);
// Textfeld für die Eingabe des Imaginäranteils
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 0;
controlPanel.add(imagTextField, gridBagConstraints);
// Der Addbutton
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
controlPanel.add(addButton, gridBagConstraints);
// Addrandom Button
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
controlPanel.add(addRandomButton, gridBagConstraints);
// Delete selected Button
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
controlPanel.add(deleteButton, gridBagConstraints);
// Clear List Button
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
controlPanel.add(deleteAllButton, gridBagConstraints);
// Load
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
controlPanel.add(loadButton, gridBagConstraints);
// Save
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 3;
controlPanel.add(saveButton, gridBagConstraints);
controlPanel.setPreferredSize(new Dimension(250, 100));
return controlPanel;
}
private JComponent buildGraphicPanel() {
JComponent graphicPanel = new JPanel();
graphicPanel.setPreferredSize(new Dimension(MAIN_WIDTH, MAIN_HEIGHT));
graphicPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
return graphicPanel;
}
private JComponent buildResultPanel() {
JComponent resultPanel = new JPanel(new GridLayout(2, 3, 5, 0));
JCheckBox checkSumme = new JCheckBox("Summe: ", true);
JCheckBox checkDifferenz = new JCheckBox("Differenz: ");
JCheckBox checkBoxProdukt = new JCheckBox("Produkt: ", true);
JCheckBox checkBoxQuotient = new JCheckBox("Quotient: ");
JLabel checkBoxBetrag = new JLabel("Betrag: ");
JLabel checkBoxPhase = new JLabel("Phase: ");
resultPanel.add(checkSumme);
resultPanel.add(checkDifferenz);
resultPanel.add(checkBoxBetrag);
resultPanel.add(checkBoxProdukt);
resultPanel.add(checkBoxQuotient);
resultPanel.add(checkBoxPhase);
resultPanel.setPreferredSize(new Dimension(MAIN_WIDTH, 100));
return resultPanel;
}
/**Erstellen der Menueleiste
*
* @return menuBar
*/
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem load = new JMenuItem("Load");
JMenuItem save = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
menuBar.add(fileMenu);
fileMenu.add(load);
load.addActionListener(this);
load.setActionCommand("Load");
load.setMnemonic('L');
fileMenu.add(save);
save.addActionListener(this);
save.setActionCommand("Save");
save.setMnemonic('S');
fileMenu.addSeparator();
fileMenu.add(exit);
exit.addActionListener(this);
exit.setActionCommand("Exit");
exit.setMnemonic('E');
return menuBar;
}
private void startup() {
JFrame frame = new JFrame("Complex Solutions");
frame.setResizable(false);
frame.setJMenuBar(createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(MAIN_WIDTH, MAIN_HEIGHT);
frame.add(buildMainPanel());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
}
@Override
public void actionPerformed(ActionEvent arg) {
if ("Load".equals(arg.getActionCommand()))
System.out.println("Load");
else if ("Save".equals(arg.getActionCommand()))
System.out.println("Save");
else if ("Add".equals(arg.getActionCommand()))
System.out.println("Add");
else if ("AddRandom".equals(arg.getActionCommand()))
System.out.println("AddRandom");
else if ("Del".equals(arg.getActionCommand()))
System.out.println("Del");
else if ("DelAll".equals(arg.getActionCommand()))
System.out.println("DelAll");
}
private void addNumber() {
try {
double real = Double.valueOf(realTextField.getText());
double imag = Double.valueOf(imaginary_txt.getText());
Complex newComplexNumber = new Complex(real, imag);
if (listModel.contains(newComplexNumber.toString()) == false) {
numberList.add(newComplexNumber);
listModel.addElement(newComplexNumber.toString());
cs.setSelectedIndicesArray(list.getSelectedIndices());
cs.repaint();
} else {
JOptionPane.showMessageDialog(null,
"You can't enter the same number twice", "Error",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"You entered an invalid number", "Error",
JOptionPane.INFORMATION_MESSAGE);
}
}
}