Clemens Schalkw
Gebruiker
- Lid geworden
- 5 dec 2007
- Berichten
- 166
Volgens mij heb ik eerder ook al vragen gesteld naar aanleiding van het maken van een applet, maar ik ben volgens mij inmiddels een stuk verder.
Ik heb de lay-out helemaal klaar, maar ActionListeners werken ook, maar alleen in de compiler en niet in de Applet zelf.
Hoe kan ik ervoor zorgen dat bij het klikken op een knop (zie regel 173) een actie wordt uitgevoerd en wordt getoond in mijn JPanel?
Ik heb het geprobeerd, zoals te zien is, met repaint() en met validate(), maar beide lijken het niet goed te doen.
Wat doe ik fout?????
Ik heb de lay-out helemaal klaar, maar ActionListeners werken ook, maar alleen in de compiler en niet in de Applet zelf.
Hoe kan ik ervoor zorgen dat bij het klikken op een knop (zie regel 173) een actie wordt uitgevoerd en wordt getoond in mijn JPanel?
Ik heb het geprobeerd, zoals te zien is, met repaint() en met validate(), maar beide lijken het niet goed te doen.
Wat doe ik fout?????
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class Kreta extends JApplet
{
protected static final long serialVersionUID = 1;
JPanel headerPanel;
JPanel rightPanel;
JPanel footerPanel;
JPanel leftPanel;
JPanel centerPanel;
/*
* Include the JButtons which will be used in the applet.
*
* @addButton @newButton @deleteButton @editButton
*/
JButton addButton;
JButton newButton;
JButton deleteButton;
JButton editButton;
/*
* One JList will be added to the applet to list all dishes added to the database
*
* @dishesList
*/
JList dishesList;
/*
* Three JTextFields will be added to the applet to split name, dish & price to the database @ name @ dish @ price
*/
JTextField name;
JTextField dish;
JTextField price;
/*
* Two JTextArea's are added to the applet to show the ingredients and the recipe of a dish @ recipe @ ingredients
*/
JTextArea recipe;
JTextArea ingredients;
/*
* JLabels will be added to the textfields and textarea's
*/
JLabel nameTextField;
JLabel dishTextField;
JLabel priceTextField;
JLabel recipeTextArea;
JLabel ingredientsTextArea;
@Override
public void init()
{
addButton = new JButton("Voeg Toe");
newButton = new JButton("Nieuw");
deleteButton = new JButton("Verwijder");
editButton = new JButton("Bewerk");
String[] dishes = { "gerecht 1", "gerecht 2", "gerecht 3", "gerecht 4", "gerecht 5", "gerecht 6", "gerecht 7",
"gerecht 8", "gerecht 9", "gerecht 10" };
dishesList = new JList(dishes);
name = new JTextField(12);
nameTextField = new JLabel();
nameTextField.setText("naam");
dish = new JTextField(12);
dishTextField = new JLabel();
dishTextField.setText("gerecht");
price = new JTextField(5);
priceTextField = new JLabel();
priceTextField.setText("prijs €");
recipe = new JTextArea(10, 20);
recipeTextArea = new JLabel();
recipeTextArea.setText("recept");
ingredients = new JTextArea(10, 10);
ingredientsTextArea = new JLabel();
ingredientsTextArea.setText("ingrediënten");
/*
* Make different Jpanels for each boxLayout-region @ headerPanel @ rightPanel @ footerPanel @ leftPanel @
* centerPanel
*/
JPanel headerPanel = new JPanel();
JPanel rightPanel = new JPanel();
JPanel footerPanel = new JPanel();
JPanel leftPanel = new JPanel();
JPanel centerPanel = new JPanel();
headerPanel.setBackground(Color.red);
rightPanel.setBackground(Color.red);
footerPanel.setBackground(Color.red);
leftPanel.setBackground(Color.red);
centerPanel.setBackground(Color.blue);
JScrollPane recipeScroller = new JScrollPane(recipe);
JScrollPane ingredientsScroller = new JScrollPane(ingredients);
recipe.setLineWrap(true);
ingredients.setLineWrap(true);
recipeScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
recipeScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ingredientsScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
ingredientsScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
headerPanel.add(addButton);
headerPanel.add(newButton);
headerPanel.add(deleteButton);
headerPanel.add(editButton);
// werkt in compiler, niet in applet
addButton.addActionListener(new addAction());
newButton.addActionListener(new newAction());
deleteButton.addActionListener(new deleteAction());
editButton.addActionListener(new editAction());
leftPanel.add(dishesList);
footerPanel.add(nameTextField);
footerPanel.add(name);
footerPanel.add(dishTextField);
footerPanel.add(dish);
footerPanel.add(priceTextField);
footerPanel.add(price);
centerPanel.add(ingredientsScroller);
centerPanel.add(ingredientsTextArea);
centerPanel.add(ingredients);
centerPanel.add(recipeScroller);
centerPanel.add(recipeTextArea);
centerPanel.add(recipe);
getContentPane().add(BorderLayout.NORTH, headerPanel);
getContentPane().add(BorderLayout.EAST, rightPanel);
getContentPane().add(BorderLayout.SOUTH, footerPanel);
getContentPane().add(BorderLayout.WEST, leftPanel);
getContentPane().add(BorderLayout.CENTER, centerPanel);
this.setSize(600, 600);
setVisible(true);
} // close init-method
class addAction implements ActionListener
{
JTextField vak = new JTextField(20);
String mijnTekst = "Hello World";
public void actionPerformed(ActionEvent e)
{
vak.getText();
centerPanel.add(vak);
centerPanel.repaint();
}
}
class newAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("nieuw button");
}
}
class editAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("wijzig button");
}
}
class deleteAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Verwijder button");
}
}
@Override
public void start()
{
} // close start-method
} // close applet
Laatst bewerkt: