Reputation:
I'm making a "check writer" applet for homework and I'm trying to get the user's name to show up anywhere on the UI. I've tried a couple things but now I'm just trying to get the input onto a JLabel then onto a Panel.
public class project2 extends JPanel {
private JLabel labelName, labelAmount, resultName, resultMoney;
//private JTextArea resultName, resultMoney;
private JTextField name, dollarAmount;
private final int w = 1000, h = 200;
protected void paintComponent(Graphics g) {
g.drawString("Bank of Northwest Vista College", 425, 40);
g.drawString("Pay to the order of: ", 75, 70);
g.drawString("$", 730, 70);
}
public project2() {
setBackground(Color.white);
setPreferredSize(new Dimension(w, h));
labelName = new JLabel("Name:");
labelAmount = new JLabel("Check Amount:");
resultName = new JLabel("");
//resultMoney = new JLabel("$" + dollarAmount);
name = new JTextField(20);
name.addActionListener(new nameListener());
dollarAmount = new JTextField(20);
dollarAmount.addActionListener(new dollarListener());
add(labelName);
add(name);
add(labelAmount);
add(dollarAmount);
add(resultName);
//add(resultMoney);
}
where i basically need the most help
private class nameListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JLabel namelabel = new JLabel(name.getText());
JPanel namepanel = new JPanel();
namepanel.setPreferredSize(new Dimension(40, 10));
namepanel.add(name);
}
}
private class dollarListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String MONEY = dollarAmount.getText();
}
}
}
Upvotes: 0
Views: 77
Reputation: 285430
Problems:
Suggestions:
super.paintComponent(g);
method, usually as the first call within your override method. This will ensure that dirty pixels are cleaned up and that other component graphics housekeeping is done.For example:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class SimpleExampleWithComponents extends JPanel {
private static final int COLS = 15;
private JTextField nameField = new JTextField(COLS);
private JTextField amountField = new JTextField(COLS);
private JButton submitButton = new JButton("Submit");
private JLabel payeeLabel = new JLabel(" ");
private JLabel amountLabel = new JLabel("$ ");
public SimpleExampleWithComponents() {
SubmitListener submitListener = new SubmitListener();
nameField.addActionListener(submitListener);
amountField.addActionListener(submitListener);
submitButton.addActionListener(submitListener);
submitButton.setMnemonic(KeyEvent.VK_S);
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Name:"));
topPanel.add(nameField);
topPanel.add(Box.createHorizontalStrut(15));
topPanel.add(new JLabel("Amount:"));
topPanel.add(amountField);
topPanel.add(Box.createHorizontalStrut(15));
topPanel.add(submitButton);
// holds one row and 3 columns
JPanel middlePanel = new JPanel(new GridLayout(1, 3));
middlePanel.add(new JLabel("Pay to the order of:"));
middlePanel.add(payeeLabel);
middlePanel.add(Box.createHorizontalStrut(15));
middlePanel.add(amountLabel);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(middlePanel, BorderLayout.CENTER);
}
private class SubmitListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText().trim();
String amount = amountField.getText().trim();
payeeLabel.setText(name);
amountLabel.setText("$" + amount);
}
}
private static void createAndShowGui() {
SimpleExampleWithComponents mainPanel = new SimpleExampleWithComponents();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Upvotes: 1