简体   繁体   中英

JRadioButton Will not appear until Mouse over

import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class IndicatorWindow implements ItemListener {

    JRadioButton RMA, EMA, SMA, Williams, Stochastic;
    JPanel IndPan, RadioPanel, title;
    JLabel Lab;
    JButton OK;

    public JPanel createContentPane() {
        JPanel GUI = new JPanel();
        GUI.setLayout(null);

        title = new JPanel();
        title.setLayout(null);
        title.setLocation(0, 0);
        title.setSize(500, 145);
        GUI.add(title);

        Lab = new JLabel("Please Select Indicator Type");
        Lab.setLocation(5, 0);
        Lab.setSize(200, 30);

        title.add(Lab);

        ButtonGroup bg1 = new ButtonGroup();

        RadioPanel = new JPanel();
        RadioPanel.setLayout(null);
        RadioPanel.setLocation(10, 30);
        RadioPanel.setSize(190, 220);
        GUI.add(RadioPanel);

        RMA = new JRadioButton("RMA");
        RMA.setLocation(0, 0);
        RMA.addItemListener(this);
        RMA.setSize(110, 20);
        bg1.add(RMA);
        RadioPanel.add(RMA);

        EMA = new JRadioButton("EMA");
        EMA.setLocation(0, 30);
        EMA.addItemListener(this);
        EMA.setSize(110, 20);
        bg1.add(EMA);
        RadioPanel.add(EMA);

        SMA = new JRadioButton("SMA");
        SMA.setLocation(0, 60);
        SMA.addItemListener(this);
        SMA.setSize(110, 20);
        bg1.add(SMA);
        RadioPanel.add(SMA);

        Stochastic = new JRadioButton("Stochastic");
        Stochastic.setLocation(0, 90);
        Stochastic.addItemListener(this);
        Stochastic.setSize(110, 20);
        bg1.add(Stochastic);
        RadioPanel.add(Stochastic);

        Williams = new JRadioButton("Williams");
        Williams.setLocation(0, 120);
        Williams.addItemListener(this);
        Williams.setSize(110, 20);
        bg1.add(Williams);
        RadioPanel.add(Williams);

        OK = new JButton();
        OK.setText("Confirm");
        OK.setLocation(45, 150);
        OK.addItemListener(this);
        OK.setSize(90, 30);
        RadioPanel.add(OK);

        //GUI.setOpaque(true);
        return GUI;

    }

    public void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();
        if (source == RMA) {
            System.out.print("Browse");
        } else if (source == EMA) {
            System.out.print("EMA");
        } else if (source == SMA) {
            System.out.print("SMA");
        } else if (source == Williams) {
            System.out.print("Williams");
        } else if (source == Stochastic) {
            System.out.print("Stochastic");
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Indicators");

        IndicatorWindow ind = new IndicatorWindow();
        frame.setContentPane(ind.createContentPane());

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(200, 250);
        frame.setLayout(null);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);

        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
        frame.setState(Frame.NORMAL);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}

My problem is that when i compile and run this code, the jFrame appears but there is only one problem, 3 JRadioButtons dont appear until you put your mouse over them. The RMA and Williams radiobuttons appear, the 3 in the middle do not though, any thoughts on why this is?

http://i.stack.imgur.com/gNnIb.jpg

You should be using layout managers. People think using a "null layout" is easier, but it is not and you are more prone to having errors with your code. Layout managers will position and size components properly to make sure all components are displayed. Sometimes you even use multiple different layout managers to achieve the layout you desire.

Your problem in this case is that you have two components occupying the same space in your container. So one component gets painted over top of the other. After you mouse over your radio button, the button is repainted because of the rollover effect of the button. However, now try resizing the frame and the radio buttons will disappear because all the components are repainted and the component is painted over top of the buttons again.

The following line of code is the problem:

// title.setSize(500, 145);
title.setSize(500, 20);

But the real solution is to rewrite the code and use layout managers. While you are at it use proper Java naming conventions. Variable names do NOT start with an uppercase letter. You got "title" and "bg1" correct. So fix "EMA", "RMA" etc...

@camickr is correct. Note how using layout managers (and a little re-factoring) can actually simplify your code. Also, the relevant tutorial suggests using an action listener, rather than an item listener.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5255337 */
public class IndicatorWindow implements ActionListener {

    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
    JRadioButton rma, ema, sma, stochastic, williams;
    ButtonGroup bg = new ButtonGroup();

    public JPanel createContentPane() {
        JPanel gui = new JPanel(new BorderLayout());

        JPanel title = new JPanel();
        JLabel lab = new JLabel("Please Select Indicator Type");
        title.add(lab);
        gui.add(title, BorderLayout.NORTH);

        createRadioButton(rma, "RMA");
        createRadioButton(ema, "EMA");
        createRadioButton(sma, "SMA");
        createRadioButton(stochastic, "Stochastic");
        createRadioButton(williams, "Williams");
        gui.add(radioPanel, BorderLayout.CENTER);

        JButton ok = new JButton();
        ok.setText("Confirm");
        ok.addActionListener(this);
        radioPanel.add(ok);

        return gui;
    }

    private void createRadioButton(JRadioButton jrb, String name) {
        jrb = new JRadioButton(name);
        bg.add(jrb);
        jrb.addActionListener(this);
        radioPanel.add(jrb);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Indicators");

        frame.add(new IndicatorWindow().createContentPane());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setAlwaysOnTop(true);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

You should add your JRadioButtons with a method:

private void bgAdd (String name, int y)
{
    JRadioButton rb = new JRadioButton (name);
    rb.setLocation (0, y);
    rb.addItemListener (this);
    rb.setSize (110, 19);
    bg1.add (rb);
    radioPanel.add (rb);    
}

Calling code:

    bgAdd ("RMA", 0);
    bgAdd ("EMA", 30);
    bgAdd ("SMA", 60);
    bgAdd ("Stochastic", 90);
    bgAdd ("Williams",  120);

Action:

public void itemStateChanged (ItemEvent e) {
    Object button = e.getItemSelectable ();
    String source = ((JRadioButton) button).getText ();
    System.out.print (source + " ");
}

Then add BoxLayout to the page, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM