繁体   English   中英

鼠标悬停之前,JRadioButton不会出现

[英]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();
            }
        });
    }
}

我的问题是,当我编译并运行此代码时,jFrame出现了,但是只有一个问题,直到您将鼠标放在它们上面,才会出现3个JRadioButtons。 RMA和Williams单选按钮出现,中间的3个没有出现,为什么会这样呢?

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

您应该使用布局管理器。 人们认为使用“空布局”更容易,但实际上并非如此,并且您更容易出现代码错误。 布局管理器将正确定位和调整组件的大小,以确保显示所有组件。 有时,您甚至使用多个不同的布局管理器来获得所需的布局。

在这种情况下,您的问题是您有两个组件在容器中占据了相同的空间。 因此,一个组件会在另一个组件之上绘制。 将鼠标悬停在单选按钮上之后,由于按钮的翻转效果,该按钮将重新绘制。 但是,现在尝试调整框架的大小,单选按钮将消失,因为所有组件均已重新绘制,并且该组件再次绘制在按钮上方。

下面的代码行就是问题所在:

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

但是真正的解决方案是重写代码并使用布局管理器。 在使用时,请使用正确的Java命名约定。 变量名不能以大写字母开头。 您正确设置了“标题”和“ bg1”。 因此,修复“ EMA”,“ RMA”等...

@camickr是正确的。 注意使用布局管理器(以及一些重构)如何实际上可以简化您的代码。 另外,相关教程建议使用操作侦听器,而不是项目侦听器。

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();
            }
        });
    }
}

您应该使用以下方法添加JRadioButtons:

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);    
}

调用代码:

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

行动:

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

例如,然后将BoxLayout添加到页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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