program idea

Started by
1 comment, last by Kevinator 18 years, 4 months ago
I am implementing an idea I've had for a while now - an applet - and have run into four small problems that I can't seem to fix. First of all, I'm pretty new to the whole Java framework. I have a directory called "Widget" that contains all of my Java classes (in .java files) as well as the applet .java file. (The applet is supposed to call the other classes written in the other .java files.) I am using javac to compile the applet into a .class file. The problem is, how do I instruct javac to compile the applet .java AND all the other ten or so .java files at the same time? I suspect I may need to make a JAR file, but I've never done that before, and I have no idea how to. Anybody have any thoughts? Second, I'm having trouble getting my radio buttons to work properly. I've assigned them to a ButtonGroup, but they still act the same was as a checkbox in that they can be clicked, un-clicked, and more than one radio button (in the same ButtonGroup) can be checked at the same time. I want the radio buttons to act like they should! - just one button at a time. Here is an example of my code: ButtonGroup bg = new ButtonGroup(); JRadioButton b1 = new JRadioButton("Choice 1"); JRadioButton b2 = new JRadioButton("Choice 2"); bg.add(b1); bg.add(b2); this.add(bg); // "this" is the applet Third, I'm having a terrible time with my first attempt at using the event/listener setup. Let's say I have 100 JButtons, which all implement ActionListener. Well I define an actionPerformed(ActionEvent ae) function in the applet body. I want the function to detect which button sent the event, and to respond differently to each. I can't seem to figure out how to do this. And lastly, depending on user input, I want to draw and erase different control (JButtons, JCheckboxes, etc.) to the screen. How do I erase a button that's already been drawn to screen? hisDudeness
Well I believe in God, and the only thing that scares me is Keyser Soze.
Advertisement
1) javac *.java
2) don't know, haven't worked with Java in a long time
3) call the getSource method of the ActionEvent and you can compare it to an object. For example
if(e.getSource() == btn1) ...

4) don't know
2) Add your radiobuttons directly to the applet and it will work. Alternatively, add them to a JPanel and then add the JPanel to the applet. Adding the buttongroup directly won't work at runtime because ButtonGroup isn't a subclass of the AWT or Swing component heirarchy. ButtonGroup is just conceptual; not graphical.

Here is the simplest possible code to illustrate a working buttongroup (bare in mind I tested it so I know it works):

import javax.swing.*;public class Test {        public static void main(String[] args) {                JFrame f = new JFrame();                JRadioButton rb1 = new JRadioButton("Hi");                JRadioButton rb2 = new JRadioButton("heya");                ButtonGroup g = new ButtonGroup();                g.add(rb1);                g.add(rb2);                JPanel j = new JPanel();                j.add(rb1);                j.add(rb2);                f.getContentPane().add(j);                f.pack();                f.setVisible(true);        }}


4) If you want to "grey out" a button or other component, call setEnabled(false) on it. For example, if you had a button named button1, call button1.setEnabled(false) . To remove a component altogether, call setVisible(false) on it and conversely setVisible(true) to make it reappear.

This topic is closed to new replies.

Advertisement