Java GUI Need some help with the GUI this is what I have package labCoin; public
ID: 653024 • Letter: J
Question
Java GUI
Need some help with the GUI this is what I have
package labCoin;
public class CoinApp{
public static void main(String[] args){
for(Coin el : Coin.values()){
System.out.printf("%s ", el);
}
}
}
______________________________________
package labCoin;
public enum Coin {
CENT(2.500, 19.05), NICKLE(5.0, 21.21), DIME(2.268, 17.91), QUARTER(5.670, 24.26);
private final double weight;
private final double diameter;
private Coin(double weightValue, double diameterValue){
weight = weightValue;
diameter = diameterValue;
}
@Override
public String toString(){
String coinName = super.toString();
return String.format("%s w:%.1f d:%.1f",coinName, weight, diameter);
}
}
___________________________________
package labCoin;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class CoinGUI {
public CoinGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 498, 300);
frame.setDefaultCloseOpertion(JFrame.EXIT_ON_CLOSE);
JPanel coinPanel = new JPanel();
coinPanel.setBackground(SystemColor.menu);
frame.getContentPane().add(coinPanel, BorderLayout.NORTH);
pennyBtn = new JRadioButton("");
penny.setIcon(new ImageIcon(this.getClass().getResource("UsCent.png")));
pennyBtn.addActionListener(new btnClickEvent());
coinPanel.add(pennyBtn);
nickelBtn = new JRadioButton("");
nickel.setIcon(new ImageIcon(this.getClass().getResource("UsNickel.png")));
nickelBtn.addActionListener(new btnClickEvent());
coinPanel.add(nickelBtn);
dimeBtn = new JRadioButton("");
dime.setIcon(new ImageIcon(this.getClass().getResource("UsDime.png")));
dimeBtn.addActionListener(new btnClickEvent());
coinPanel.add(dimeBtn);
quarterBtn = new JRadioButton("");
quarter.setIcon(new ImageIcon(this.getClass().getResource("UsQuarter.png")));
quarterBtn.addActionListener(new btnClickEvent());
coinPanel.add(quarterBtn);
coinInfo = new JLabel("Click a Coin");
coinInfo.setFont(new Font("SimSun", Font.PLAIN, 32));
coininfo.setHorizontalAlignment(SwingConstants.CENTER);
frame.getContentPane().add(coinInfo, BorderLayout.CENTER);
ButtonGroup coinGroup = new ButtonGroup();
coinGroup.add(pennyBtn);
coinGroup.add(nickelBtn);
coinGroup.add(dimeBtn);
coinGroup.add(quarterBtn);
}
private class btnClickEvent implements ActionListener{
@Override
public void actionPreformed(ActionEvent e){
if(pennyBtn.isSelected())
coinInfo.setText(Coin.CENT.toString());
if(nickelBtn.isSelected())
coinInfo.setText(Coin.NICKEL.toString());
if(dimeBtn.isSelected())
coinInfo.setText(Coin.DIME.toString());
if(quarterBtn.isSelected())
coinInfo.setText(Coin.QUARTER.toString());
}
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
public class CoinPanel extends JPanel{
private JButton buttons[] = new JButton[9];
private JLabel multiplySign[] = new JLabel[9];
private JLabel coinCount[] = new JLabel[9];
String [] names= {"1c", "2c", "5c", "10c", "20c", "50c", "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.