since there are no useful tutorials on the internet on how to use buttons I will now ask here how do I use buttons on java? I got one button to work to output "hello" but when I have muiltiple how do I make them work. the code I have is:
Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MaxHit extends JPanel { private static JButton Button; private JButton IncreaseStr,Increasebon,DecreaseStr,Decreasebon,Maxhitbut; private JComboBox Prayop,Potop; public MaxHit() { //construct preComponents String[] PrayopItems = {"None", "Burst of Strength", "Super Human Strength", "Ultimate Strength", "Chivarly", "Piety", "Turmoil"}; String[] PotopItems = {"None", "Strength potion", "Super Strength potions", "Extreme Strength potion"}; //construct components IncreaseStr = new JButton ("Increase Strength"); Increasebon = new JButton ("Increase Bonus"); DecreaseStr = new JButton ("Descrease Strength"); Decreasebon = new JButton ("Decrease Bonus"); Maxhitbut = new JButton ("Max hit"); Prayop = new JComboBox (PrayopItems); Potop = new JComboBox (PotopItems); //adjust size and set layout setPreferredSize (new Dimension (432, 362)); setLayout (null); //add components add (IncreaseStr); add (Increasebon); add (DecreaseStr); add (Decreasebon); add (Maxhitbut); add (Prayop); add (Potop); //set component bounds (only needed by Absolute Positioning) IncreaseStr.setBounds (115, 10, 190, 30); Increasebon.setBounds (115, 90, 190, 30); DecreaseStr.setBounds (115, 50, 190, 30); Decreasebon.setBounds (115, 130, 190, 30); Maxhitbut.setBounds (85, 270, 245, 65); Prayop.setBounds (105, 175, 210, 25); Potop.setBounds (105, 210, 210, 25); } public static void main (String[] args) { JFrame frame = new JFrame ("zzzzz"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new MaxHit()); frame.pack(); frame.setVisible (true); Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("test"); } }); } }
When I run it I get:
But the GUI still pops upCode:Exception in thread "main" java.lang.NullPointerException at MaxHit.main(MaxHit.java:60)
How Do i make buttons and lists work properly?




Reply With Quote







